-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactor_test.js
193 lines (180 loc) · 5.15 KB
/
actor_test.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
/*
* actor_test.js -- actor.js unit tests
*
* author: Dale Schumacher <[email protected]>
*/
import core from "./core.js";
import Actor from "./actor.js";
import Test from "./test.js";
function testSuite (callback) {
var log = core.trace; // log to trace channel
var trace = core.trace;
var Dictionary = core.Dictionary;
var Config = Actor.Config;
var suite = Test.Suite();
var asyncTest = function (name, test_fn) {
suite.asyncTest(name, function () {
log('... '+name+' ...');
test_fn();
});
};
var asyncWait = suite.asyncWait;
var asyncDone = suite.asyncDone;
var test = function (name, test_fn) {
suite.test(name, function () {
log('--- '+name+' ---');
test_fn();
});
};
var fail = suite.fail;
var assertEqual = suite.assertEqual;
var assert_fail_beh = function (msg) {
fail('assert_fail_beh');
};
var assert_end_beh = function (end) {
return function (msg) {
assertEqual(end, msg, 'assert_end_beh');
this.become(assert_fail_beh);
};
};
var assert_equal_beh = function (expect, next) {
return function (actual) {
assertEqual(expect, actual);
this.become(next);
};
};
test('create Config', function () {
var cfg = Config();
assertEqual(true, cfg.sink.isActor, 'cfg.sink.isActor');
});
test('actor basics', function () {
var cfg = Config();
var actor;
assertEqual(0, cfg.pending(), 'no messages in initial config');
actor = cfg.create(); // create default (sink) actor
assertEqual(true, actor.isActor, 'run-time type indicator');
cfg.send('Hello', actor); // imperative send
assertEqual(1, cfg.pending(), 'first message queued for dispatch');
actor.send('World'); // object-oriented send
assertEqual(2, cfg.pending(), 'second message queued for dispatch');
assertEqual(true, cfg.dispatch(), 'first message delivered');
assertEqual(true, cfg.dispatch(), 'second message delivered');
assertEqual(false, cfg.dispatch(), 'all messages delivered');
assertEqual(0, cfg.pending(), 'no more message to dispatch');
});
test('single message', function () {
var self = this;
var cfg = Config();
var mock = cfg.create(
assert_equal_beh(
'single',
assert_end_beh(self)
)
);
// test behavior
cfg.send('single', mock);
assertEqual(1, cfg.pending(), 'pending before');
while (cfg.dispatch())
;
assertEqual(0, cfg.pending(), 'pending after');
// verify mock
cfg.send(self, mock);
assertEqual(true, cfg.dispatch(), 'end mock');
assertEqual(false, cfg.dispatch(), 'end config');
});
test('observer pattern', function () {
var subject_beh = Actor.subject_beh;
var cfg = Config();
var a, b, c, s;
var observer_beh = function (label) {
return function (msg) {
trace('observer[' + label +']: ' + msg);
};
};
a = cfg.create(observer_beh('a'));
b = cfg.create(observer_beh('b'));
c = cfg.create(observer_beh('c'));
s = cfg.create(subject_beh());
s.send(Dictionary({ op:'attach', observer:a }));
s.send(Dictionary({ op:'attach', observer:b }));
s.send(Dictionary({ op:'attach', observer:c }));
s.send(Dictionary({ op:'notify', event:'Ping!' }));
assertEqual(4, cfg.pending(), 'pending before');
while (cfg.dispatch())
;
assertEqual(0, cfg.pending(), 'pending after');
});
asyncTest('delay timer', function () {
var sink_beh = Actor.sink_beh;
var cfg = Config();
var delay_beh = function (delay, target) {
return function (msg) {
setTimeout(function () {
log('AFTER ' + delay + ' SEND (' + msg + ') TO ' + target);
cfg.send(msg, target);
}, delay);
};
};
var done = false;
var countdown_beh = function (count) {
return function (msg) {
trace('countdown: ' + count);
if (count > 0) {
--count;
this.become(countdown_beh(count));
this.send(msg,
this.create(delay_beh(337, this.self)));
} else {
done = true;
this.become(sink_beh);
}
};
};
var timer_dispatch = function () {
trace('timer_dispatch: ' + cfg);
while (cfg.dispatch())
;
if (!done) {
trace('timer_dispatch: waiting...');
setTimeout(timer_dispatch, 144);
} else {
trace('timer_dispatch: done!');
asyncDone();
}
};
cfg.create(countdown_beh(5)).send('tick');
timer_dispatch();
asyncWait();
});
asyncTest('run/halt', function () {
var sink_beh = Actor.sink_beh;
var cfg = Config();
var countdown_beh = function (count) {
return function (msg) {
trace('countdown: ' + count);
if (count > 0) {
--count;
this.become(countdown_beh(count));
this.sendAfter(400, count, this.self);
} else {
trace('countdown: done!');
cfg.halt();
this.become(sink_beh);
asyncDone();
}
};
};
cfg.create(countdown_beh(3)).send(3);
cfg.run();
asyncWait();
});
return suite.getResult(callback);
};
function run_tests() {
var log = core.log; // log to info channel
log('core(Actor) v' + Actor.version);
testSuite(function (result) {
log(result.formatted('Actor suite: '));
});
};
export default Object.freeze({testSuite, run_tests});