-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.js
169 lines (138 loc) · 4.27 KB
/
tests.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
'use strict';
const net = require('net');
const zlib = require('zlib');
const test = require('tap').test;
const jmsg = require('./');
const createPair = () => {
const left = jmsg((obj) => {
right.dispatch(obj);
});
const right = jmsg((obj) => {
left.dispatch(obj);
});
left.timeout = right.timeout = 100;
return { left: left, right: right };
};
test('action / reply', { timeout: 500 }, (t) => {
t.plan(5);
const pair = createPair();
pair.right.handlers.beep = (num, cb) => {
t.pass('Right side received action');
cb(null, num + 1, rightReplyCb);
};
const leftReplyCb = (err, num, cb) => {
t.ok(!err, 'Left side received reply ' + num);
cb(null, num + 1, num < 3 && leftReplyCb);
};
const rightReplyCb = (err, num, cb) => {
t.ok(!err, 'Right side received reply ' + num);
cb(null, num + 1, num < 4 && rightReplyCb);
};
pair.left.send('beep', 0, leftReplyCb);
});
test('event', { timeout: 500 }, (t) => {
t.plan(2);
const pair = createPair();
pair.right.handlers.beep = (err) => {
t.ok(!err, 'Received event');
};
pair.left.send('beep', (err) => {
t.is(err.message, 'Timeout', 'Event reply should time out');
});
});
test('bad action / reply', { timeout: 500 }, (t) => {
t.plan(3);
const pair = createPair();
pair.left.send('beep', (err) => {
t.is(err.message, 'No such action',
'Non-existant action should error');
});
pair.right.handlers.beep = (val, cb) => {
t.pass('Received action');
cb(null, null, (err) => {
t.is(err.message, 'No reply expected',
'Reply to event should error');
});
};
pair.left.send('beep');
});
test('close', { timeout: 500 }, (t) => {
t.plan(5);
const pair = createPair();
// This is here to check for double callbacks.
pair.right.handlers.good = (arg, cb) => {
t.pass('Received good action');
cb();
};
pair.left.send('good', (err) => {
t.ok(!err, 'Received good reply');
});
pair.right.handlers.bad = () => {
t.pass('Received bad action');
};
pair.left.send('bad', (err) => {
t.is(err.message, 'Connection closed',
'Open callback receives error on close');
});
pair.left.close();
pair.left.send('good', (err) => {
t.is(err.message, 'Connection closed',
'Callback receives error after close');
});
});
const createSocketPair = (t, cb) => {
let right;
const server = net.createServer((left) => {
t.on('end', () => {
left.destroy();
});
cb(left, right);
});
t.on('end', () => {
server.close();
});
server.listen(0, () => {
right = net.connect(server.address().port);
t.on('end', () => {
right.destroy();
});
});
};
test('duplex streams', { timeout: 500 }, (t) => {
t.plan(2);
createSocketPair(t, (leftSocket, rightSocket) => {
const left = jmsg.stream(leftSocket);
const right = jmsg.stream(rightSocket);
left.timeout = right.timeout = 100;
right.handlers.beep = (arg, cb) => {
t.pass('Right side received action');
cb();
};
left.send('beep', (err) => {
t.ok(!err, 'Left side received reply');
});
});
});
test('read/write streams', { timeout: 500 }, (t) => {
t.plan(2);
createSocketPair(t, (leftSocket, rightSocket) => {
const leftWrite = zlib.createGzip({ flush: zlib.Z_SYNC_FLUSH });
const leftRead = zlib.createGunzip();
leftWrite.pipe(leftSocket);
leftSocket.pipe(leftRead);
const rightWrite = zlib.createGzip({ flush: zlib.Z_SYNC_FLUSH });
const rightRead = zlib.createGunzip();
rightWrite.pipe(rightSocket);
rightSocket.pipe(rightRead);
const left = jmsg.streams(leftRead, leftWrite);
const right = jmsg.streams(rightRead, rightWrite);
left.timeout = right.timeout = 100;
right.handlers.beep = (arg, cb) => {
t.pass('Right side received action');
cb();
};
left.send('beep', (err) => {
t.ok(!err, 'Left side received reply');
});
});
});