1 import core.atomic; 2 // write a test of runActor 3 4 static 5 final synchronized class SharedCounter 6 { 7 this() {} 8 9 private int count; 10 11 void inc() { atomicOp!"+="(count, 1); } 12 int value() { return count; } 13 } 14 15 16 static struct TestActor 17 { 18 import std.exception; 19 20 this(shared(SharedCounter) c1, shared(SharedCounter) c2) { this.c1 = c1; this.c2 = c2; } 21 22 @ThreadEvent void inc() { c1.inc(); enforce(false); } 23 bool isEnd() { return c1.value > 2; } 24 void onResurrection(Throwable) { c2.inc(); } 25 26 shared(SharedCounter) c1, c2; 27 } 28 29 auto scnt1 = new shared SharedCounter(), 30 scnt2 = new shared SharedCounter(); 31 auto con = runPhoenixActor!TestActor(scnt1, scnt2); 32 33 con.inc(); 34 Thread.sleep(dur!"msecs"(100)); 35 assert(scnt1.value == 1); 36 assert(scnt2.value == 1); 37 38 con.inc(); 39 Thread.sleep(dur!"msecs"(100)); 40 assert(scnt1.value == 2); 41 assert(scnt2.value == 2); 42 43 con.inc(); 44 Thread.sleep(dur!"msecs"(100)); 45 assert(scnt1.value == 3); 46 assert(scnt2.value == 3); 47 48 con.inc(); 49 Thread.sleep(dur!"msecs"(100)); 50 assert(scnt1.value == 3); 51 assert(scnt2.value > 0);
runActorと同様に,アクターAを別スレッドで起動しますが,Aで例外が飛んだ場合,ただちに復帰します.