2 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
11 #include <cppunit/TestAssert.h>
13 #include "base/AsyncCallQueue.h"
14 #include "CapturingStoreEntry.h"
17 #include "testEvent.h"
18 #include "unitTestMain.h"
20 CPPUNIT_TEST_SUITE_REGISTRATION( testEvent
);
22 /* init legacy static-initialized modules */
32 * Test creating a Scheduler
35 testEvent::testCreate()
37 EventScheduler scheduler
= EventScheduler();
40 /* Helper for tests - an event which records the number of calls it received. */
43 CalledEvent() : calls(0) {}
45 static void Handler(void *data
) {
46 static_cast<CalledEvent
*>(data
)->calls
++;
52 /* submit two callbacks, and cancel one, then dispatch and only the other should run.
55 testEvent::testCancel()
57 EventScheduler scheduler
;
59 CalledEvent event_to_cancel
;
60 scheduler
.schedule("test event", CalledEvent::Handler
, &event
, 0, 0, false);
61 scheduler
.schedule("test event2", CalledEvent::Handler
, &event_to_cancel
, 0, 0, false);
62 scheduler
.cancel(CalledEvent::Handler
, &event_to_cancel
);
63 scheduler
.checkEvents(0);
64 AsyncCallQueue::Instance().fire();
65 CPPUNIT_ASSERT_EQUAL(1, event
.calls
);
66 CPPUNIT_ASSERT_EQUAL(0, event_to_cancel
.calls
);
69 /* submit two callbacks, and then dump the queue.
74 EventScheduler scheduler
;
77 CapturingStoreEntry
* anEntry
= new CapturingStoreEntry();
78 String expect
= "Last event to run: last event\n"
80 "Operation \tNext Execution \tWeight\tCallback Valid?\n"
81 "test event \t0.000 sec\t 0\t N/A\n"
82 "test event2 \t0.000 sec\t 0\t N/A\n";
84 scheduler
.schedule("last event", CalledEvent::Handler
, &event
, 0, 0, false);
86 /* schedule and dispatch to set the last run event */
87 scheduler
.checkEvents(0);
88 AsyncCallQueue::Instance().fire();
89 scheduler
.schedule("test event", CalledEvent::Handler
, &event
, 0, 0, false);
90 scheduler
.schedule("test event2", CalledEvent::Handler
, &event2
, 0, 0, false);
91 scheduler
.dump(anEntry
);
93 /* loop over the strings, showing exactly where they differ (if at all) */
94 printf("Actual Text:\n");
95 /* TODO: these should really be just [] lookups, but String doesn't have those here yet. */
96 for ( unsigned int i
= 0; i
< anEntry
->_appended_text
.size(); ++i
) {
97 CPPUNIT_ASSERT( expect
[i
] );
98 CPPUNIT_ASSERT( anEntry
->_appended_text
[i
] );
100 /* slight hack to make special chars visible */
101 switch (anEntry
->_appended_text
[i
]) {
106 printf("%c", anEntry
->_appended_text
[i
] );
108 /* make this an int comparison, so that we can see the ASCII code at failure */
109 CPPUNIT_ASSERT_EQUAL( (int)(expect
[i
]), (int)anEntry
->_appended_text
[i
] );
112 CPPUNIT_ASSERT_EQUAL( expect
, anEntry
->_appended_text
);
118 /* submit two callbacks, and find the right one.
121 testEvent::testFind()
123 EventScheduler scheduler
;
125 CalledEvent event_to_find
;
126 scheduler
.schedule("test event", CalledEvent::Handler
, &event
, 0, 0, false);
127 scheduler
.schedule("test event2", CalledEvent::Handler
, &event_to_find
, 0, 0, false);
128 CPPUNIT_ASSERT_EQUAL(true, scheduler
.find(CalledEvent::Handler
, &event_to_find
));
131 /* do a trivial test of invoking callbacks */
133 testEvent::testCheckEvents()
135 EventScheduler scheduler
;
137 /* with no events, its an idle engine */
138 CPPUNIT_ASSERT_EQUAL(int(AsyncEngine::EVENT_IDLE
), scheduler
.checkEvents(0));
139 /* event running now gets will get sent to the dispatcher and the
140 * engine becomes idle.
142 scheduler
.schedule("test event", CalledEvent::Handler
, &event
, 0, 0, false);
143 CPPUNIT_ASSERT_EQUAL(int(AsyncEngine::EVENT_IDLE
), scheduler
.checkEvents(0));
144 AsyncCallQueue::Instance().fire();
145 /* event running later results in a delay of the time till it runs */
146 scheduler
.schedule("test event", CalledEvent::Handler
, &event
, 2, 0, false);
147 CPPUNIT_ASSERT_EQUAL(2000, scheduler
.checkEvents(0));
148 AsyncCallQueue::Instance().fire();
149 CPPUNIT_ASSERT_EQUAL(1, event
.calls
);
152 /* for convenience we have a singleton scheduler */
154 testEvent::testSingleton()
156 EventScheduler
*scheduler
= dynamic_cast<EventScheduler
*>(EventScheduler::GetInstance());
157 CPPUNIT_ASSERT(NULL
!= scheduler
);