Skip to content
Navigation Menu
{{ message }}
forked from actor-framework/actor-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_spawn.cpp
More file actions
935 lines (852 loc) · 28 KB
/
Copy pathtest_spawn.cpp
File metadata and controls
935 lines (852 loc) · 28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
#include <stack>
#include <chrono>
#include <iostream>
#include <functional>
#include "test.hpp"
#include "ping_pong.hpp"
#include "cppa/cppa.hpp"
#include "cppa/detail/cs_thread.hpp"
#include "cppa/detail/yield_interface.hpp"
using namespace std;
using namespace cppa;
namespace {
class event_testee : public sb_actor<event_testee> {
friend class sb_actor<event_testee>;
behavior wait4string;
behavior wait4float;
behavior wait4int;
behavior& init_state = wait4int;
public:
event_testee() {
wait4string = (
on<string>() >> [=] {
become(wait4int);
},
on<atom("get_state")>() >> [=] {
return "wait4string";
}
);
wait4float = (
on<float>() >> [=] {
become(wait4string);
},
on<atom("get_state")>() >> [=] {
return "wait4float";
}
);
wait4int = (
on<int>() >> [=] {
become(wait4float);
},
on<atom("get_state")>() >> [=] {
return "wait4int";
}
);
}
};
// quits after 5 timeouts
actor spawn_event_testee2(actor parent) {
struct impl : event_based_actor {
actor parent;
impl(actor parent_actor) : parent(parent_actor) { }
behavior wait4timeout(int remaining) {
CPPA_LOG_TRACE(CPPA_ARG(remaining));
return {
after(chrono::milliseconds(1)) >> [=] {
CPPA_PRINT(CPPA_ARG(remaining));
if (remaining == 1) {
send(parent, atom("t2done"));
quit();
}
else become(wait4timeout(remaining - 1));
}
};
}
behavior make_behavior() override {
return wait4timeout(5);
}
};
return spawn<impl>(parent);
}
struct chopstick : public sb_actor<chopstick> {
behavior taken_by(actor whom) {
return (
on<atom("take")>() >> [=] {
return atom("busy");
},
on(atom("put"), whom) >> [=] {
become(available);
},
on(atom("break")) >> [=] {
quit();
}
);
}
behavior available;
behavior& init_state = available;
chopstick() {
available = (
on(atom("take"), arg_match) >> [=](actor whom) -> atom_value {
become(taken_by(whom));
return atom("taken");
},
on(atom("break")) >> [=] {
quit();
}
);
}
};
class testee_actor {
void wait4string(blocking_actor* self) {
bool string_received = false;
self->do_receive (
on<string>() >> [&] {
string_received = true;
},
on<atom("get_state")>() >> [&] {
return "wait4string";
}
)
.until(gref(string_received));
}
void wait4float(blocking_actor* self) {
bool float_received = false;
self->do_receive (
on<float>() >> [&] {
float_received = true;
},
on<atom("get_state")>() >> [&] {
return "wait4float";
}
)
.until(gref(float_received));
wait4string(self);
}
public:
void operator()(blocking_actor* self) {
self->receive_loop (
on<int>() >> [&] {
wait4float(self);
},
on<atom("get_state")>() >> [&] {
return "wait4int";
}
);
}
};
// self->receives one timeout and quits
void testee1(event_based_actor* self) {
CPPA_LOGF_TRACE("");
self->become(after(chrono::milliseconds(10)) >> [=] {
CPPA_LOGF_TRACE("");
self->unbecome();
});
}
template<class Testee>
string behavior_test(scoped_actor& self, actor et) {
string testee_name = detail::to_uniform_name(typeid(Testee));
CPPA_LOGF_TRACE(CPPA_TARG(et, to_string) << ", " << CPPA_ARG(testee_name));
string result;
self->send(et, 1);
self->send(et, 2);
self->send(et, 3);
self->send(et, .1f);
self->send(et, "hello " + testee_name);
self->send(et, .2f);
self->send(et, .3f);
self->send(et, "hello again " + testee_name);
self->send(et, "goodbye " + testee_name);
self->send(et, atom("get_state"));
self->receive (
on_arg_match >> [&](const string& str) {
result = str;
},
after(chrono::minutes(1)) >> [&]() {
CPPA_LOGF_ERROR(testee_name << " does not reply");
throw runtime_error(testee_name + " does not reply");
}
);
self->send_exit(et, exit_reason::user_shutdown);
self->await_all_other_actors_done();
return result;
}
class fixed_stack : public sb_actor<fixed_stack> {
friend class sb_actor<fixed_stack>;
size_t max_size = 10;
vector<int> data;
behavior full;
behavior filled;
behavior empty;
behavior& init_state = empty;
public:
fixed_stack(size_t max) : max_size(max) {
full = (
on(atom("push"), arg_match) >> [=](int) { /* discard */ },
on(atom("pop")) >> [=]() -> cow_tuple<atom_value, int> {
auto result = data.back();
data.pop_back();
become(filled);
return {atom("ok"), result};
}
);
filled = (
on(atom("push"), arg_match) >> [=](int what) {
data.push_back(what);
if (data.size() == max_size) become(full);
},
on(atom("pop")) >> [=]() -> cow_tuple<atom_value, int> {
auto result = data.back();
data.pop_back();
if (data.empty()) become(empty);
return {atom("ok"), result};
}
);
empty = (
on(atom("push"), arg_match) >> [=](int what) {
data.push_back(what);
become(filled);
},
on(atom("pop")) >> [=] {
return atom("failure");
}
);
}
};
behavior echo_actor(event_based_actor* self) {
return (
others() >> [=]() -> any_tuple {
self->quit(exit_reason::normal);
return self->last_dequeued();
}
);
}
struct simple_mirror : sb_actor<simple_mirror> {
behavior init_state;
simple_mirror() {
init_state = (
others() >> [=]() -> any_tuple {
return last_dequeued();
}
);
}
};
behavior high_priority_testee(event_based_actor* self) {
self->send(self, atom("b"));
self->send(message_priority::high, self, atom("a"));
// 'a' must be self->received before 'b'
return (
on(atom("b")) >> [=] {
CPPA_FAILURE("received 'b' before 'a'");
self->quit();
},
on(atom("a")) >> [=] {
CPPA_CHECKPOINT();
self->become (
on(atom("b")) >> [=] {
CPPA_CHECKPOINT();
self->quit();
},
others() >> CPPA_UNEXPECTED_MSG_CB(self)
);
},
others() >> CPPA_UNEXPECTED_MSG_CB(self)
);
}
struct high_priority_testee_class : event_based_actor {
behavior make_behavior() override {
return high_priority_testee(this);
}
};
struct master : event_based_actor {
behavior make_behavior() override {
return (
on(atom("done")) >> [=] {
quit(exit_reason::user_shutdown);
}
);
}
};
struct slave : event_based_actor {
slave(actor master_actor) : master{master_actor} { }
behavior make_behavior() override {
link_to(master);
return (
others() >> CPPA_UNEXPECTED_MSG_CB(this)
);
}
actor master;
};
void test_serial_reply() {
auto mirror_behavior = [=](event_based_actor* self) {
self->become(others() >> [=]() -> any_tuple {
CPPA_PRINT("return self->last_dequeued()");
return self->last_dequeued();
});
};
auto master = spawn([=](event_based_actor* self) {
cout << "ID of master: " << self->id() << endl;
// spawn 5 mirror actors
auto c0 = self->spawn<linked>(mirror_behavior);
auto c1 = self->spawn<linked>(mirror_behavior);
auto c2 = self->spawn<linked>(mirror_behavior);
auto c3 = self->spawn<linked>(mirror_behavior);
auto c4 = self->spawn<linked>(mirror_behavior);
self->become (
on(atom("hi there")) >> [=]() -> continue_helper {
CPPA_PRINT("received 'hi there'");
return self->sync_send(c0, atom("sub0")).then(
on(atom("sub0")) >> [=]() -> continue_helper {
CPPA_PRINT("received 'sub0'");
return self->sync_send(c1, atom("sub1")).then(
on(atom("sub1")) >> [=]() -> continue_helper {
CPPA_PRINT("received 'sub1'");
return self->sync_send(c2, atom("sub2")).then(
on(atom("sub2")) >> [=]() -> continue_helper {
CPPA_PRINT("received 'sub2'");
return self->sync_send(c3, atom("sub3")).then(
on(atom("sub3")) >> [=]() -> continue_helper {
CPPA_PRINT("received 'sub3'");
return self->sync_send(c4, atom("sub4")).then(
on(atom("sub4")) >> [=]() -> atom_value {
CPPA_PRINT("received 'sub4'");
return atom("hiho");
}
);
}
);
}
);
}
);
}
);
}
);
}
);
{ // lifetime scope of self
scoped_actor self;
cout << "ID of main: " << self->id() << endl;
self->sync_send(master, atom("hi there")).await(
on(atom("hiho")) >> [] {
CPPA_CHECKPOINT();
},
others() >> CPPA_UNEXPECTED_MSG_CB_REF(self)
);
self->send_exit(master, exit_reason::user_shutdown);
}
await_all_actors_done();
}
void test_or_else() {
scoped_actor self;
partial_function handle_a {
on("a") >> [] { return 1; }
};
partial_function handle_b {
on("b") >> [] { return 2; }
};
partial_function handle_c {
on("c") >> [] { return 3; }
};
auto run_testee([&](actor testee) {
self->sync_send(testee, "a").await([](int i) {
CPPA_CHECK_EQUAL(i, 1);
});
self->sync_send(testee, "b").await([](int i) {
CPPA_CHECK_EQUAL(i, 2);
});
self->sync_send(testee, "c").await([](int i) {
CPPA_CHECK_EQUAL(i, 3);
});
self->send_exit(testee, exit_reason::user_shutdown);
self->await_all_other_actors_done();
});
CPPA_PRINT("run_testee: handle_a.or_else(handle_b).or_else(handle_c)");
run_testee(
spawn([=] {
return handle_a.or_else(handle_b).or_else(handle_c);
})
);
CPPA_PRINT("run_testee: handle_a.or_else(handle_b), on(\"c\") ...");
run_testee(
spawn([=] {
return (
handle_a.or_else(handle_b),
on("c") >> [] { return 3; }
);
})
);
CPPA_PRINT("run_testee: on(\"a\") ..., handle_b.or_else(handle_c)");
run_testee(
spawn([=] {
return (
on("a") >> [] { return 1; },
handle_b.or_else(handle_c)
);
})
);
}
void test_continuation() {
auto mirror = spawn<simple_mirror>();
spawn([=](event_based_actor* self) {
self->sync_send(mirror, 42).then(
on(42) >> [] {
return "fourty-two";
}
).continue_with(
[=](const string& ref) {
CPPA_CHECK_EQUAL(ref, "fourty-two");
return 4.2f;
}
).continue_with(
[=](float f) {
CPPA_CHECK_EQUAL(f, 4.2f);
self->send_exit(mirror, exit_reason::user_shutdown);
self->quit();
}
);
});
await_all_actors_done();
}
void test_simple_reply_response() {
auto s = spawn([](event_based_actor* self) -> behavior {
return (
others() >> [=]() -> any_tuple {
CPPA_CHECK(self->last_dequeued() == make_any_tuple(atom("hello")));
self->quit();
return self->last_dequeued();
}
);
});
scoped_actor self;
self->send(s, atom("hello"));
self->receive(
others() >> [&] {
CPPA_CHECK(self->last_dequeued() == make_any_tuple(atom("hello")));
}
);
self->await_all_other_actors_done();
}
void test_spawn() {
test_simple_reply_response();
CPPA_CHECKPOINT();
test_serial_reply();
CPPA_CHECKPOINT();
test_or_else();
CPPA_CHECKPOINT();
test_continuation();
CPPA_CHECKPOINT();
scoped_actor self;
// check whether detached actors and scheduled actors interact w/o errors
auto m = spawn<master, detached>();
spawn<slave>(m);
spawn<slave>(m);
self->send(m, atom("done"));
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
CPPA_PRINT("test self->send()");
self->send(self, 1, 2, 3, true);
self->receive(on(1, 2, 3, true) >> [] { });
self->send_tuple(self, any_tuple{});
self->receive(on() >> [] { });
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
CPPA_PRINT("test self->receive with zero timeout");
self->receive (
others() >> CPPA_UNEXPECTED_MSG_CB_REF(self),
after(chrono::seconds(0)) >> [] { /* mailbox empty */ }
);
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
CPPA_PRINT("test mirror"); {
auto mirror = self->spawn<simple_mirror, monitored>();
self->send(mirror, "hello mirror");
self->receive (
on("hello mirror") >> CPPA_CHECKPOINT_CB(),
others() >> CPPA_UNEXPECTED_MSG_CB_REF(self)
);
self->send_exit(mirror, exit_reason::user_shutdown);
self->receive (
on_arg_match >> [&](const down_msg& dm) {
if (dm.reason == exit_reason::user_shutdown) {
CPPA_CHECKPOINT();
}
else { CPPA_UNEXPECTED_MSG_CB_REF(self); }
},
others() >> CPPA_UNEXPECTED_MSG_CB_REF(self)
);
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
}
CPPA_PRINT("test detached mirror"); {
auto mirror = self->spawn<simple_mirror, monitored+detached>();
self->send(mirror, "hello mirror");
self->receive (
on("hello mirror") >> CPPA_CHECKPOINT_CB(),
others() >> CPPA_UNEXPECTED_MSG_CB_REF(self)
);
self->send_exit(mirror, exit_reason::user_shutdown);
self->receive (
on_arg_match >> [&](const down_msg& dm) {
if (dm.reason == exit_reason::user_shutdown) {
CPPA_CHECKPOINT();
}
else { CPPA_UNEXPECTED_MSG(self); }
},
others() >> CPPA_UNEXPECTED_MSG_CB_REF(self)
);
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
}
CPPA_PRINT("test priority aware mirror"); {
auto mirror = self->spawn<simple_mirror, monitored+priority_aware>();
CPPA_CHECKPOINT();
self->send(mirror, "hello mirror");
self->receive (
on("hello mirror") >> CPPA_CHECKPOINT_CB(),
others() >> CPPA_UNEXPECTED_MSG_CB_REF(self)
);
self->send_exit(mirror, exit_reason::user_shutdown);
self->receive (
on_arg_match >> [&](const down_msg& dm) {
if (dm.reason == exit_reason::user_shutdown) {
CPPA_CHECKPOINT();
}
else { CPPA_UNEXPECTED_MSG(self); }
},
others() >> CPPA_UNEXPECTED_MSG_CB_REF(self)
);
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
}
CPPA_PRINT("test echo actor");
auto mecho = spawn(echo_actor);
self->send(mecho, "hello echo");
self->receive (
on("hello echo") >> [] { },
others() >> CPPA_UNEXPECTED_MSG_CB_REF(self)
);
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
CPPA_PRINT("test delayed_send()");
self->delayed_send(self, chrono::seconds(1), 1, 2, 3);
self->receive(on(1, 2, 3) >> [] { });
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
CPPA_PRINT("test timeout");
self->receive(after(chrono::seconds(1)) >> [] { });
CPPA_CHECKPOINT();
spawn(testee1);
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
spawn_event_testee2(self);
self->receive(on(atom("t2done")) >> CPPA_CHECKPOINT_CB());
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
auto cstk = spawn<chopstick>();
self->send(cstk, atom("take"), self);
self->receive (
on(atom("taken")) >> [&] {
self->send(cstk, atom("put"), self);
self->send(cstk, atom("break"));
},
others() >> CPPA_UNEXPECTED_MSG_CB_REF(self)
);
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
auto st = spawn<fixed_stack>(size_t{10});
// push 20 values
for (int i = 0; i < 20; ++i) self->send(st, atom("push"), i);
// pop 20 times
for (int i = 0; i < 20; ++i) self->send(st, atom("pop"));
// expect 10 failure messages
{
int i = 0;
self->receive_for(i, 10) (
on(atom("failure")) >> CPPA_CHECKPOINT_CB()
);
CPPA_CHECKPOINT();
}
// expect 10 {'ok', value} messages
{
vector<int> values;
int i = 0;
self->receive_for(i, 10) (
on(atom("ok"), arg_match) >> [&](int value) {
values.push_back(value);
}
);
vector<int> expected{9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
CPPA_CHECK_EQUAL(util::join(values, ","), util::join(expected, ","));
}
// terminate st
self->send_exit(st, exit_reason::user_shutdown);
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
auto sync_testee1 = spawn<blocking_api>([](blocking_actor* s) {
if (detail::cs_thread::is_disabled_feature) {
CPPA_LOGF_WARNING("compiled w/o context switching "
"(skip some tests)");
}
else {
CPPA_CHECKPOINT();
// scheduler should switch back immediately
detail::yield(detail::yield_state::ready);
CPPA_CHECKPOINT();
}
s->receive (
on(atom("get")) >> [] {
return make_cow_tuple(42, 2);
}
);
});
self->send(self, 0, 0);
auto handle = self->sync_send(sync_testee1, atom("get"));
// wait for some time (until sync response arrived in mailbox)
self->receive (after(chrono::milliseconds(50)) >> [] { });
// enqueue async messages (must be skipped by self->receive_response)
self->send(self, 42, 1);
// must skip sync message
self->receive (
on(42, arg_match) >> [&](int i) {
CPPA_CHECK_EQUAL(i, 1);
}
);
// must skip remaining async message
handle.await(
on_arg_match >> [&](int a, int b) {
CPPA_CHECK_EQUAL(a, 42);
CPPA_CHECK_EQUAL(b, 2);
},
others() >> CPPA_UNEXPECTED_MSG_CB_REF(self),
after(chrono::seconds(10)) >> CPPA_UNEXPECTED_TOUT_CB()
);
// dequeue remaining async. message
self->receive (on(0, 0) >> CPPA_CHECKPOINT_CB());
// make sure there's no other message in our mailbox
self->receive (
others() >> CPPA_UNEXPECTED_MSG_CB_REF(self),
after(chrono::seconds(0)) >> [] { }
);
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
CPPA_PRINT("test sync send");
CPPA_CHECKPOINT();
auto sync_testee = spawn<blocking_api>([](blocking_actor* s) {
s->receive (
on("hi", arg_match) >> [&](actor from) {
s->sync_send(from, "whassup?", s).await(
on_arg_match >> [&](const string& str) -> string {
CPPA_CHECK(s->last_sender() != nullptr);
CPPA_CHECK_EQUAL(str, "nothing");
return "goodbye!";
},
after(chrono::minutes(1)) >> [] {
cerr << "PANIC!!!!" << endl;
abort();
}
);
},
others() >> CPPA_UNEXPECTED_MSG_CB_REF(s)
);
});
self->monitor(sync_testee);
self->send(sync_testee, "hi", self);
self->receive (
on("whassup?", arg_match) >> [&](actor other) -> std::string {
CPPA_CHECKPOINT();
// this is NOT a reply, it's just an asynchronous message
self->send(other, "a lot!");
return "nothing";
}
);
self->receive (
on("goodbye!") >> CPPA_CHECKPOINT_CB(),
after(std::chrono::seconds(5)) >> CPPA_UNEXPECTED_TOUT_CB()
);
self->receive (
on_arg_match >> [&](const down_msg& dm) {
CPPA_CHECK_EQUAL(dm.reason, exit_reason::normal);
CPPA_CHECK_EQUAL(dm.source, sync_testee);
}
);
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
self->sync_send(sync_testee, "!?").await(
on<sync_exited_msg>() >> CPPA_CHECKPOINT_CB(),
others() >> CPPA_UNEXPECTED_MSG_CB_REF(self),
after(chrono::milliseconds(5)) >> CPPA_UNEXPECTED_TOUT_CB()
);
CPPA_CHECKPOINT();
auto inflater = [](event_based_actor* s, const string& name, actor buddy) {
CPPA_LOGF_TRACE(CPPA_ARG(s) << ", " << CPPA_ARG(name)
<< ", " << CPPA_TARG(buddy, to_string));
s->become(
on_arg_match >> [=](int n, const string& str) {
s->send(buddy, n * 2, str + " from " + name);
},
on(atom("done")) >> [=] {
s->quit();
}
);
};
auto joe = spawn(inflater, "Joe", self);
auto bob = spawn(inflater, "Bob", joe);
self->send(bob, 1, "hello actor");
self->receive (
on(4, "hello actor from Bob from Joe") >> CPPA_CHECKPOINT_CB(),
others() >> CPPA_UNEXPECTED_MSG_CB_REF(self)
);
// kill joe and bob
auto poison_pill = make_any_tuple(atom("done"));
anon_send_tuple(joe, poison_pill);
anon_send_tuple(bob, poison_pill);
self->await_all_other_actors_done();
function<actor (const string&, const actor&)> spawn_next;
// it's safe to capture spawn_next as reference here, because
// - it is guaranteeed to outlive kr34t0r by general scoping rules
// - the lambda is always executed in the current actor's thread
// but using spawn_next in a message handler could
// still cause undefined behavior!
auto kr34t0r = [&spawn_next](event_based_actor* s, const string& name, actor pal) {
if (name == "Joe" && !pal) {
pal = spawn_next("Bob", s);
}
s->become (
others() >> [=] {
// forward message and die
s->send_tuple(pal, s->last_dequeued());
s->quit();
}
);
};
spawn_next = [&kr34t0r](const string& name, const actor& pal) {
return spawn(kr34t0r, name, pal);
};
auto joe_the_second = spawn(kr34t0r, "Joe", invalid_actor);
self->send(joe_the_second, atom("done"));
self->await_all_other_actors_done();
auto f = [](const string& name) -> behavior {
return (
on(atom("get_name")) >> [name] {
return make_cow_tuple(atom("name"), name);
}
);
};
auto a1 = spawn(f, "alice");
auto a2 = spawn(f, "bob");
self->send(a1, atom("get_name"));
self->receive (
on(atom("name"), arg_match) >> [&](const string& name) {
CPPA_CHECK_EQUAL(name, "alice");
}
);
self->send(a2, atom("get_name"));
self->receive (
on(atom("name"), arg_match) >> [&](const string& name) {
CPPA_CHECK_EQUAL(name, "bob");
}
);
self->send_exit(a1, exit_reason::user_shutdown);
self->send_exit(a2, exit_reason::user_shutdown);
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
auto res1 = behavior_test<testee_actor>(self, spawn<blocking_api>(testee_actor{}));
CPPA_CHECK_EQUAL("wait4int", res1);
CPPA_CHECK_EQUAL(behavior_test<event_testee>(self, spawn<event_testee>()), "wait4int");
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
// create some actors linked to one single actor
// and kill them all through killing the link
auto legion = spawn([](event_based_actor* s) {
CPPA_PRINT("spawn 100 actors");
for (int i = 0; i < 100; ++i) {
s->spawn<event_testee, linked>();
}
s->become(others() >> CPPA_UNEXPECTED_MSG_CB(s));
});
self->send_exit(legion, exit_reason::user_shutdown);
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
self->trap_exit(true);
auto ping_actor = self->spawn<monitored+blocking_api>(ping, 10);
auto pong_actor = self->spawn<monitored+blocking_api>(pong, ping_actor);
self->link_to(pong_actor);
int i = 0;
int flags = 0;
self->delayed_send(self, chrono::seconds(1), atom("FooBar"));
// wait for DOWN and EXIT messages of pong
self->receive_for(i, 4) (
on_arg_match >> [&](const exit_msg& em) {
CPPA_CHECK_EQUAL(em.source, pong_actor);
CPPA_CHECK_EQUAL(em.reason, exit_reason::user_shutdown);
flags |= 0x01;
},
on_arg_match >> [&](const down_msg& dm) {
if (dm.source == pong_actor) {
flags |= 0x02;
CPPA_CHECK_EQUAL(dm.reason, exit_reason::user_shutdown);
}
else if (dm.source == ping_actor) {
flags |= 0x04;
CPPA_CHECK_EQUAL(dm.reason, exit_reason::normal);
}
},
on_arg_match >> [&](const atom_value& val) {
CPPA_CHECK(val == atom("FooBar"));
flags |= 0x08;
},
others() >> [&]() {
CPPA_FAILURE("unexpected message: " << to_string(self->last_dequeued()));
},
after(chrono::seconds(5)) >> [&]() {
CPPA_FAILURE("timeout in file " << __FILE__ << " in line " << __LINE__);
}
);
// wait for termination of all spawned actors
self->await_all_other_actors_done();
CPPA_CHECK_EQUAL(flags, 0x0F);
// verify pong messages
CPPA_CHECK_EQUAL(pongs(), 10);
CPPA_CHECKPOINT();
spawn<priority_aware>(high_priority_testee);
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
spawn<high_priority_testee_class, priority_aware>();
self->await_all_other_actors_done();
// test sending message to self via scoped_actor
self->send(self, atom("check"));
self->receive (
on(atom("check")) >> [] {
CPPA_CHECKPOINT();
}
);
CPPA_CHECKPOINT();
CPPA_PRINT("check whether timeouts trigger more than once");
auto counter = make_shared<int>(0);
auto sleeper = self->spawn<monitored>([=](event_based_actor* s) {
return after(std::chrono::milliseconds(1)) >> [=] {
CPPA_PRINT("received timeout #" << (*counter + 1));
if (++*counter > 3) {
CPPA_CHECKPOINT();
s->quit();
}
};
});
self->receive(
[&](const down_msg& msg) {
CPPA_CHECK_EQUAL(msg.source, sleeper);
CPPA_CHECK_EQUAL(msg.reason, exit_reason::normal);
}
);
CPPA_CHECKPOINT();
}
} // namespace <anonymous>
int main() {
CPPA_TEST(test_spawn);
test_spawn();
CPPA_CHECKPOINT();
shutdown();
CPPA_CHECKPOINT();
return CPPA_TEST_RESULT();
}
You can’t perform that action at this time.
