1 #define SQUID_UNIT_TEST 1
4 #include <cppunit/TestAssert.h>
6 #include "testEventLoop.h"
11 CPPUNIT_TEST_SUITE_REGISTRATION( testEventLoop
);
13 /* init legacy static-initialized modules */
16 testEventLoop::setUp()
23 * Test creating a EventLoop
26 testEventLoop::testCreate()
31 #if POLISHED_MAIN_LOOP
34 * Running the loop once is useful for integration with other loops, such as
35 * migrating to it in incrementally.
37 * This test works by having a custom dispatcher and engine which record how
38 * many times they are called.
41 class RecordDispatcher
: public CompletionDispatcher
46 RecordDispatcher(): calls(0) {}
50 /* claim we dispatched calls to be useful for the testStopOnIdle test.
56 #endif /* POLISHED_MAIN_LOOP */
58 class RecordingEngine
: public AsyncEngine
65 RecordingEngine(int return_timeout
=0): calls(0), lasttimeout(0),
66 return_timeout(return_timeout
) {}
68 virtual int checkEvents(int timeout
) {
70 lasttimeout
= timeout
;
71 return return_timeout
;
75 #if POLISHED_MAIN_LOOP
78 testEventLoop::testRunOnce()
81 RecordDispatcher dispatcher
;
82 theLoop
.registerDispatcher(&dispatcher
);
83 RecordingEngine engine
;
84 theLoop
.registerEngine(&engine
);
86 CPPUNIT_ASSERT_EQUAL(1, dispatcher
.calls
);
87 CPPUNIT_ASSERT_EQUAL(1, engine
.calls
);
91 * completion dispatchers registered with the event loop are invoked by the
94 * This test works by having a customer dispatcher which shuts the loop down
95 * once its been invoked twice.
97 * It also tests that loop.run() and loop.stop() work, because if they dont
98 * work, this test will either hang, or fail.
101 class ShutdownDispatcher
: public CompletionDispatcher
107 ShutdownDispatcher(EventLoop
& theLoop
):theLoop(theLoop
), calls(0) {}
118 testEventLoop::testRegisterDispatcher()
121 ShutdownDispatcher
testDispatcher(theLoop
);
122 theLoop
.registerDispatcher(&testDispatcher
);
124 /* we should get two calls because the test dispatched returns true from
125 * dispatch(), and calls stop on the second call.
127 CPPUNIT_ASSERT_EQUAL(2, testDispatcher
.calls
);
130 /* test that a registered async engine is invoked on each loop run
131 * we do this with an intstrumented async engine.
134 testEventLoop::testRegisterEngine()
137 ShutdownDispatcher
testDispatcher(theLoop
);
138 theLoop
.registerDispatcher(&testDispatcher
);
139 RecordingEngine testEngine
;
140 theLoop
.registerEngine(&testEngine
);
142 CPPUNIT_ASSERT_EQUAL(2, testEngine
.calls
);
145 /* each AsyncEngine needs to be given a timeout. We want one engine in each
146 * loop to be given the timeout value - and the rest to have a timeout of 0.
147 * The last registered engine should be given this timeout, which will mean
148 * that we dont block in the loop until the last engine. This will allow for
149 * dynamic introduction and removal of engines, as long as the last engine
150 * is one which can do a os call rather than busy waiting.
152 * So - we want the timeout hints returned from the earlier engines to be
153 * tracked, and the lowest non-negative value given to the last engine.
156 testEventLoop::testEngineTimeout()
159 RecordingEngine
engineOne(5);
160 RecordingEngine engineTwo
;
161 theLoop
.registerEngine(&engineOne
);
162 theLoop
.registerEngine(&engineTwo
);
164 CPPUNIT_ASSERT_EQUAL(0, engineOne
.lasttimeout
);
165 CPPUNIT_ASSERT_EQUAL(5, engineTwo
.lasttimeout
);
168 /* An event loop with all idle engines, and nothing dispatched in a run should
169 * automatically quit. The runOnce call should return True when the loop is
170 * entirely idle to make it easy for people running the loop by hand.
173 testEventLoop::testStopOnIdle()
176 /* trivial case - no dispatchers or engines, should quit immediately */
177 CPPUNIT_ASSERT_EQUAL(true, theLoop
.runOnce());
179 /* add a dispatcher with nothing to dispatch - use an EventDispatcher as its
180 * sufficient and handy
182 EventDispatcher dispatcher
;
183 theLoop
.registerDispatcher(&dispatcher
);
184 CPPUNIT_ASSERT_EQUAL(true, theLoop
.runOnce());
186 /* add an engine which is idle.
188 RecordingEngine
engine(AsyncEngine::EVENT_IDLE
);
189 theLoop
.registerEngine(&engine
);
190 CPPUNIT_ASSERT_EQUAL(true, theLoop
.runOnce());
191 CPPUNIT_ASSERT_EQUAL(1, engine
.calls
);
193 CPPUNIT_ASSERT_EQUAL(2, engine
.calls
);
194 /* add an engine which is suffering errors. This should result in 10
195 * loops until the loop stops - because thats the error retry amount
197 RecordingEngine
failing_engine(AsyncEngine::EVENT_ERROR
);
198 theLoop
.registerEngine(&failing_engine
);
199 CPPUNIT_ASSERT_EQUAL(false, theLoop
.runOnce());
200 CPPUNIT_ASSERT_EQUAL(1, failing_engine
.calls
);
202 /* run resets the error count ... */
203 CPPUNIT_ASSERT_EQUAL(11, failing_engine
.calls
);
205 /* an engine that asks for a timeout should not be detected as idle:
206 * use runOnce which should return false
208 theLoop
= EventLoop();
209 RecordingEngine
non_idle_engine(1000);
210 theLoop
.registerEngine(&non_idle_engine
);
211 CPPUNIT_ASSERT_EQUAL(false, theLoop
.runOnce());
214 #endif /* POLISHED_MAIN_LOOP */
216 /* An event loop has a time service which is like an async engine but never
217 * generates events and there can only be one such service.
220 class StubTime
: public TimeEngine
224 StubTime() : calls(0) {}
233 testEventLoop::testSetTimeService()
237 /* the loop will not error without a time service */
239 /* we can set the time service */
240 theLoop
.setTimeService(&myTime
);
241 /* it invokes our tick() call */
243 CPPUNIT_ASSERT_EQUAL(1, myTime
.calls
);
244 /* it invokes our tick() call again */
246 CPPUNIT_ASSERT_EQUAL(2, myTime
.calls
);
249 /* one async engine is the primary engine - the engine that is allowed to block.
250 * this defaults to the last added one, but can be explicitly nominated
253 testEventLoop::testSetPrimaryEngine()
256 RecordingEngine
first_engine(10);
257 RecordingEngine
second_engine(10);
258 /* one engine - gets a timeout */
259 theLoop
.registerEngine(&first_engine
);
261 CPPUNIT_ASSERT_EQUAL(EVENT_LOOP_TIMEOUT
, first_engine
.lasttimeout
);
262 /* two engines - the second gets the timeout */
263 theLoop
.registerEngine(&second_engine
);
265 CPPUNIT_ASSERT_EQUAL(0, first_engine
.lasttimeout
);
266 CPPUNIT_ASSERT_EQUAL(10, second_engine
.lasttimeout
);
267 /* set the first engine to be primary explicitly and now gets the timeout */
268 theLoop
.setPrimaryEngine(&first_engine
);
270 CPPUNIT_ASSERT_EQUAL(10, first_engine
.lasttimeout
);
271 CPPUNIT_ASSERT_EQUAL(0, second_engine
.lasttimeout
);