1 #define SQUID_UNIT_TEST 1
4 #include <cppunit/TestAssert.h>
6 #include "AsyncEngine.h"
11 #include "testEventLoop.h"
13 CPPUNIT_TEST_SUITE_REGISTRATION( testEventLoop
);
15 /* init legacy static-initialized modules */
18 testEventLoop::setUp()
25 * Test creating a EventLoop
28 testEventLoop::testCreate()
33 #if POLISHED_MAIN_LOOP
36 * Running the loop once is useful for integration with other loops, such as
37 * migrating to it in incrementally.
39 * This test works by having a custom dispatcher and engine which record how
40 * many times they are called.
43 class RecordDispatcher
: public CompletionDispatcher
48 RecordDispatcher(): calls(0) {}
52 /* claim we dispatched calls to be useful for the testStopOnIdle test.
58 #endif /* POLISHED_MAIN_LOOP */
60 class RecordingEngine
: public AsyncEngine
67 RecordingEngine(int aTimeout
=0): calls(0), lasttimeout(0), return_timeout(aTimeout
) {}
69 virtual int checkEvents(int timeout
) {
71 lasttimeout
= timeout
;
72 return return_timeout
;
76 #if POLISHED_MAIN_LOOP
79 testEventLoop::testRunOnce()
82 RecordDispatcher dispatcher
;
83 theLoop
.registerDispatcher(&dispatcher
);
84 RecordingEngine engine
;
85 theLoop
.registerEngine(&engine
);
87 CPPUNIT_ASSERT_EQUAL(1, dispatcher
.calls
);
88 CPPUNIT_ASSERT_EQUAL(1, engine
.calls
);
92 * completion dispatchers registered with the event loop are invoked by the
95 * This test works by having a customer dispatcher which shuts the loop down
96 * once its been invoked twice.
98 * It also tests that loop.run() and loop.stop() work, because if they dont
99 * work, this test will either hang, or fail.
102 class ShutdownDispatcher
: public CompletionDispatcher
108 ShutdownDispatcher(EventLoop
& theLoop
):theLoop(theLoop
), calls(0) {}
119 testEventLoop::testRegisterDispatcher()
122 ShutdownDispatcher
testDispatcher(theLoop
);
123 theLoop
.registerDispatcher(&testDispatcher
);
125 /* we should get two calls because the test dispatched returns true from
126 * dispatch(), and calls stop on the second call.
128 CPPUNIT_ASSERT_EQUAL(2, testDispatcher
.calls
);
131 /* test that a registered async engine is invoked on each loop run
132 * we do this with an intstrumented async engine.
135 testEventLoop::testRegisterEngine()
138 ShutdownDispatcher
testDispatcher(theLoop
);
139 theLoop
.registerDispatcher(&testDispatcher
);
140 RecordingEngine testEngine
;
141 theLoop
.registerEngine(&testEngine
);
143 CPPUNIT_ASSERT_EQUAL(2, testEngine
.calls
);
146 /* each AsyncEngine needs to be given a timeout. We want one engine in each
147 * loop to be given the timeout value - and the rest to have a timeout of 0.
148 * The last registered engine should be given this timeout, which will mean
149 * that we dont block in the loop until the last engine. This will allow for
150 * dynamic introduction and removal of engines, as long as the last engine
151 * is one which can do a os call rather than busy waiting.
153 * So - we want the timeout hints returned from the earlier engines to be
154 * tracked, and the lowest non-negative value given to the last engine.
157 testEventLoop::testEngineTimeout()
160 RecordingEngine
engineOne(5);
161 RecordingEngine engineTwo
;
162 theLoop
.registerEngine(&engineOne
);
163 theLoop
.registerEngine(&engineTwo
);
165 CPPUNIT_ASSERT_EQUAL(0, engineOne
.lasttimeout
);
166 CPPUNIT_ASSERT_EQUAL(5, engineTwo
.lasttimeout
);
169 /* An event loop with all idle engines, and nothing dispatched in a run should
170 * automatically quit. The runOnce call should return True when the loop is
171 * entirely idle to make it easy for people running the loop by hand.
174 testEventLoop::testStopOnIdle()
177 /* trivial case - no dispatchers or engines, should quit immediately */
178 CPPUNIT_ASSERT_EQUAL(true, theLoop
.runOnce());
180 /* add a dispatcher with nothing to dispatch - use an EventDispatcher as its
181 * sufficient and handy
183 EventDispatcher dispatcher
;
184 theLoop
.registerDispatcher(&dispatcher
);
185 CPPUNIT_ASSERT_EQUAL(true, theLoop
.runOnce());
187 /* add an engine which is idle.
189 RecordingEngine
engine(AsyncEngine::EVENT_IDLE
);
190 theLoop
.registerEngine(&engine
);
191 CPPUNIT_ASSERT_EQUAL(true, theLoop
.runOnce());
192 CPPUNIT_ASSERT_EQUAL(1, engine
.calls
);
194 CPPUNIT_ASSERT_EQUAL(2, engine
.calls
);
195 /* add an engine which is suffering errors. This should result in 10
196 * loops until the loop stops - because thats the error retry amount
198 RecordingEngine
failing_engine(AsyncEngine::EVENT_ERROR
);
199 theLoop
.registerEngine(&failing_engine
);
200 CPPUNIT_ASSERT_EQUAL(false, theLoop
.runOnce());
201 CPPUNIT_ASSERT_EQUAL(1, failing_engine
.calls
);
203 /* run resets the error count ... */
204 CPPUNIT_ASSERT_EQUAL(11, failing_engine
.calls
);
206 /* an engine that asks for a timeout should not be detected as idle:
207 * use runOnce which should return false
209 theLoop
= EventLoop();
210 RecordingEngine
non_idle_engine(1000);
211 theLoop
.registerEngine(&non_idle_engine
);
212 CPPUNIT_ASSERT_EQUAL(false, theLoop
.runOnce());
215 #endif /* POLISHED_MAIN_LOOP */
217 /* An event loop has a time service which is like an async engine but never
218 * generates events and there can only be one such service.
221 class StubTime
: public TimeEngine
225 StubTime() : calls(0) {}
234 testEventLoop::testSetTimeService()
238 /* the loop will not error without a time service */
240 /* we can set the time service */
241 theLoop
.setTimeService(&myTime
);
242 /* it invokes our tick() call */
244 CPPUNIT_ASSERT_EQUAL(1, myTime
.calls
);
245 /* it invokes our tick() call again */
247 CPPUNIT_ASSERT_EQUAL(2, myTime
.calls
);
250 /* one async engine is the primary engine - the engine that is allowed to block.
251 * this defaults to the last added one, but can be explicitly nominated
254 testEventLoop::testSetPrimaryEngine()
257 RecordingEngine
first_engine(10);
258 RecordingEngine
second_engine(10);
259 /* one engine - gets a timeout */
260 theLoop
.registerEngine(&first_engine
);
262 CPPUNIT_ASSERT_EQUAL(EVENT_LOOP_TIMEOUT
, first_engine
.lasttimeout
);
263 /* two engines - the second gets the timeout */
264 theLoop
.registerEngine(&second_engine
);
266 CPPUNIT_ASSERT_EQUAL(0, first_engine
.lasttimeout
);
267 CPPUNIT_ASSERT_EQUAL(10, second_engine
.lasttimeout
);
268 /* set the first engine to be primary explicitly and now gets the timeout */
269 theLoop
.setPrimaryEngine(&first_engine
);
271 CPPUNIT_ASSERT_EQUAL(10, first_engine
.lasttimeout
);
272 CPPUNIT_ASSERT_EQUAL(0, second_engine
.lasttimeout
);