Skip to content

Commit

Permalink
test: Allow multiple trials when unittesting http headers (y-scope#613)
Browse files Browse the repository at this point in the history
Co-authored-by: Xiaochong Wei <[email protected]>
  • Loading branch information
anlowee and Xiaochong Wei authored Nov 28, 2024
1 parent f1876cd commit 12cdf6d
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions components/core/tests/test-NetworkReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,26 +196,36 @@ TEST_CASE("network_reader_with_valid_http_header_kv_pairs", "[NetworkReader]") {
std::unordered_map<std::string, std::string> valid_http_header_kv_pairs;
// We use httpbin (https://httpbin.org/) to test the user-specified headers. On success, it is
// supposed to respond all the user-specified headers as key-value pairs in JSON form.
constexpr int cNumHttpHeaderKeyValuePairs{10};
constexpr size_t cNumHttpHeaderKeyValuePairs{10};
for (size_t i{0}; i < cNumHttpHeaderKeyValuePairs; ++i) {
valid_http_header_kv_pairs.emplace(
fmt::format("Unit-Test-Key{}", i),
fmt::format("Unit-Test-Value{}", i)
);
}
clp::NetworkReader reader{
"https://httpbin.org/headers",
0,
false,
clp::CurlDownloadHandler::cDefaultOverallTimeout,
clp::CurlDownloadHandler::cDefaultConnectionTimeout,
clp::NetworkReader::cDefaultBufferPoolSize,
clp::NetworkReader::cDefaultBufferSize,
valid_http_header_kv_pairs
};
auto const content{get_content(reader)};
REQUIRE(assert_curl_error_code(CURLE_OK, reader));
auto const parsed_content = nlohmann::json::parse(content);
std::optional<std::vector<char>> optional_content;
// Retry the unit test a limited number of times to handle transient server-side HTTP errors.
// This ensures the test is not marked as failed due to temporary issues beyond our control.
constexpr size_t cNumMaxTrials{10};
for (size_t i{0}; i < cNumMaxTrials; ++i) {
clp::NetworkReader reader{
"https://httpbin.org/headers",
0,
false,
clp::CurlDownloadHandler::cDefaultOverallTimeout,
clp::CurlDownloadHandler::cDefaultConnectionTimeout,
clp::NetworkReader::cDefaultBufferPoolSize,
clp::NetworkReader::cDefaultBufferSize,
valid_http_header_kv_pairs
};
auto const content = get_content(reader);
if (assert_curl_error_code(CURLE_OK, reader)) {
optional_content.emplace(content);
break;
}
}
REQUIRE(optional_content.has_value());
auto const parsed_content = nlohmann::json::parse(optional_content.value());
auto const& headers{parsed_content.at("headers")};
for (auto const& [key, value] : valid_http_header_kv_pairs) {
REQUIRE((value == headers.at(key).get<std::string_view>()));
Expand Down

0 comments on commit 12cdf6d

Please sign in to comment.