]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testEvent.cc
SourceLayout: shuffle memory pool allocators to mem/libmem.la
[thirdparty/squid.git] / src / tests / testEvent.cc
1 /*
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9 #include "squid.h"
10
11 #include <cppunit/TestAssert.h>
12
13 #include "base/AsyncCallQueue.h"
14 #include "CapturingStoreEntry.h"
15 #include "event.h"
16 #include "stat.h"
17 #include "testEvent.h"
18
19 CPPUNIT_TEST_SUITE_REGISTRATION( testEvent );
20
21 /* init legacy static-initialized modules */
22
23 void
24 testEvent::setUp()
25 {
26 Mem::Init();
27 statInit();
28 }
29
30 /*
31 * Test creating a Scheduler
32 */
33 void
34 testEvent::testCreate()
35 {
36 EventScheduler scheduler = EventScheduler();
37 }
38
39 /* Helper for tests - an event which records the number of calls it received. */
40
41 struct CalledEvent {
42 CalledEvent() : calls(0) {}
43
44 static void Handler(void *data) {
45 static_cast<CalledEvent *>(data)->calls++;
46 }
47
48 int calls;
49 };
50
51 /* submit two callbacks, and cancel one, then dispatch and only the other should run.
52 */
53 void
54 testEvent::testCancel()
55 {
56 EventScheduler scheduler;
57 CalledEvent event;
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);
66 }
67
68 /* submit two callbacks, and then dump the queue.
69 */
70 void
71 testEvent::testDump()
72 {
73 EventScheduler scheduler;
74 CalledEvent event;
75 CalledEvent event2;
76 CapturingStoreEntry * anEntry = new CapturingStoreEntry();
77 String expect = "Last event to run: last event\n"
78 "\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";
82
83 scheduler.schedule("last event", CalledEvent::Handler, &event, 0, 0, false);
84
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);
91
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] );
98
99 /* slight hack to make special chars visible */
100 switch (anEntry->_appended_text[i]) {
101 case '\t':
102 printf("\\t");
103 break;
104 default:
105 printf("%c", anEntry->_appended_text[i] );
106 }
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] );
109 }
110 printf("\n");
111 CPPUNIT_ASSERT_EQUAL( expect, anEntry->_appended_text);
112
113 /* cleanup */
114 delete anEntry;
115 }
116
117 /* submit two callbacks, and find the right one.
118 */
119 void
120 testEvent::testFind()
121 {
122 EventScheduler scheduler;
123 CalledEvent event;
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));
128 }
129
130 /* do a trivial test of invoking callbacks */
131 void
132 testEvent::testCheckEvents()
133 {
134 EventScheduler scheduler;
135 CalledEvent event;
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.
140 */
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);
149 }
150
151 /* for convenience we have a singleton scheduler */
152 void
153 testEvent::testSingleton()
154 {
155 EventScheduler *scheduler = dynamic_cast<EventScheduler *>(EventScheduler::GetInstance());
156 CPPUNIT_ASSERT(NULL != scheduler);
157 }