1 #define SQUID_UNIT_TEST 1
4 #include <cppunit/TestAssert.h>
6 #include "testEventLoop.h"
7 #include "AsyncEngine.h"
10 #include "SquidTime.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 return_timeout
=0): calls(0), lasttimeout(0),
68 return_timeout(return_timeout
) {}
70 virtual int checkEvents(int timeout
) {
72 lasttimeout
= timeout
;
73 return return_timeout
;
77 #if POLISHED_MAIN_LOOP
80 testEventLoop::testRunOnce()
83 RecordDispatcher dispatcher
;
84 theLoop
.registerDispatcher(&dispatcher
);
85 RecordingEngine engine
;
86 theLoop
.registerEngine(&engine
);
88 CPPUNIT_ASSERT_EQUAL(1, dispatcher
.calls
);
89 CPPUNIT_ASSERT_EQUAL(1, engine
.calls
);
93 * completion dispatchers registered with the event loop are invoked by the
96 * This test works by having a customer dispatcher which shuts the loop down
97 * once its been invoked twice.
99 * It also tests that loop.run() and loop.stop() work, because if they dont
100 * work, this test will either hang, or fail.
103 class ShutdownDispatcher
: public CompletionDispatcher
109 ShutdownDispatcher(EventLoop
& theLoop
):theLoop(theLoop
), calls(0) {}
120 testEventLoop::testRegisterDispatcher()
123 ShutdownDispatcher
testDispatcher(theLoop
);
124 theLoop
.registerDispatcher(&testDispatcher
);
126 /* we should get two calls because the test dispatched returns true from
127 * dispatch(), and calls stop on the second call.
129 CPPUNIT_ASSERT_EQUAL(2, testDispatcher
.calls
);
132 /* test that a registered async engine is invoked on each loop run
133 * we do this with an intstrumented async engine.
136 testEventLoop::testRegisterEngine()
139 ShutdownDispatcher
testDispatcher(theLoop
);
140 theLoop
.registerDispatcher(&testDispatcher
);
141 RecordingEngine testEngine
;
142 theLoop
.registerEngine(&testEngine
);
144 CPPUNIT_ASSERT_EQUAL(2, testEngine
.calls
);
147 /* each AsyncEngine needs to be given a timeout. We want one engine in each
148 * loop to be given the timeout value - and the rest to have a timeout of 0.
149 * The last registered engine should be given this timeout, which will mean
150 * that we dont block in the loop until the last engine. This will allow for
151 * dynamic introduction and removal of engines, as long as the last engine
152 * is one which can do a os call rather than busy waiting.
154 * So - we want the timeout hints returned from the earlier engines to be
155 * tracked, and the lowest non-negative value given to the last engine.
158 testEventLoop::testEngineTimeout()
161 RecordingEngine
engineOne(5);
162 RecordingEngine engineTwo
;
163 theLoop
.registerEngine(&engineOne
);
164 theLoop
.registerEngine(&engineTwo
);
166 CPPUNIT_ASSERT_EQUAL(0, engineOne
.lasttimeout
);
167 CPPUNIT_ASSERT_EQUAL(5, engineTwo
.lasttimeout
);
170 /* An event loop with all idle engines, and nothing dispatched in a run should
171 * automatically quit. The runOnce call should return True when the loop is
172 * entirely idle to make it easy for people running the loop by hand.
175 testEventLoop::testStopOnIdle()
178 /* trivial case - no dispatchers or engines, should quit immediately */
179 CPPUNIT_ASSERT_EQUAL(true, theLoop
.runOnce());
181 /* add a dispatcher with nothing to dispatch - use an EventDispatcher as its
182 * sufficient and handy
184 EventDispatcher dispatcher
;
185 theLoop
.registerDispatcher(&dispatcher
);
186 CPPUNIT_ASSERT_EQUAL(true, theLoop
.runOnce());
188 /* add an engine which is idle.
190 RecordingEngine
engine(AsyncEngine::EVENT_IDLE
);
191 theLoop
.registerEngine(&engine
);
192 CPPUNIT_ASSERT_EQUAL(true, theLoop
.runOnce());
193 CPPUNIT_ASSERT_EQUAL(1, engine
.calls
);
195 CPPUNIT_ASSERT_EQUAL(2, engine
.calls
);
196 /* add an engine which is suffering errors. This should result in 10
197 * loops until the loop stops - because thats the error retry amount
199 RecordingEngine
failing_engine(AsyncEngine::EVENT_ERROR
);
200 theLoop
.registerEngine(&failing_engine
);
201 CPPUNIT_ASSERT_EQUAL(false, theLoop
.runOnce());
202 CPPUNIT_ASSERT_EQUAL(1, failing_engine
.calls
);
204 /* run resets the error count ... */
205 CPPUNIT_ASSERT_EQUAL(11, failing_engine
.calls
);
207 /* an engine that asks for a timeout should not be detected as idle:
208 * use runOnce which should return false
210 theLoop
= EventLoop();
211 RecordingEngine
non_idle_engine(1000);
212 theLoop
.registerEngine(&non_idle_engine
);
213 CPPUNIT_ASSERT_EQUAL(false, theLoop
.runOnce());
216 #endif /* POLISHED_MAIN_LOOP */
218 /* An event loop has a time service which is like an async engine but never
219 * generates events and there can only be one such service.
222 class StubTime
: public TimeEngine
226 StubTime() : calls(0) {}
235 testEventLoop::testSetTimeService()
239 /* the loop will not error without a time service */
241 /* we can set the time service */
242 theLoop
.setTimeService(&myTime
);
243 /* it invokes our tick() call */
245 CPPUNIT_ASSERT_EQUAL(1, myTime
.calls
);
246 /* it invokes our tick() call again */
248 CPPUNIT_ASSERT_EQUAL(2, myTime
.calls
);
251 /* one async engine is the primary engine - the engine that is allowed to block.
252 * this defaults to the last added one, but can be explicitly nominated
255 testEventLoop::testSetPrimaryEngine()
258 RecordingEngine
first_engine(10);
259 RecordingEngine
second_engine(10);
260 /* one engine - gets a timeout */
261 theLoop
.registerEngine(&first_engine
);
263 CPPUNIT_ASSERT_EQUAL(EVENT_LOOP_TIMEOUT
, first_engine
.lasttimeout
);
264 /* two engines - the second gets the timeout */
265 theLoop
.registerEngine(&second_engine
);
267 CPPUNIT_ASSERT_EQUAL(0, first_engine
.lasttimeout
);
268 CPPUNIT_ASSERT_EQUAL(10, second_engine
.lasttimeout
);
269 /* set the first engine to be primary explicitly and now gets the timeout */
270 theLoop
.setPrimaryEngine(&first_engine
);
272 CPPUNIT_ASSERT_EQUAL(10, first_engine
.lasttimeout
);
273 CPPUNIT_ASSERT_EQUAL(0, second_engine
.lasttimeout
);