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