2 * Copyright (C) 1996-2014 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"
19 CPPUNIT_TEST_SUITE_REGISTRATION( testEvent
);
21 /* init legacy static-initialized modules */
31 * Test creating a Scheduler
34 testEvent::testCreate()
36 EventScheduler scheduler
= EventScheduler();
39 /* Helper for tests - an event which records the number of calls it received. */
42 CalledEvent() : calls(0) {}
44 static void Handler(void *data
) {
45 static_cast<CalledEvent
*>(data
)->calls
++;
51 /* submit two callbacks, and cancel one, then dispatch and only the other should run.
54 testEvent::testCancel()
56 EventScheduler scheduler
;
58 CalledEvent event_to_cancel
;
59 scheduler
.schedule("test event", CalledEvent::Handler
, &event
, 0, 0, false);
60 scheduler
.schedule("test event2", CalledEvent::Handler
, &event_to_cancel
, 0, 0, false);
61 scheduler
.cancel(CalledEvent::Handler
, &event_to_cancel
);
62 scheduler
.checkEvents(0);
63 AsyncCallQueue::Instance().fire();
64 CPPUNIT_ASSERT_EQUAL(1, event
.calls
);
65 CPPUNIT_ASSERT_EQUAL(0, event_to_cancel
.calls
);
68 /* submit two callbacks, and then dump the queue.
73 EventScheduler scheduler
;
76 CapturingStoreEntry
* anEntry
= new CapturingStoreEntry();
77 String expect
= "Last event to run: last event\n"
79 "Operation \tNext Execution \tWeight\tCallback Valid?\n"
80 "test event \t0.000 sec\t 0\t N/A\n"
81 "test event2 \t0.000 sec\t 0\t N/A\n";
83 scheduler
.schedule("last event", CalledEvent::Handler
, &event
, 0, 0, false);
85 /* schedule and dispatch to set the last run event */
86 scheduler
.checkEvents(0);
87 AsyncCallQueue::Instance().fire();
88 scheduler
.schedule("test event", CalledEvent::Handler
, &event
, 0, 0, false);
89 scheduler
.schedule("test event2", CalledEvent::Handler
, &event2
, 0, 0, false);
90 scheduler
.dump(anEntry
);
92 /* loop over the strings, showing exactly where they differ (if at all) */
93 printf("Actual Text:\n");
94 /* TODO: these should really be just [] lookups, but String doesn't have those here yet. */
95 for ( unsigned int i
= 0; i
< anEntry
->_appended_text
.size(); ++i
) {
96 CPPUNIT_ASSERT( expect
[i
] );
97 CPPUNIT_ASSERT( anEntry
->_appended_text
[i
] );
99 /* slight hack to make special chars visible */
100 switch (anEntry
->_appended_text
[i
]) {
105 printf("%c", anEntry
->_appended_text
[i
] );
107 /* make this an int comparison, so that we can see the ASCII code at failure */
108 CPPUNIT_ASSERT_EQUAL( (int)(expect
[i
]), (int)anEntry
->_appended_text
[i
] );
111 CPPUNIT_ASSERT_EQUAL( expect
, anEntry
->_appended_text
);
117 /* submit two callbacks, and find the right one.
120 testEvent::testFind()
122 EventScheduler scheduler
;
124 CalledEvent event_to_find
;
125 scheduler
.schedule("test event", CalledEvent::Handler
, &event
, 0, 0, false);
126 scheduler
.schedule("test event2", CalledEvent::Handler
, &event_to_find
, 0, 0, false);
127 CPPUNIT_ASSERT_EQUAL(true, scheduler
.find(CalledEvent::Handler
, &event_to_find
));
130 /* do a trivial test of invoking callbacks */
132 testEvent::testCheckEvents()
134 EventScheduler scheduler
;
136 /* with no events, its an idle engine */
137 CPPUNIT_ASSERT_EQUAL(int(AsyncEngine::EVENT_IDLE
), scheduler
.checkEvents(0));
138 /* event running now gets will get sent to the dispatcher and the
139 * engine becomes idle.
141 scheduler
.schedule("test event", CalledEvent::Handler
, &event
, 0, 0, false);
142 CPPUNIT_ASSERT_EQUAL(int(AsyncEngine::EVENT_IDLE
), scheduler
.checkEvents(0));
143 AsyncCallQueue::Instance().fire();
144 /* event running later results in a delay of the time till it runs */
145 scheduler
.schedule("test event", CalledEvent::Handler
, &event
, 2, 0, false);
146 CPPUNIT_ASSERT_EQUAL(2000, scheduler
.checkEvents(0));
147 AsyncCallQueue::Instance().fire();
148 CPPUNIT_ASSERT_EQUAL(1, event
.calls
);
151 /* for convenience we have a singleton scheduler */
153 testEvent::testSingleton()
155 EventScheduler
*scheduler
= dynamic_cast<EventScheduler
*>(EventScheduler::GetInstance());
156 CPPUNIT_ASSERT(NULL
!= scheduler
);