-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtest-forked-actor.js
949 lines (763 loc) · 26.3 KB
/
test-forked-actor.js
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
936
937
938
939
940
941
942
943
944
945
946
947
948
949
/*
* Copyright (c) 2016-2017 Untu, Inc.
* This code is licensed under Eclipse Public License - v 1.0.
* The full license text can be found in LICENSE.txt file and
* on the Eclipse official site (https://www.eclipse.org/legal/epl-v10.html).
*/
'use strict';
/* eslint require-jsdoc: "off" */
var actors = require('../index');
var tu = require('../lib/utils/test.js');
var expect = require('chai').expect;
var http = require('http');
var net = require('net');
var request = require('supertest');
var isRunning = require('is-running');
var P = require('bluebird');
var _ = require('underscore');
var system;
var rootActor;
describe('ForkedActor', function() {
beforeEach(function() {
system = actors({
test: true,
additionalRequires: 'ts-node/register'
});
return system.rootActor().then(rootActor0 => {
rootActor = rootActor0;
});
});
afterEach(function() {
return system.destroy();
});
describe('sendAndReceive()', function() {
it('should throw error if handler threw error', function(done) {
rootActor
.createChild({
myMessage: () => {
throw new Error('Sorry!');
}
}, { mode: 'forked' })
.then(testActor => testActor.sendAndReceive('myMessage', 'Hi!'))
.then(() => {
done('Expected error!');
})
.catch(err => {
expect(err.message).to.be.equal('Sorry!');
})
.then(done)
.catch(done);
});
it('should fork a sub-process and perform message exchange', P.coroutine(function*() {
var behaviour = {
getPid: () => {
return process.pid;
}
};
var forkedChild = yield rootActor.createChild(behaviour, { mode: 'forked' });
var forkedPid = yield forkedChild.sendAndReceive('getPid');
expect(forkedPid).to.be.a.number;
expect(forkedPid).to.be.not.equal(process.pid);
// Check that child process is running.
expect(isRunning(forkedPid)).to.be.equal(true);
// Destroy forked actor.
yield forkedChild.destroy();
// From this point, any additional communication should not be possible.
var expectedErr = yield forkedChild.sendAndReceive('getPid').catch(err => err);
expect(expectedErr).to.be.instanceof(Error);
// The process should be stopped eventually.
yield tu.waitForCondition(() => !isRunning(forkedPid));
}));
it('should be able to import modules in forked process', P.coroutine(function*() {
// Use module import in behaviour.
var behaviour = {
sayHello: () => {
var P = require('bluebird');
return P.resolve('Hello!');
}
};
var forkedChild = yield rootActor.createChild(behaviour, { mode: 'forked' });
var result = yield forkedChild.sendAndReceive('sayHello');
expect(result).to.be.equal('Hello!');
}));
it('should be able to send a message to parent actor', P.coroutine(function*() {
var replyMsg = yield new P((resolve, reject) => {
var parentBehaviour = {
reply: function(msg) {
resolve(msg);
}
};
var childBehaviour = {
initialize: function(selfActor) {
this.parent = selfActor.getParent();
},
sayHello: function() {
return this.parent.sendAndReceive('reply', 'Hi!');
}
};
rootActor.createChild(parentBehaviour)
.then(parent => parent.createChild(childBehaviour, { mode: 'forked' }))
.then(child => child.sendAndReceive('sayHello'))
.catch(reject);
});
expect(replyMsg).to.be.equal('Hi!');
}));
it('should be able to forward messages to parent', P.coroutine(function*() {
var replyMsg = yield new P((resolve, reject) => {
var parentBehaviour = {
reply: function(msg) {
resolve(msg);
}
};
var childBehaviour = {
initialize: function(selfActor) {
selfActor.forwardToParent('reply');
return selfActor
.createChild({
initialize: function(selfActor) {
this.parent = selfActor.getParent();
},
sayHello: function() {
return this.parent.sendAndReceive('reply', 'Hi!');
}
})
.then(child => this.child = child);
},
sayHello: function() {
return this.child.sendAndReceive('sayHello');
}
};
rootActor.createChild(parentBehaviour)
.then(parent => parent.createChild(childBehaviour, { mode: 'forked' }))
.then(child => child.sendAndReceive('sayHello'))
.catch(reject);
});
expect(replyMsg).to.be.equal('Hi!');
}));
it('should support custom object marshallers in object form', P.coroutine(function*() {
class TestMessageClass {
constructor(pid) {
this.pid = pid;
}
getPid() {
return this.pid;
}
}
yield system.destroy();
system = actors({
test: true,
marshallers: [
{
type: TestMessageClass,
marshall: function(msg) {
return { pid: msg.pid };
},
unmarshall: function(msg) {
return {
getPid: () => msg.pid
};
}
}
]
});
var rootActor = yield system.rootActor();
var child = yield rootActor.createChild(
{
sayHello: (msg) => 'Hello ' + msg.getPid()
},
{ mode: 'forked' });
var result = yield child.sendAndReceive('sayHello', new TestMessageClass(process.pid));
expect(result).to.be.equal('Hello ' + process.pid);
}));
it('should support custom object marshallers in class form', P.coroutine(function*() {
class TestMessageClass {
static typeName() {
return 'TestMessageClass';
}
constructor(pid) {
this.pid = pid;
}
getPid() {
return this.pid;
}
}
class TestMessageClassMarshaller {
getType() {
return 'TestMessageClass';
}
marshall(msg) {
return { pid: msg.pid };
}
unmarshall(msg) {
return {
getPid: () => msg.pid
};
}
}
yield system.destroy();
system = actors({
test: true,
marshallers: [TestMessageClassMarshaller]
});
var rootActor = yield system.rootActor();
var child = yield rootActor.createChild(
{
sayHello: (msg) => 'Hello ' + msg.getPid()
},
{ mode: 'forked' });
var result = yield child.sendAndReceive('sayHello', new TestMessageClass(process.pid));
expect(result).to.be.equal('Hello ' + process.pid);
}));
it('should support custom module-based object marshallers in class form', P.coroutine(function*() {
class TestMessageClass {
static typeName() {
return 'TestMessageClass';
}
constructor(pid) {
this.pid = pid;
}
getPid() {
return this.pid;
}
}
yield system.destroy();
system = actors({
test: true,
marshallers: ['/test-resources/actors/test-message-class-marshaller']
});
var rootActor = yield system.rootActor();
var child = yield rootActor.createChild(
{
sayHello: (msg) => 'Hello ' + msg.getPid()
},
{ mode: 'forked' });
var result = yield child.sendAndReceive('sayHello', new TestMessageClass(process.pid));
expect(result).to.be.equal('Hello ' + process.pid);
}));
it('should support variable arguments', P.coroutine(function*() {
var child = yield rootActor.createChild({
hello: (from, to) => `Hello from ${from} to ${to}.`
}, { mode: 'forked' });
var result = yield child.sendAndReceive('hello', 'Bob', 'Alice');
expect(result).to.be.equal('Hello from Bob to Alice.');
}));
it('should be able to marshall each variable argument with a custom marshaller', P.coroutine(function*() {
class TestMessageClass {
static typeName() {
return 'TestMessageClass';
}
constructor(pid) {
this.pid = pid;
}
getPid() {
return this.pid;
}
}
yield system.destroy();
system = actors({
test: true,
marshallers: ['/test-resources/actors/test-message-class-marshaller']
});
var rootActor = yield system.rootActor();
var child = yield rootActor.createChild(
{
sayHello: (msg, from) => `Hello ${msg.getPid()} from ${from}`
},
{ mode: 'forked' });
var result = yield child.sendAndReceive('sayHello', new TestMessageClass(process.pid), 'Test');
expect(result).to.be.equal(`Hello ${process.pid} from Test`);
}));
it('should support http.Server object transfer', P.coroutine(function*() {
var server = http.createServer();
server.listen(8888);
var child = yield rootActor.createChild({
setServer: function(server) {
// Handle HTTP requests.
server.on('request', (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello!');
});
this.server = server;
},
destroy: function() {
return require('bluebird').fromCallback(cb => {
this.server.close(cb);
});
}
}, { mode: 'forked' });
yield child.sendAndReceive('setServer', server);
// Close server in this process to avoid receiving connections locally.
yield P.fromCallback(cb => {
server.close(cb);
});
yield request('http://127.0.0.1:8888')
.get('/')
.expect(200)
.then(res => {
expect(res.text).to.be.equal('Hello!');
});
}));
it('should support net.Server object transfer', P.coroutine(function*() {
var server = net.createServer();
yield P.fromCallback(cb => {
server.listen(8889, '127.0.0.1', cb);
});
var child = yield rootActor.createChild({
setServer: function(server) {
// Send hello message on connection.
server.on('connection', socket => {
socket.end('Hello!');
});
this.server = server;
},
destroy: function() {
return require('bluebird').fromCallback(cb => {
this.server.close(cb);
});
}
}, { mode: 'forked' });
yield child.sendAndReceive('setServer', server);
// Close server in this process to avoid receiving connections locally.
yield P.fromCallback(cb => {
server.close(cb);
});
var serverMessage = yield P.fromCallback(cb => {
var clientSocket = new net.Socket();
clientSocket.setEncoding('UTF8');
clientSocket.on('data', data => {
cb(null, data);
});
clientSocket.connect(8889, '127.0.0.1', (err) => {
if (err) return cb(err);
});
});
expect(serverMessage).to.be.equal('Hello!');
}));
it('should be able to pass actor references', P.coroutine(function*() {
var rootActor = yield system.rootActor();
var localCounter = 0;
var localChild = yield rootActor.createChild({
tell: msg => {
localCounter++;
return msg.toUpperCase();
}
});
var forkedChild = yield rootActor.createChild({
setLocal: function(actor) {
this.localActor = actor;
},
tellLocal: function(msg) {
return this.localActor.sendAndReceive('tell', msg);
}
}, { mode: 'forked' });
yield forkedChild.sendAndReceive('setLocal', localChild);
var result = yield forkedChild.sendAndReceive('tellLocal', 'Hello!');
expect(result).to.be.equal('HELLO!');
expect(localCounter).to.be.equal(1);
}));
});
describe('send()', function() {
it('should support variable arguments', P.coroutine(function*() {
var replyDfd = P.pending();
var parent = yield rootActor.createChild({
helloReply: function(from, to) {
replyDfd.resolve(`Hello reply from ${from} to ${to}.`);
}
}, { mode: 'in-memory' });
var child = yield parent.createChild({
initialize: function(selfActor) {
this.parent = selfActor.getParent();
},
hello: function(from, to) {
this.parent.send('helloReply', to, from);
}
}, { mode: 'forked' });
yield child.send('hello', 'Bob', 'Alice');
var result = yield replyDfd.promise;
expect(result).to.be.equal('Hello reply from Alice to Bob.');
}));
});
describe('createChild()', function() {
it('should support ES6 class behaviour definitions', function() {
class TestBase {
sayHello() {
return 'Hello from ' + this.name;
}
}
class TestActor extends TestBase {
initialize() {
this.name = 'TestActor';
}
}
return rootActor
.createChild(TestActor, { mode: 'forked' })
.then(testActor => testActor.sendAndReceive('sayHello'))
.then(result => expect(result).to.be.equal('Hello from TestActor'));
});
it('should support ES5 class behaviour definitions', function() {
var TestActor = function() {
};
TestActor.prototype.initialize = function() {
this.name = 'TestActor';
};
TestActor.prototype.sayHello = function() {
return 'Hello from ' + this.name;
};
return rootActor
.createChild(TestActor, { mode: 'forked' })
.then(testActor => testActor.sendAndReceive('sayHello'))
.then(result => expect(result).to.be.equal('Hello from TestActor'));
});
it('should support ES5 class behaviour definitions in named function form', function() {
function TestActor() {
this.name = 'TestActor';
}
TestActor.prototype.initialize = function() {
this.name += ' initialized';
};
TestActor.prototype.sayHello = function() {
return 'Hello from ' + this.name;
};
return rootActor
.createChild(TestActor, { mode: 'forked' })
.then(testActor => testActor.sendAndReceive('sayHello'))
.then(result => expect(result).to.be.equal('Hello from TestActor initialized'));
});
it('should support ES5 class behaviour definition with inheritance', function() {
function TestBase() {
}
TestBase.prototype.sayHello = function() {
return 'Hello from ' + this.name;
};
function TestActor() {
TestBase.call(this);
}
actors.inherits(TestActor, TestBase);
TestActor.prototype.initialize = function() {
this.name = 'TestActor';
};
return rootActor
.createChild(TestActor, { mode: 'forked' })
.then(testActor => testActor.sendAndReceive('sayHello'))
.then(result => expect(result).to.be.equal('Hello from TestActor'));
});
it('should support crashed actor respawn', P.coroutine(function*() {
var dfd = P.pending();
var localChild = yield rootActor.createChild({
forkedReady: () => {
dfd.resolve();
}
}, { mode: 'in-memory' });
var forkedChild = yield localChild.createChild({
initialize: (selfActor) => {
process.nextTick(() => selfActor.getParent().send('forkedReady'));
},
kill: () => {
process.exit(1);
},
ping: () => 'pong'
}, { mode: 'forked', onCrash: 'respawn' });
// Wait for forked actor to initialize first time.
yield dfd.promise;
for (var i = 0; i < 3; i++) {
// Create new promise.
dfd = P.pending();
// Kill forked actor.
yield forkedChild.send('kill');
// Wait for forked actor to respawn.
yield dfd.promise;
// Ping forked actor.
var resp = yield forkedChild.sendAndReceive('ping');
expect(resp).to.be.equal('pong');
}
}));
it('should be able to load an actor from a given module', function() {
return rootActor
.createChild('/test-resources/actors/test-actor', { mode: 'forked' })
.then(actor => {
expect(actor.getName()).to.be.equal('TestActor');
return actor.sendAndReceive('hello', 123)
.then(response => {
expect(response).to.be.equal('Hello 123!');
});
});
});
it('should be able to load an actor from a given TypeScript module', function() {
return rootActor
.createChild('/test-resources/actors/test-typescript-actor', { mode: 'forked' })
.then(actor => {
expect(actor.getName()).to.be.equal('TestActor');
return actor.sendAndReceive('hello', '123')
.then(response => {
expect(response).to.be.equal('Hello 123!');
});
});
});
it('should be able to pass custom parameters to child actor', P.coroutine(function*() {
class MyActor {
initialize(selfActor) {
this.helloResponse = selfActor.getCustomParameters().helloResponse;
}
hello() {
return this.helloResponse;
}
}
// Create child actor with custom parameter.
var childActor = yield rootActor.createChild(MyActor, {
mode: 'forked',
customParameters: { helloResponse: 'Hi there!' }
});
var response = yield childActor.sendAndReceive('hello');
expect(response).to.be.equal('Hi there!');
}));
it('should be able to pass actor references through custom parameters', P.coroutine(function*() {
var rootActor = yield system.rootActor();
var localCounter = 0;
var localChild = yield rootActor.createChild({
tell: msg => {
localCounter++;
return msg.toUpperCase();
}
});
var forkedChild = yield rootActor.createChild({
initialize: function(selfActor) {
this.localActor = selfActor.getCustomParameters().localActor;
},
tellLocal: function(msg) {
return this.localActor.sendAndReceive('tell', msg);
}
}, {
mode: 'forked',
customParameters: {
localActor: localChild
}
});
var result = yield forkedChild.sendAndReceive('tellLocal', 'Hello!');
expect(result).to.be.equal('HELLO!');
expect(localCounter).to.be.equal(1);
}));
it('should be able to pass http.Server object as custom parameter to child actor', P.coroutine(function*() {
var server = http.createServer();
server.listen(8888);
yield rootActor.createChild({
initialize: function(selfActor) {
this.server = selfActor.getCustomParameters().server;
// Handle HTTP requests.
this.server.on('request', (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello!');
});
},
destroy: function() {
return require('bluebird').fromCallback(cb => {
this.server.close(cb);
});
}
}, { mode: 'forked', customParameters: { server: server } });
// Close server in this process to avoid receiving connections locally.
yield P.fromCallback(cb => {
server.close(cb);
});
yield request('http://127.0.0.1:8888')
.get('/')
.expect(200)
.then(res => {
expect(res.text).to.be.equal('Hello!');
});
}));
it('should be able to pass net.Server object as custom parameter to child actor', P.coroutine(function*() {
var server = net.createServer();
yield P.fromCallback(cb => {
server.listen(8889, '127.0.0.1', cb);
});
yield rootActor.createChild({
initialize: function(selfActor) {
this.server = selfActor.getCustomParameters().server;
// Send hello message on connection.
this.server.on('connection', socket => {
socket.end('Hello!');
});
},
destroy: function() {
return require('bluebird').fromCallback(cb => {
this.server.close(cb);
});
}
}, { mode: 'forked', customParameters: { server: server } });
// Close server in this process to avoid receiving connections locally.
yield P.fromCallback(cb => {
server.close(cb);
});
var serverMessage = yield P.fromCallback(cb => {
var clientSocket = new net.Socket();
clientSocket.setEncoding('UTF8');
clientSocket.on('data', data => {
cb(null, data);
});
clientSocket.connect(8889, '127.0.0.1', (err) => {
if (err) return cb(err);
});
});
expect(serverMessage).to.be.equal('Hello!');
}));
it('should respawn crashed actor with original custom parameters', P.coroutine(function*() {
var server = http.createServer();
server.listen(8888);
var childActor = yield rootActor.createChild({
initialize: function(selfActor) {
this.server = selfActor.getCustomParameters().server;
// Handle HTTP requests.
this.server.on('request', (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello!');
});
},
kill: function() {
process.exit(1);
},
destroy: function() {
return require('bluebird').fromCallback(cb => {
this.server.close(cb);
});
}
}, { mode: 'forked', onCrash: 'respawn', customParameters: { server: server } });
// Close server in this process to avoid receiving connections locally.
yield P.fromCallback(cb => {
server.close(cb);
});
var respawnPromise = new P(resolve => {
childActor.once('respawned', resolve);
});
// Kill child actor.
yield childActor.send('kill');
// Wait for child to respawn.
yield respawnPromise;
// Test connection.
yield request('http://127.0.0.1:8888')
.get('/')
.expect(200)
.then(res => {
expect(res.text).to.be.equal('Hello!');
});
}));
});
describe('createChildren()', function() {
it('should create module actor children from a specified directory', P.coroutine(function*() {
var childActors = yield rootActor.createChildren('/test-resources/actors/child-actors', { mode: 'forked' });
expect(childActors.length).to.be.equal(2);
var childActorNames = _.map(childActors, actor => actor.getName());
expect(childActorNames).to.have.members(['ChildActor1', 'ChildActor2']);
var childActorReplies = yield P.map(childActors, actor => actor.sendAndReceive('hello'));
expect(childActorReplies).to.have.members(['Hello from ChildActor1', 'Hello from ChildActor2']);
}));
});
describe('forwardToChild()', function() {
it('should forward messages with given topics to a given child actor', P.coroutine(function*() {
var parent = yield rootActor.createChild({
initialize: selfActor => {
// Create first child that receives 'hello' messages and sends 'tell...' messages to parent.
var child1Promise = selfActor
.createChild({
initialize: function(selfActor) {
this.parent = selfActor.getParent();
},
hello: function(msg) {
return this.parent.sendAndReceive('tellChild2', msg);
}
}, { mode: 'forked' })
.then(child1 => {
// Forward 'hello' messages to this child.
return selfActor.forwardToChild(child1, 'hello');
});
// Create second child that receives 'tell...' messages and writes to mailbox.
var child2Promise = selfActor
.createChild({
initialize: function() {
this.mailbox = [];
},
tellChild2: function(msg) {
this.mailbox.push(msg);
},
getMailbox: function() {
return this.mailbox;
}
}, { mode: 'forked' })
.then(child2 => {
// Forward 'tell...' and 'getMailbox' messages to this child.
return selfActor.forwardToChild(child2, /^tell.*/, 'getMailbox');
});
return P.join(child1Promise, child2Promise);
}
});
yield parent.sendAndReceive('hello', 'World!');
var child2Mailbox = yield parent.sendAndReceive('getMailbox');
expect(child2Mailbox).to.have.members(['World!']);
}));
});
describe('metrics()', function() {
it('should collect metrics from target actor and all the actor sub-tree', P.coroutine(function*() {
var parent = yield rootActor.createChild({
metrics: function() {
return {
parentMetric: 111
};
}
});
yield parent.createChild({
metrics: function() {
return {
childMetric: 222
};
}
}, { name: 'Child1', mode: 'forked' });
yield parent.createChild({
metrics: function() {
return {
childMetric: 333
};
}
}, { name: 'Child2', mode: 'forked' });
var metrics = yield parent.metrics();
expect(metrics).to.be.deep.equal({
parentMetric: 111,
Child1: {
childMetric: 222
},
Child2: {
childMetric: 333
}
});
}));
it('should not collect metrics from destroyed actors', P.coroutine(function*() {
var parent = yield rootActor.createChild({
metrics: function() {
return {
parentMetric: 111
};
}
});
yield parent.createChild({
metrics: function() {
return {
childMetric: 222
};
}
}, { name: 'Child1', mode: 'forked' });
var child2 = yield parent.createChild({
metrics: function() {
return {
childMetric: 333
};
}
}, { name: 'Child2', mode: 'forked' });
yield child2.destroy();
var metrics = yield parent.metrics();
expect(metrics).to.be.deep.equal({
parentMetric: 111,
Child1: {
childMetric: 222
}
});
}));
});
});