1 module mecca.reactor.tests.wekapp_70973;
2 
3 // Licensed under the Boost license. Full copyright information in the AUTHORS file
4 
5 version(unittest):
6 
7 import mecca.lib.exception;
8 import mecca.log;
9 import mecca.reactor;
10 import mecca.reactor.sync.event;
11 
12 unittest {
13     // Make sure fibers don't start after the reactor is already closing.
14 
15     FiberId fiberRan;
16     Event holder1, holder2, holder3;
17     uint counter;
18 
19     void externalFiberBody(Event* holder) {
20         scope(exit)
21                 counter++;
22 
23         holder.wait();
24         fiberRan = theReactor.currentFiberId;
25     }
26 
27     theReactor.setup();
28     scope(exit) theReactor.teardown();
29 
30     void testBody() {
31         auto fh = theReactor.spawnFiber(&externalFiberBody, &holder1);
32         INFO!"Spawn sleeping fiber %s"(fh.fiberId);
33 
34         fh = theReactor.spawnFiber(&externalFiberBody, &holder2);
35         INFO!"Spawn scheduled fiber %s"(fh.fiberId);
36 
37         theReactor.yield();
38 
39         holder3.set();
40         fh = theReactor.spawnFiber(&externalFiberBody, &holder3);
41         INFO!"Spawn starting fiber %s"(fh.fiberId);
42 
43         holder2.set();
44 
45         INFO!"Stopping reactor"();
46         theReactor.stop();
47     }
48 
49     theReactor.spawnFiber(&testBody);
50 
51     theReactor.start();
52 
53     // Only 2 fibers reach the scope(exit) and increment the counter
54     assertEQ(counter, 2, "Not all fibers quit");
55     ASSERT!"Fiber %s ran despite being after theReactor.stop"(!fiberRan.isValid, fiberRan);
56 }