UNPKG

5.61 kBJavaScriptView Raw
1require('mocha');
2var should = require('should');
3var _ = require('lodash');
4
5var actors = require(__dirname + '/../lib/actors');
6var properties = require(__dirname + '/../lib/properties');
7var utils = {
8 aid: require(__dirname + '/../lib/utils/aid')
9};
10
11describe('actors module', function () {
12 var originalID = properties.ID;
13 var originalNetInfo = properties.netInfo;
14
15 var testActors = {
16 ping: {id: 'ping', container: {id: '0', netInfo: {pid: 0, ip: '0.0.0.0'}}},
17 pong: {id: 'pong', container: {id: '1', netInfo: {pid: 1, ip: '0.0.0.0'}}},
18 peng: {id: 'peng', container: {id: '2', netInfo: {pid: 3, ip: '1.1.1.1'}}},
19 pang: {id: 'pang', container: {id: '2', netInfo: {pid: 3, ip: '1.1.1.1'}}},
20 fping1: {id: 'fping/1', container: {id: '0', netInfo: {pid: 0, ip: '0.0.0.0'}}},
21 fping2: {id: 'fping/2', container: {id: '0', netInfo: {pid: 0, ip: '0.0.0.0'}}},
22 fping4: {id: 'fping/3', container: {id: '2', netInfo: {pid: 3, ip: '1.1.1.1'}}}
23 };
24
25 before(function () {
26 properties.ID = 0;
27 properties.netInfo = {pid: 0, ip: '0.0.0.0'};
28 });
29
30 after(function () {
31 properties.ID = originalID;
32 properties.netInfo = originalNetInfo;
33 });
34
35 beforeEach(function () {
36 _.forOwn(testActors, function (actor) {
37 actors.add(actor);
38 });
39 });
40
41 afterEach(function () {
42 _.forOwn(testActors, function (actor) {
43 actors.remove(actor.id);
44 });
45 });
46
47 describe('getAll function', function () {
48 it('should return [fping/1]', function () {
49 var aids = actors.getAll('fping/1');
50 should.exist(aids);
51 aids.should.be.an.instanceof(Array);
52 aids.should.have.length(1);
53 aids[0].should.be.eql('fping/1');
54 });
55 it('should return [fping/1, fping/2, fping/3]', function () {
56 var aids = actors.getAll('fping');
57 should.exist(aids);
58 aids.should.be.an.instanceof(Array);
59 aids.should.have.length(3);
60 });
61 it('should return [fping/3]', function () {
62 var aids = actors.getAll('fping', actors.scope.REMOTE);
63 should.exist(aids);
64 aids.should.be.an.instanceof(Array);
65 aids.should.have.length(1);
66 aids[0].should.be.eql('fping/3');
67 });
68 it('should return []', function () {
69 var aids = actors.getAll('fake');
70 should.exist(aids);
71 aids.should.be.an.instanceof(Array);
72 aids.should.be.empty;
73 });
74 });
75
76 describe('get function', function () {
77 it('should return ping actor', function () {
78 var aids = actors.getAll('ping');
79 aids.should.have.length(1);
80 var actor = actors.get(aids[0]);
81 actor.should.be.eql(testActors.ping);
82 actor.scope.should.be.eql(actors.scope.PROCESS);
83 });
84 it('should return null', function () {
85 var actor = actors.get('fake');
86 should.not.exist(actor);
87 });
88 it('should return ping actor (force scope : process)', function () {
89 var aids = actors.getAll('ping');
90 aids.should.have.length(1);
91 var actor = actors.get(aids[0], actors.scope.PROCESS);
92 actor.should.be.eql(testActors.ping);
93 actor.scope.should.be.eql(actors.scope.PROCESS);
94 });
95 it('should return pong actor (force scope : local)', function () {
96 var aids = actors.getAll('pong');
97 aids.should.have.length(1);
98 var actor = actors.get(aids[0], actors.scope.LOCAL);
99 actor.should.be.eql(testActors.pong);
100 actor.scope.should.be.eql(actors.scope.LOCAL);
101 });
102 it('should return peng actor (force scope : remote)', function () {
103 var aids = actors.getAll('peng');
104 aids.should.have.length(1);
105 var actor = actors.get(aids[0], actors.scope.REMOTE);
106 actor.should.be.eql(testActors.peng);
107 actor.scope.should.be.eql(actors.scope.REMOTE);
108 });
109 });
110
111 describe('remove function', function () {
112 it('should remove an actor', function () {
113 actors.remove('ping');
114 var aids = actors.getAll('ping');
115 aids.should.have.length(0);
116 });
117 it('should not remove an actor', function () {
118 actors.remove('ping', actors.scope.REMOTE);
119 var aids = actors.getAll('ping');
120 aids.should.have.length(1);
121 });
122 });
123
124 describe('removeByContainer function', function () {
125 it('should remove peng and pang', function () {
126 actors.removeByContainer('2');
127 var aids = actors.getAll('peng');
128 aids.should.have.length(0);
129 aids = actors.getAll('pang');
130 aids.should.have.length(0);
131 });
132 });
133
134 describe('add function', function () {
135 it('should add an actor', function () {
136 var actor = {id: 'tmp', container: {id: '12', netInfo: {pid: 123, ip: '1.2.3.4'}}};
137 actors.add(actor);
138 var aids = actors.getAll('tmp');
139 aids.should.have.length(1);
140 var retreivedActor = actors.get(aids[0]);
141 should.exist(retreivedActor);
142 retreivedActor.should.be.eql(actor);
143 actors.remove('tmp');
144 });
145 });
146
147 describe('pick function', function () {
148 it('should return ping', function () {
149 var aid = actors.pick('ping');
150 should.exist(aid);
151 utils.aid.bare(aid).should.be.eql('ping');
152 });
153 it('should return a process element', function () {
154 var aid = actors.pick('fping');
155 should.exist(aid);
156 utils.aid.bare(aid).should.be.eql('fping');
157 actors.get(aid).scope.should.be.eql(actors.scope.PROCESS);
158 });
159 });
160
161 describe('exists function', function () {
162 it('should return true', function () {
163 actors.exists(actors.pick('ping')).should.be.eql(true);
164 });
165 it('should return false', function () {
166 actors.exists(actors.pick('fake')).should.be.eql(false);
167 });
168 });
169});