@@ -187,6 +187,39 @@ private final class LocalScheduler : C.FiberScheduler
187187private LocalScheduler scheduler;
188188
189189
190+ /* ******************************************************************************
191+
192+ Provide eventloop-like functionalities
193+
194+ Since nodes instantiated via this modules are Vibe.d server,
195+ they expect the ability to run an asynchronous task ,
196+ usually provided by `vibe.core.core : runTask`.
197+
198+ In order for them to properly work, we need to integrate them to our event
199+ loop by providing the ability to spawn a task, and wait on some condition,
200+ optionally with a timeout.
201+
202+ The following functions do that.
203+ Note that those facilities are not available from the main thread,
204+ while is supposed to do tests and doesn't have a scheduler.
205+
206+ *******************************************************************************/
207+
208+ public void runTask (scope void delegate () dg)
209+ {
210+ assert (scheduler ! is null , " Cannot call this function from the main thread" );
211+ scheduler.spawn(dg);
212+ }
213+
214+ // / Ditto
215+ public void sleep (Duration timeout)
216+ {
217+ assert (scheduler ! is null , " Cannot call this function from the main thread" );
218+ scope cond = scheduler.new FiberCondition();
219+ cond.wait(timeout);
220+ }
221+
222+
190223/* ******************************************************************************
191224
192225 A reference to an alread-instantiated node
@@ -656,3 +689,54 @@ unittest
656689 // 7 level of re-entrancy
657690 assert (210 == nodes[0 ].call(20 , 0 ));
658691}
692+
693+
694+ // / Nodes can start tasks
695+ unittest
696+ {
697+ static import core.thread ;
698+ import core.time ;
699+
700+ static interface API
701+ {
702+ public void start ();
703+ public ulong getCounter ();
704+ }
705+
706+ static class Node : API
707+ {
708+ public override void start ()
709+ {
710+ runTask(&this .task);
711+ }
712+
713+ public override ulong getCounter ()
714+ {
715+ scope (exit) this .counter = 0 ;
716+ return this .counter;
717+ }
718+
719+ private void task ()
720+ {
721+ while (true )
722+ {
723+ this .counter++ ;
724+ sleep(50. msecs);
725+ }
726+ }
727+
728+ private ulong counter;
729+ }
730+
731+ import std.format ;
732+ auto node = RemoteAPI! API .spawn! Node();
733+ assert (node.getCounter() == 0 );
734+ node.start();
735+ assert (node.getCounter() == 1 );
736+ assert (node.getCounter() == 0 );
737+ core.thread.Thread.sleep (1. seconds);
738+ // It should be 19 but some machines are very slow
739+ // (e.g. Travis Mac testers) so be safe
740+ assert (node.getCounter() >= 9 );
741+ assert (node.getCounter() == 0 );
742+ }
0 commit comments