Skip to content

Commit

Permalink
majordomoworker_rest_tests: test with LongPollIdx
Browse files Browse the repository at this point in the history
Fix the unittests which do not corretly handle the
LongPollingIdx parameter.

Signed-off-by: Alexander Krimm <[email protected]>
  • Loading branch information
wirew0rm authored and RalphSteinhagen committed Nov 7, 2024
1 parent 83c4743 commit 8da3e2f
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/majordomo/test/majordomoworker_rest_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@ std::jthread makeGetRequestResponseCheckerThread(const std::string &address, con
});
}

std::jthread makeLongPollingRequestResponseCheckerThread(const std::string &address, const std::vector<std::string> &requiredResponses, const std::vector<int> &requiredStatusCodes = {}, [[maybe_unused]] std::source_location location = std::source_location::current()) {
return std::jthread([=] {
httplib::Client http("localhost", majordomo::DEFAULT_REST_PORT);
http.set_follow_location(true);
http.set_keep_alive(true);
#define requireWithSource(arg) \
if (!(arg)) opencmw::zmq::debug::withLocation(location) << "<- call got a failed requirement:"; \
REQUIRE(arg)
for (std::size_t i = 0; i < requiredResponses.size(); ++i) {
const std::string url = fmt::format("{}{}LongPollingIdx={}", address, address.contains('?') ? "&" : "?", i == 0 ? "Next" : fmt::format("{}", i));
const auto response = http.Get(url);
if (i == 0) { // check forwarding to the explicit index
REQUIRE(response->location.find("&LongPollingIdx=0") != std::string::npos);
}
requireWithSource(response);
const auto requiredStatusCode = i < requiredStatusCodes.size() ? requiredStatusCodes[i] : 200;
requireWithSource(response->status == requiredStatusCode);
requireWithSource(response->body.find(requiredResponses[i]) != std::string::npos);
}
#undef requireWithSource
});
}

struct ColorContext {
bool red = false;
bool green = false;
Expand Down Expand Up @@ -252,12 +275,12 @@ TEST_CASE("Subscriptions", "[majordomo][majordomoworker][subscription]") {

REQUIRE(waitUntilWorkerServiceAvailable(broker.context, worker));

auto allListener = makeGetRequestResponseCheckerThread("/colors?LongPollingIdx=Next", { "0", "1", "2", "3", "4", "5", "6" });
auto redListener = makeGetRequestResponseCheckerThread("/colors?LongPollingIdx=Next&red", { "0", "3", "4", "6" });
auto yellowListener = makeGetRequestResponseCheckerThread("/colors?LongPollingIdx=Next&red&green", { "4", "6" });
auto whiteListener1 = makeGetRequestResponseCheckerThread("/colors?LongPollingIdx=Next&red&green&blue", { "6" });
auto whiteListener2 = makeGetRequestResponseCheckerThread("/colors?LongPollingIdx=Next&green&red&blue", { "6" });
auto whiteListener3 = makeGetRequestResponseCheckerThread("/colors?LongPollingIdx=Next&blue&green&red", { "6" });
auto allListener = makeLongPollingRequestResponseCheckerThread("/colors", { "0", "1", "2", "3", "4", "5", "6" });
auto redListener = makeLongPollingRequestResponseCheckerThread("/colors?red", { "0", "3", "4", "6" });
auto yellowListener = makeLongPollingRequestResponseCheckerThread("/colors?red&green", { "4", "6" });
auto whiteListener1 = makeLongPollingRequestResponseCheckerThread("/colors?red&green&blue", { "6" });
auto whiteListener2 = makeLongPollingRequestResponseCheckerThread("/colors?green&red&blue", { "6" });
auto whiteListener3 = makeLongPollingRequestResponseCheckerThread("/colors?blue&green&red", { "6" });

std::this_thread::sleep_for(50ms); // give time for subscriptions to happen

Expand Down

0 comments on commit 8da3e2f

Please sign in to comment.