From 25382df3526a9677728be5d0e21b7c3840019e1b Mon Sep 17 00:00:00 2001 From: Andy Ford Date: Wed, 5 Oct 2022 17:42:49 +0100 Subject: [PATCH] fix: properly trigger events after push events --- .../prenote/PrenoteAcknowledgedPushEventHandler.cpp | 9 ++++++--- .../prenote/PrenoteAcknowledgedPushEventHandler.h | 8 +++++++- .../prenote/PrenoteDeletedPushEventHandler.cpp | 9 ++++++--- src/plugin/prenote/PrenoteDeletedPushEventHandler.h | 8 +++++++- src/plugin/prenote/PrenoteModule.cpp | 8 ++++---- .../PrenoteAcknowledgedPushEventHandlerTest.cpp | 12 +++++++++++- .../prenote/PrenoteDeletedPushEventHandlerTest.cpp | 12 +++++++++++- 7 files changed, 52 insertions(+), 14 deletions(-) diff --git a/src/plugin/prenote/PrenoteAcknowledgedPushEventHandler.cpp b/src/plugin/prenote/PrenoteAcknowledgedPushEventHandler.cpp index f633bede6..16d01ef90 100644 --- a/src/plugin/prenote/PrenoteAcknowledgedPushEventHandler.cpp +++ b/src/plugin/prenote/PrenoteAcknowledgedPushEventHandler.cpp @@ -1,14 +1,15 @@ #include "PrenoteAcknowledgedPushEventHandler.h" #include "PrenoteMessage.h" #include "PrenoteMessageCollection.h" +#include "PrenoteMessageEventHandlerCollection.h" #include "push/PushEventSubscription.h" using UKControllerPlugin::Push::PushEventSubscription; namespace UKControllerPlugin::Prenote { PrenoteAcknowledgedPushEventHandler::PrenoteAcknowledgedPushEventHandler( - std::shared_ptr prenotes) - : prenotes(std::move(prenotes)) + std::shared_ptr prenotes, const PrenoteMessageEventHandlerCollection& eventHandlers) + : prenotes(std::move(prenotes)), eventHandlers(eventHandlers) { } @@ -21,7 +22,9 @@ namespace UKControllerPlugin::Prenote { } int prenoteId = messageData.at("id").get(); - this->prenotes->GetById(prenoteId)->Acknowledge(); + const auto prenote = prenotes->GetById(prenoteId); + prenote->Acknowledge(); + eventHandlers.MessageAcknowledged(*prenote); LogInfo("Acknowledged prenote id " + std::to_string(prenoteId)); } diff --git a/src/plugin/prenote/PrenoteAcknowledgedPushEventHandler.h b/src/plugin/prenote/PrenoteAcknowledgedPushEventHandler.h index 1280fbcd2..a97d9d906 100644 --- a/src/plugin/prenote/PrenoteAcknowledgedPushEventHandler.h +++ b/src/plugin/prenote/PrenoteAcknowledgedPushEventHandler.h @@ -3,11 +3,14 @@ namespace UKControllerPlugin::Prenote { class PrenoteMessageCollection; + class PrenoteMessageEventHandlerCollection; class PrenoteAcknowledgedPushEventHandler : public UKControllerPlugin::Push::PushEventProcessorInterface { public: - explicit PrenoteAcknowledgedPushEventHandler(std::shared_ptr prenotes); + explicit PrenoteAcknowledgedPushEventHandler( + std::shared_ptr prenotes, + const PrenoteMessageEventHandlerCollection& eventHandlers); void ProcessPushEvent(const Push::PushEvent& message) override; [[nodiscard]] auto GetPushEventSubscriptions() const -> std::set override; @@ -17,5 +20,8 @@ namespace UKControllerPlugin::Prenote { // All the prenotes const std::shared_ptr prenotes; + + // Handles prenote events + const PrenoteMessageEventHandlerCollection& eventHandlers; }; } // namespace UKControllerPlugin::Prenote diff --git a/src/plugin/prenote/PrenoteDeletedPushEventHandler.cpp b/src/plugin/prenote/PrenoteDeletedPushEventHandler.cpp index 5dfb3040a..0856030a5 100644 --- a/src/plugin/prenote/PrenoteDeletedPushEventHandler.cpp +++ b/src/plugin/prenote/PrenoteDeletedPushEventHandler.cpp @@ -1,12 +1,14 @@ #include "PrenoteDeletedPushEventHandler.h" #include "PrenoteMessage.h" #include "PrenoteMessageCollection.h" +#include "PrenoteMessageEventHandlerCollection.h" using UKControllerPlugin::Push::PushEventSubscription; namespace UKControllerPlugin::Prenote { - PrenoteDeletedPushEventHandler::PrenoteDeletedPushEventHandler(std::shared_ptr prenotes) - : prenotes(std::move(prenotes)) + PrenoteDeletedPushEventHandler::PrenoteDeletedPushEventHandler( + std::shared_ptr prenotes, const PrenoteMessageEventHandlerCollection& eventHandlers) + : prenotes(std::move(prenotes)), eventHandlers(eventHandlers) { } @@ -19,7 +21,8 @@ namespace UKControllerPlugin::Prenote { } int prenoteId = messageData.at("id").get(); - this->prenotes->Remove(prenoteId); + eventHandlers.MessageCancelled(*prenotes->GetById(prenoteId)); + prenotes->Remove(prenoteId); LogInfo("Deleted prenote id " + std::to_string(prenoteId)); } diff --git a/src/plugin/prenote/PrenoteDeletedPushEventHandler.h b/src/plugin/prenote/PrenoteDeletedPushEventHandler.h index e88cef701..3e3f4714c 100644 --- a/src/plugin/prenote/PrenoteDeletedPushEventHandler.h +++ b/src/plugin/prenote/PrenoteDeletedPushEventHandler.h @@ -3,11 +3,14 @@ namespace UKControllerPlugin::Prenote { class PrenoteMessageCollection; + class PrenoteMessageEventHandlerCollection; class PrenoteDeletedPushEventHandler : public UKControllerPlugin::Push::PushEventProcessorInterface { public: - explicit PrenoteDeletedPushEventHandler(std::shared_ptr prenotes); + explicit PrenoteDeletedPushEventHandler( + std::shared_ptr prenotes, + const PrenoteMessageEventHandlerCollection& eventHandlers); void ProcessPushEvent(const Push::PushEvent& message) override; [[nodiscard]] auto GetPushEventSubscriptions() const -> std::set override; @@ -17,5 +20,8 @@ namespace UKControllerPlugin::Prenote { // All the prenotes const std::shared_ptr prenotes; + + // Handles prenote events + const PrenoteMessageEventHandlerCollection& eventHandlers; }; } // namespace UKControllerPlugin::Prenote diff --git a/src/plugin/prenote/PrenoteModule.cpp b/src/plugin/prenote/PrenoteModule.cpp index c3258dfce..a4f854a80 100644 --- a/src/plugin/prenote/PrenoteModule.cpp +++ b/src/plugin/prenote/PrenoteModule.cpp @@ -100,10 +100,10 @@ namespace UKControllerPlugin::Prenote { // Push event processors persistence.pushEventProcessors->AddProcessor(std::make_shared( persistence.prenotes, *persistence.controllerPositions, *persistence.prenoteMessageHandlers)); - persistence.pushEventProcessors->AddProcessor( - std::make_shared(persistence.prenotes)); - persistence.pushEventProcessors->AddProcessor( - std::make_shared(persistence.prenotes)); + persistence.pushEventProcessors->AddProcessor(std::make_shared( + persistence.prenotes, *persistence.prenoteMessageHandlers)); + persistence.pushEventProcessors->AddProcessor(std::make_shared( + persistence.prenotes, *persistence.prenoteMessageHandlers)); persistence.timedHandler->RegisterEvent( std::make_shared(persistence.prenotes), MESSAGE_TIMEOUT_CHECK_INTERVAL); diff --git a/test/plugin/prenote/PrenoteAcknowledgedPushEventHandlerTest.cpp b/test/plugin/prenote/PrenoteAcknowledgedPushEventHandlerTest.cpp index ce971cd03..efeafa41d 100644 --- a/test/plugin/prenote/PrenoteAcknowledgedPushEventHandlerTest.cpp +++ b/test/plugin/prenote/PrenoteAcknowledgedPushEventHandlerTest.cpp @@ -2,6 +2,7 @@ #include "prenote/PrenoteAcknowledgedPushEventHandler.h" #include "prenote/PrenoteMessage.h" #include "prenote/PrenoteMessageCollection.h" +#include "prenote/PrenoteMessageEventHandlerCollection.h" #include "push/PushEvent.h" #include "push/PushEventSubscription.h" @@ -9,6 +10,7 @@ using UKControllerPlugin::Controller::ControllerPosition; using UKControllerPlugin::Prenote::PrenoteAcknowledgedPushEventHandler; using UKControllerPlugin::Prenote::PrenoteMessage; using UKControllerPlugin::Prenote::PrenoteMessageCollection; +using UKControllerPlugin::Prenote::PrenoteMessageEventHandlerCollection; using UKControllerPlugin::Push::PushEvent; using UKControllerPlugin::Push::PushEventSubscription; @@ -17,7 +19,7 @@ namespace UKControllerPluginTest::Prenote { { public: PrenoteAcknowledgedPushEventHandlerTest() - : messages(std::make_shared()), handler(messages) + : messages(std::make_shared()), handler(messages, eventHandlers) { sendingPosition = std::make_shared( 1, "EGKK_TWR", 124.225, std::vector{"EGKK"}, true, false); @@ -32,6 +34,8 @@ namespace UKControllerPluginTest::Prenote { sendingPosition, receivingPosition, std::chrono::system_clock::now())); + mockHandler = std::make_shared>(); + eventHandlers.AddHandler(mockHandler); } /* @@ -51,9 +55,11 @@ namespace UKControllerPluginTest::Prenote { return {"prenote-message.received", "test", eventData, eventData.dump()}; }; + std::shared_ptr> mockHandler; std::shared_ptr sendingPosition; std::shared_ptr receivingPosition; std::shared_ptr messages; + PrenoteMessageEventHandlerCollection eventHandlers; PrenoteAcknowledgedPushEventHandler handler; }; @@ -67,24 +73,28 @@ namespace UKControllerPluginTest::Prenote { TEST_F(PrenoteAcknowledgedPushEventHandlerTest, ItAcknowledgesPrenoteFromMessage) { + EXPECT_CALL(*mockHandler, MessageAcknowledged(testing::_)).Times(1); this->handler.ProcessPushEvent(MakePushEvent()); EXPECT_TRUE(this->messages->GetById(1)->IsAcknowledged()); } TEST_F(PrenoteAcknowledgedPushEventHandlerTest, ItHandlesMissingIdFromMessage) { + EXPECT_CALL(*mockHandler, MessageAcknowledged(testing::_)).Times(0); this->handler.ProcessPushEvent(MakePushEvent(nlohmann::json::object(), "id")); EXPECT_FALSE(this->messages->GetById(1)->IsAcknowledged()); } TEST_F(PrenoteAcknowledgedPushEventHandlerTest, ItHandlesIdNotIntegerFromMessage) { + EXPECT_CALL(*mockHandler, MessageAcknowledged(testing::_)).Times(0); this->handler.ProcessPushEvent(MakePushEvent(nlohmann::json::object({{"id", "abc"}}))); EXPECT_FALSE(this->messages->GetById(1)->IsAcknowledged()); } TEST_F(PrenoteAcknowledgedPushEventHandlerTest, ItHandlesPrenoteNotFoundToAcknowledge) { + EXPECT_CALL(*mockHandler, MessageAcknowledged(testing::_)).Times(0); this->messages->Remove(1); EXPECT_NO_THROW(this->handler.ProcessPushEvent(MakePushEvent())); } diff --git a/test/plugin/prenote/PrenoteDeletedPushEventHandlerTest.cpp b/test/plugin/prenote/PrenoteDeletedPushEventHandlerTest.cpp index 83d314b10..aadf93c6c 100644 --- a/test/plugin/prenote/PrenoteDeletedPushEventHandlerTest.cpp +++ b/test/plugin/prenote/PrenoteDeletedPushEventHandlerTest.cpp @@ -2,11 +2,13 @@ #include "prenote/PrenoteDeletedPushEventHandler.h" #include "prenote/PrenoteMessage.h" #include "prenote/PrenoteMessageCollection.h" +#include "prenote/PrenoteMessageEventHandlerCollection.h" using UKControllerPlugin::Controller::ControllerPosition; using UKControllerPlugin::Prenote::PrenoteDeletedPushEventHandler; using UKControllerPlugin::Prenote::PrenoteMessage; using UKControllerPlugin::Prenote::PrenoteMessageCollection; +using UKControllerPlugin::Prenote::PrenoteMessageEventHandlerCollection; using UKControllerPlugin::Push::PushEvent; using UKControllerPlugin::Push::PushEventSubscription; @@ -15,7 +17,7 @@ namespace UKControllerPluginTest::Prenote { { public: PrenoteDeletedPushEventHandlerHandlerTest() - : messages(std::make_shared()), handler(messages) + : messages(std::make_shared()), handler(messages, eventHandlers) { sendingPosition = std::make_shared( 1, "EGKK_TWR", 124.225, std::vector{"EGKK"}, true, false); @@ -30,6 +32,8 @@ namespace UKControllerPluginTest::Prenote { sendingPosition, receivingPosition, std::chrono::system_clock::now())); + mockHandler = std::make_shared>(); + eventHandlers.AddHandler(mockHandler); } /* @@ -49,9 +53,11 @@ namespace UKControllerPluginTest::Prenote { return {"prenote-message.received", "test", eventData, eventData.dump()}; }; + std::shared_ptr> mockHandler; std::shared_ptr sendingPosition; std::shared_ptr receivingPosition; std::shared_ptr messages; + PrenoteMessageEventHandlerCollection eventHandlers; PrenoteDeletedPushEventHandler handler; }; @@ -64,24 +70,28 @@ namespace UKControllerPluginTest::Prenote { TEST_F(PrenoteDeletedPushEventHandlerHandlerTest, ItRemovesPrenoteFromMessage) { + EXPECT_CALL(*mockHandler, MessageCancelled(testing::_)).Times(1); this->handler.ProcessPushEvent(MakePushEvent()); EXPECT_EQ(nullptr, this->messages->GetById(1)); } TEST_F(PrenoteDeletedPushEventHandlerHandlerTest, ItHandlesMissingIdFromMessage) { + EXPECT_CALL(*mockHandler, MessageCancelled(testing::_)).Times(0); this->handler.ProcessPushEvent(MakePushEvent(nlohmann::json::object(), "id")); EXPECT_NE(nullptr, this->messages->GetById(1)); } TEST_F(PrenoteDeletedPushEventHandlerHandlerTest, ItHandlesIdNotIntegerFromMessage) { + EXPECT_CALL(*mockHandler, MessageCancelled(testing::_)).Times(0); this->handler.ProcessPushEvent(MakePushEvent(nlohmann::json::object({{"id", "abc"}}))); EXPECT_NE(nullptr, this->messages->GetById(1)); } TEST_F(PrenoteDeletedPushEventHandlerHandlerTest, ItHandlesPrenoteNotFoundToRemove) { + EXPECT_CALL(*mockHandler, MessageCancelled(testing::_)).Times(0); this->messages->Remove(1); EXPECT_NO_THROW(this->handler.ProcessPushEvent(MakePushEvent())); }