From 3bfa06b33c5c17b58a82d70688a3888bf0c193d1 Mon Sep 17 00:00:00 2001 From: Dawid Drozd Date: Fri, 18 Jun 2021 12:55:52 +0200 Subject: [PATCH] Add unit test for processing events behavior --- .../dexode/eventbus/test/SuiteEventBus.cpp | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/test/unit/src/dexode/eventbus/test/SuiteEventBus.cpp b/test/unit/src/dexode/eventbus/test/SuiteEventBus.cpp index a11aa84..c01d26a 100644 --- a/test/unit/src/dexode/eventbus/test/SuiteEventBus.cpp +++ b/test/unit/src/dexode/eventbus/test/SuiteEventBus.cpp @@ -369,7 +369,6 @@ TEST_CASE("Should process event When listener transit", "[EventBus]") REQUIRE(bus.process() == 0); // All cases should be same because of deterministic way of processing - SECTION("Post event before transit") { bus.postpone(event::Value{3}); // <-- before @@ -396,6 +395,36 @@ TEST_CASE("Should process event When listener transit", "[EventBus]") CHECK(listenerBReceiveEvent == 1); } +TEST_CASE("Should NOT process event When listener unlisten before process", "[EventBus]") +{ + EventBus bus; + auto listener = EventBus::Listener::createNotOwning(bus); + + int listenerReceiveEvent = 0; + + listener.listen([&](const event::Value& event) { ++listenerReceiveEvent; }); + + REQUIRE(bus.process() == 0); + bus.postpone(event::Value{3}); + REQUIRE(bus.process() == 1); + CHECK(listenerReceiveEvent == 1); + + // All cases should be same because of deterministic way of processing + SECTION("Post event before unlisten") + { + bus.postpone(event::Value{3}); // <-- before + listener.unlistenAll(); + } + SECTION("Post event after transit") + { + listener.unlistenAll(); + bus.postpone(event::Value{3}); // <-- after + } + + REQUIRE(bus.process() == 1); + CHECK(listenerReceiveEvent == 1); +} + TEST_CASE("Should distinguish event producer When", "[EventBus]") { // EventBus bus;