Changeset View
Changeset View
Standalone View
Standalone View
src/tests/kazvjobtest.cpp
| Show First 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | TEST_CASE("setInterval can be cancelled", "[kazvjob]") | ||||
| boost::asio::io_context ioContext; | boost::asio::io_context ioContext; | ||||
| CprJobHandler h(ioContext.get_executor()); | CprJobHandler h(ioContext.get_executor()); | ||||
| std::vector<int> v; | std::vector<int> v; | ||||
| std::string id{"testTimerId"}; | std::string id{"testTimerId"}; | ||||
| h.setInterval( | h.setInterval( | ||||
| [&v, &h, id] { | [&v, &h, id]() { | ||||
| v.push_back(50); | v.push_back(50); | ||||
| std::cout << "timer executed" << std::endl; | std::cout << "timer executed" << std::endl; | ||||
| if (v.size() >= 2) { | if (v.size() >= 2) { | ||||
| h.cancel(id); | h.cancel(id); | ||||
| } | } | ||||
| }, 100, id); | }, 100, id); | ||||
| h.setTimeout( | h.setTimeout( | ||||
| [&h] { | [&h]() { | ||||
| h.stop(); | h.stop(); | ||||
| }, 300); | }, 300); | ||||
| ioContext.run(); | ioContext.run(); | ||||
| REQUIRE( v.size() == 2 ); | REQUIRE( v.size() == 2 ); | ||||
| REQUIRE( v[0] == 50 ); | REQUIRE( v[0] == 50 ); | ||||
| REQUIRE( v[1] == 50 ); | REQUIRE( v[1] == 50 ); | ||||
| } | |||||
| TEST_CASE("clearTimer should erase timer from map when timer fires", "[kazvjob]") | |||||
| { | |||||
| boost::asio::io_context ioContext; | |||||
| CprJobHandler h(ioContext.get_executor()); | |||||
| std::vector<int> firedWithIds; | |||||
| // Set two timers with the same timerId - each fires once and | |||||
| // is cleared via clearTimer -> erase-remove, then a new timer | |||||
| // with the same ID is set | |||||
| h.setTimeout( | |||||
| [&firedWithIds, &h]() { | |||||
| firedWithIds.push_back(1); | |||||
| // Set a new timer with the same ID after this one fires | |||||
| h.setTimeout( | |||||
| [&firedWithIds, &h]() { | |||||
| firedWithIds.push_back(2); | |||||
| h.stop(); | |||||
| }, 50, "shared-id"); | |||||
| }, 50, "shared-id"); | |||||
| ioContext.run(); | |||||
| REQUIRE(firedWithIds.size() == 2); | |||||
| REQUIRE(firedWithIds[0] == 1); | |||||
| REQUIRE(firedWithIds[1] == 2); | |||||
| } | |||||
| TEST_CASE("Multiple timers with same id should be independently clearable", "[kazvjob]") | |||||
| { | |||||
| boost::asio::io_context ioContext; | |||||
| CprJobHandler h(ioContext.get_executor()); | |||||
| std::vector<int> fired; | |||||
| // After each timer fires, clearTimer via erase-remove should | |||||
| // remove only that specific timer, not affect others | |||||
| h.setTimeout( | |||||
| [&fired]() { | |||||
| fired.push_back(1); | |||||
| }, 50, "multi-id"); | |||||
| h.setTimeout( | |||||
| [&fired]() { | |||||
| fired.push_back(2); | |||||
| }, 100, "multi-id"); | |||||
| h.setTimeout( | |||||
| [&fired, &h]() { | |||||
| fired.push_back(3); | |||||
| h.stop(); | |||||
| }, 150, "multi-id"); | |||||
| ioContext.run(); | |||||
| REQUIRE(fired.size() == 3); | |||||
| REQUIRE(fired[0] == 1); | |||||
| REQUIRE(fired[1] == 2); | |||||
| REQUIRE(fired[2] == 3); | |||||
| } | } | ||||
| static BaseJob succJob = | static BaseJob succJob = | ||||
| BaseJob(TEST_SERVER_URL, "/.well-known/matrix/client", BaseJob::Get{}, "TestJob") | BaseJob(TEST_SERVER_URL, "/.well-known/matrix/client", BaseJob::Get{}, "TestJob") | ||||
| .withQueue("testjob"); | .withQueue("testjob"); | ||||
| static BaseJob failJob = | static BaseJob failJob = | ||||
| BaseJob(TEST_SERVER_URL, "/.well-known/jfasjewfn/awejioaewjgjaad/fawre", BaseJob::Get{}, "AnotherTestJob") | BaseJob(TEST_SERVER_URL, "/.well-known/jfasjewfn/awejioaewjgjaad/fawre", BaseJob::Get{}, "AnotherTestJob") | ||||
| ▲ Show 20 Lines • Show All 138 Lines • Show Last 20 Lines | |||||