1
1
#include < chrono>
2
+ #include < string>
3
+
4
+ #include " envoy/event/dispatcher.h"
2
5
3
6
#include " source/extensions/tracers/datadog/event_scheduler.h"
4
7
5
8
#include " test/mocks/event/mocks.h"
6
9
#include " test/mocks/thread_local/mocks.h"
7
10
8
- #include " datadog/json.hpp"
9
11
#include " gmock/gmock.h"
10
12
#include " gtest/gtest.h"
13
+ #include " nlohmann/json.hpp"
14
+
15
+ using testing::NiceMock;
16
+ using testing::StrictMock;
11
17
12
18
namespace Envoy {
13
19
namespace Extensions {
14
20
namespace Tracers {
15
21
namespace Datadog {
16
22
namespace {
17
23
18
- TEST (DatadogEventSchedulerTest, ScheduleRecurringEventCallsCreatesATimer) {
19
- testing::NiceMock<ThreadLocal::MockInstance> thread_local_storage_;
24
+ // Test class to verify Datadog EventScheduler behaviors
25
+ class DatadogEventSchedulerTest : public testing ::Test {
26
+ public:
27
+ DatadogEventSchedulerTest ()
28
+ : thread_local_storage_(std::make_shared<NiceMock<ThreadLocal::MockInstance>>()),
29
+ scheduler_ (thread_local_storage_->dispatcher_) {}
30
+
31
+ protected:
32
+ std::shared_ptr<NiceMock<ThreadLocal::MockInstance>> thread_local_storage_;
33
+ EventScheduler scheduler_;
34
+ };
35
+
36
+ // Verify that the config() method produces a valid string that can be parsed as JSON
37
+ TEST_F (DatadogEventSchedulerTest, ConfigJson) {
38
+ const std::string config = scheduler_.config ();
39
+
40
+ // Verify it's not empty
41
+ EXPECT_FALSE (config.empty ());
42
+
43
+ // Parse the config string to verify it's valid JSON
44
+ EXPECT_NO_THROW ({
45
+ auto json = nlohmann::json::parse (config);
46
+ EXPECT_TRUE (json.is_object ());
47
+ EXPECT_EQ (" Envoy::Extensions::Tracers::Datadog::EventScheduler" , json[" type" ]);
48
+ });
49
+ }
50
+
51
+ // Test config_json returns expected content
52
+ TEST_F (DatadogEventSchedulerTest, ConfigJsonMethod) {
53
+ nlohmann::json config = scheduler_.config_json ();
54
+ EXPECT_EQ (" Envoy::Extensions::Tracers::Datadog::EventScheduler" , config[" type" ]);
55
+ }
20
56
21
- EventScheduler scheduler{thread_local_storage_.dispatcher_ };
57
+ // Test that the scheduler creates a timer when scheduling an event
58
+ TEST_F (DatadogEventSchedulerTest, ScheduleRecurringEventCallsCreatesATimer) {
22
59
testing::MockFunction<void ()> callback;
23
60
// The interval is arbitrary in these tests; we just have to be able to
24
61
// compare it to what was passed to the mocks.
25
62
// The only requirement is that it be divisible by milliseconds, because
26
63
// that's what `Timer::enableTimer` accepts.
27
64
const std::chrono::milliseconds interval (2000 );
28
65
29
- EXPECT_CALL (thread_local_storage_. dispatcher_ , createTimer_ (testing:: _));
66
+ EXPECT_CALL (thread_local_storage_-> dispatcher_ , createTimer_ (_));
30
67
31
- scheduler .schedule_recurring_event (interval, callback.AsStdFunction ());
68
+ scheduler_ .schedule_recurring_event (interval, callback.AsStdFunction ());
32
69
}
33
70
34
71
// This could be tested above, but introducing an `Event::MockTimer` disrupts
35
72
// our ability to track calls to `MockDispatcher::createTimer_`. So, two
36
73
// separate tests.
37
- TEST (DatadogEventSchedulerTest, ScheduleRecurringEventEnablesATimer) {
38
- testing::NiceMock<ThreadLocal::MockInstance> thread_local_storage_;
39
- auto * const timer = new testing::NiceMock<Event::MockTimer>(&thread_local_storage_.dispatcher_ );
40
-
41
- EventScheduler scheduler{thread_local_storage_.dispatcher_ };
74
+ TEST_F (DatadogEventSchedulerTest, ScheduleRecurringEventEnablesATimer) {
75
+ auto * const timer = new NiceMock<Event::MockTimer>(&thread_local_storage_->dispatcher_ );
42
76
testing::MockFunction<void ()> callback;
43
77
const std::chrono::milliseconds interval (2000 );
44
78
45
- EXPECT_CALL (*timer, enableTimer (interval, testing:: _));
79
+ EXPECT_CALL (*timer, enableTimer (interval, _));
46
80
47
- scheduler .schedule_recurring_event (interval, callback.AsStdFunction ());
81
+ scheduler_ .schedule_recurring_event (interval, callback.AsStdFunction ());
48
82
}
49
83
50
- TEST (DatadogEventSchedulerTest, TriggeredTimerInvokesCallbackAndReschedulesItself) {
51
- testing::NiceMock<ThreadLocal::MockInstance> thread_local_storage_;
52
- auto * const timer = new testing::NiceMock<Event::MockTimer>(&thread_local_storage_.dispatcher_ );
53
-
54
- EventScheduler scheduler{thread_local_storage_.dispatcher_ };
84
+ // Test that the timer's callback properly invokes the user-supplied callback and reschedules
85
+ TEST_F (DatadogEventSchedulerTest, TriggeredTimerInvokesCallbackAndReschedulesItself) {
86
+ auto * const timer = new NiceMock<Event::MockTimer>(&thread_local_storage_->dispatcher_ );
55
87
testing::MockFunction<void ()> callback;
56
88
const std::chrono::milliseconds interval (2000 );
57
89
58
90
// Once for the initial round, and then again when the callback is invoked.
59
- EXPECT_CALL (*timer, enableTimer (interval, testing:: _)).Times (2 );
91
+ EXPECT_CALL (*timer, enableTimer (interval, _)).Times (2 );
60
92
// The user-supplied callback is called once when the timer triggers.
61
93
EXPECT_CALL (callback, Call ());
62
94
63
- scheduler .schedule_recurring_event (interval, callback.AsStdFunction ());
95
+ scheduler_ .schedule_recurring_event (interval, callback.AsStdFunction ());
64
96
timer->invokeCallback ();
65
97
}
66
98
67
- TEST (DatadogEventSchedulerTest, CancellationFunctionCallsDisableTimerOnce) {
68
- testing::NiceMock<ThreadLocal::MockInstance> thread_local_storage_;
69
- auto * const timer = new testing::NiceMock<Event::MockTimer>(&thread_local_storage_.dispatcher_ );
70
-
71
- EventScheduler scheduler{thread_local_storage_.dispatcher_ };
99
+ // Test that the cancellation function properly disables the timer
100
+ TEST_F (DatadogEventSchedulerTest, CancellationFunctionCallsDisableTimerOnce) {
101
+ auto * const timer = new NiceMock<Event::MockTimer>(&thread_local_storage_->dispatcher_ );
72
102
testing::MockFunction<void ()> callback;
73
103
const std::chrono::milliseconds interval (2000 );
74
104
75
105
EXPECT_CALL (*timer, disableTimer ());
76
106
77
- const auto cancel = scheduler .schedule_recurring_event (interval, callback.AsStdFunction ());
107
+ const auto cancel = scheduler_ .schedule_recurring_event (interval, callback.AsStdFunction ());
78
108
cancel ();
79
109
cancel (); // idempotent
80
110
cancel (); // idempotent
@@ -83,13 +113,6 @@ TEST(DatadogEventSchedulerTest, CancellationFunctionCallsDisableTimerOnce) {
83
113
cancel (); // idempotent
84
114
}
85
115
86
- TEST (DatadogEventSchedulerTest, ConfigJson) {
87
- testing::NiceMock<ThreadLocal::MockInstance> thread_local_storage_;
88
- EventScheduler scheduler{thread_local_storage_.dispatcher_ };
89
- nlohmann::json config = scheduler.config_json ();
90
- EXPECT_EQ (" Envoy::Extensions::Tracers::Datadog::EventScheduler" , config[" type" ]);
91
- }
92
-
93
116
} // namespace
94
117
} // namespace Datadog
95
118
} // namespace Tracers
0 commit comments