Skip to content

Commit ef62c3d

Browse files
committed
test: test for data downloader event registration
1 parent 4ec4563 commit ef62c3d

4 files changed

Lines changed: 46 additions & 3 deletions

File tree

include/ECFMP/event/Event.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,7 @@ namespace ECFMP::Event {
4747
* Compares events.
4848
*/
4949
[[nodiscard]] auto operator==(const class Event& event) const -> bool;
50+
51+
// TODO: Event participants
5052
};
5153
}// namespace ECFMP::Event

include/ECFMP/eventbus/EventBus.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
namespace ECFMP::EventBus {
99

10-
template<typename EventType>
11-
class EventStreamFactory;
12-
1310
class EventBus
1411
{
1512
public:
@@ -48,6 +45,17 @@ namespace ECFMP::EventBus {
4845
GetStream<EventType>().SubscribeOnce(listener, filter);
4946
};
5047

48+
/**
49+
* Checks whether a specified type of listener is registered for a specified type of event.
50+
*
51+
* Can be used for test assertions.
52+
*/
53+
template<typename ListenerType, typename EventType>
54+
[[nodiscard]] auto HasListenerOfType() -> bool
55+
{
56+
return GetStream<EventType>().template HasListenerOfType<ListenerType>();
57+
}
58+
5159
private:
5260
template<typename EventType>
5361
auto GetStream() -> InternalEventStream<EventType>&

src/eventbus/InternalEventStream.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,26 @@ namespace ECFMP::EventBus {
3434
this->subscriptions.end()
3535
);
3636
};
37+
38+
/**
39+
* A method for testing only, shouldn't normally be used.
40+
*/
41+
template<typename ListenerType>
42+
[[nodiscard]] auto HasListenerOfType() -> bool
43+
{
44+
auto guard = std::lock_guard(this->mutex);
45+
return std::any_of(
46+
this->subscriptions.begin(), this->subscriptions.end(),
47+
[](const EventSubscription<EventType>& subscription) {
48+
try {
49+
static_cast<void>(dynamic_cast<const ListenerType&>(*subscription.listener));
50+
return true;
51+
}
52+
catch (std::bad_cast&) {
53+
return false;
54+
}
55+
}
56+
);
57+
}
3758
};
3859
}// namespace ECFMP::EventBus

test/plugin/SdkFactoryTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "ECFMP/SdkFactory.h"
22
#include "ECFMP/EventListeners.h"
3+
#include "ECFMP/eventbus/EventBus.h"
4+
#include "api/ApiDataDownloadedEvent.h"
5+
#include "api/ApiDataParser.h"
36
#include "mock/MockHttpClient.h"
47
#include "mock/MockLogger.h"
58

@@ -64,4 +67,13 @@ namespace ECFMPTest::Plugin {
6467
ECFMP::Plugin::SdkConfigurationException
6568
);
6669
}
70+
71+
TEST_F(SdkFactoryTest, ItRegistersApiDataParserForApiDataEvents)
72+
{
73+
const auto instance = ECFMP::Plugin::SdkFactory::Build().WithHttpClient(std::move(http)).Instance();
74+
auto hasListener =
75+
instance->EventBus().HasListenerOfType<ECFMP::Api::ApiDataParser, ECFMP::Api::ApiDataDownloadedEvent>();
76+
EXPECT_TRUE(hasListener);
77+
instance->Destroy();
78+
}
6779
}// namespace ECFMPTest::Plugin

0 commit comments

Comments
 (0)