Changeset View
Changeset View
Standalone View
Standalone View
src/tests/event-emitter-test.cpp
| /* | /* | ||||
| * This file is part of libkazv. | * This file is part of libkazv. | ||||
| * SPDX-FileCopyrightText: 2020 Tusooa Zhu <tusooa@kazv.moe> | * SPDX-FileCopyrightText: 2020-2026 tusooa <tusooa@kazv.moe> | ||||
| * SPDX-License-Identifier: AGPL-3.0-or-later | * SPDX-License-Identifier: AGPL-3.0-or-later | ||||
| */ | */ | ||||
| #include <libkazv-config.hpp> | #include <libkazv-config.hpp> | ||||
| #include <chrono> | #include <chrono> | ||||
| #include <catch2/catch_all.hpp> | #include <catch2/catch_all.hpp> | ||||
| Show All 9 Lines | |||||
| { | { | ||||
| boost::asio::io_context ioContext; | boost::asio::io_context ioContext; | ||||
| LagerStoreEventEmitter ee{lager::with_boost_asio_event_loop{ioContext.get_executor()}}; | LagerStoreEventEmitter ee{lager::with_boost_asio_event_loop{ioContext.get_executor()}}; | ||||
| auto watchable = ee.watchable(); | auto watchable = ee.watchable(); | ||||
| int counter = 0; | int counter = 0; | ||||
| watchable.after<SyncSuccessful>( | watchable.after<ReceivingPresenceEvent>( | ||||
| [&](auto) { | [&](auto) { | ||||
| ++counter; | ++counter; | ||||
| }); | }); | ||||
| SECTION("Event handlers should be run properly") { | SECTION("Event handlers should be run properly") { | ||||
| ee.emit(SyncSuccessful{}); | ee.emit(ReceivingPresenceEvent{Event()}); | ||||
| ee.emit(SyncFailed{}); | ee.emit(ReceivingRoomTimelineEvent{Event(), ""}); | ||||
| ioContext.run(); | ioContext.run(); | ||||
| REQUIRE(counter == 1); | REQUIRE(counter == 1); | ||||
| } | } | ||||
| SECTION("Event handlers should be called when every event fires off") { | SECTION("Event handlers should be called when every event fires off") { | ||||
| for (int i = 0; i < 100; ++i) { | for (int i = 0; i < 100; ++i) { | ||||
| ee.emit(SyncSuccessful{}); | ee.emit(ReceivingPresenceEvent{Event()}); | ||||
| } | } | ||||
| ioContext.run(); | ioContext.run(); | ||||
| REQUIRE(counter == 100); | REQUIRE(counter == 100); | ||||
| } | } | ||||
| SECTION("Handler should be disconnected once watchable is destroyed") { | SECTION("Handler should be disconnected once watchable is destroyed") { | ||||
| int counter2 = 0; | int counter2 = 0; | ||||
| auto guard = boost::asio::make_work_guard(ioContext.get_executor()); | auto guard = boost::asio::make_work_guard(ioContext.get_executor()); | ||||
| auto thread = std::thread([&] { ioContext.run(); }); | auto thread = std::thread([&] { ioContext.run(); }); | ||||
| ee.emit(SyncFailed{}); | ee.emit(ReceivingRoomTimelineEvent{}); | ||||
| std::this_thread::sleep_for(std::chrono::milliseconds{100}); | std::this_thread::sleep_for(std::chrono::milliseconds{100}); | ||||
| { | { | ||||
| auto watchable2 = ee.watchable(); | auto watchable2 = ee.watchable(); | ||||
| watchable2.after<SyncFailed>( | watchable2.after<ReceivingRoomTimelineEvent>( | ||||
| [&](auto) { | [&](auto) { | ||||
| ++counter2; | ++counter2; | ||||
| }); | }); | ||||
| ee.emit(SyncSuccessful{}); | ee.emit(ReceivingRoomTimelineEvent{}); | ||||
| ee.emit(SyncFailed{}); | ee.emit(ReceivingPresenceEvent{}); | ||||
| std::this_thread::sleep_for(std::chrono::milliseconds{100}); | std::this_thread::sleep_for(std::chrono::milliseconds{100}); | ||||
| } | } | ||||
| ee.emit(SyncFailed{}); | ee.emit(ReceivingRoomTimelineEvent{}); | ||||
| guard.reset(); | guard.reset(); | ||||
| thread.join(); | thread.join(); | ||||
| REQUIRE(counter == 1); | REQUIRE(counter == 1); | ||||
| REQUIRE(counter2 == 1); | REQUIRE(counter2 == 1); | ||||
| } | } | ||||
| } | } | ||||