-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtests.html
More file actions
206 lines (176 loc) · 4.18 KB
/
tests.html
File metadata and controls
206 lines (176 loc) · 4.18 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
<!DOCTYPE html>
<html><head>
<style type="text/css">
body {
margin: 50px;
color: white;
background: black;
font: 13px Arial, sans-serif;
}
.pass {
color: #00FF00;
}
.fail {
color: #FF0000;
}
</style>
<script type="text/javascript" src="http://localhost:5000/socket.io/socket.io.js"></script>
<script type="text/javascript">
function log(html) {
document.body.innerHTML += html;
}
var sockets = [];
function socket(pass, fail, expects) {
var sock = new io.Socket('localhost', { port: 5000 });
sock.on('message', function(data) {
var expected = expects.shift();
if (data !== expected) fail('expected "' + expected + '" but got "' + data + '"');
else if (expects.length == 0) pass();
});
sock.on('disconnect', function() {
fail('lost connection');
});
sockets.push(sock);
return sock;
}
function test(name, run) {
tests.push({ name: name, run: run });
}
var tests = [];
var numTests = 0;
var numTestsPassed = 0;
var currentTest = -1;
function nextTest() {
currentTest++;
// must disconnect sockets because Firefox 3 doesn't
// allow more than a few simultaneous connections
while (sockets.length > 0) {
sockets.pop().disconnect();
}
if (currentTest < tests.length) {
log(tests[currentTest].name + ' ... ');
try {
var current = currentTest;
tests[currentTest].run(function() {
if (current == currentTest) pass();
}, function(text) {
if (current == currentTest) fail(text);
});
setTimeout(function() {
if (current == currentTest) fail('timeout after 3 seconds');
}, 3000);
} catch (e) {
if (current == currentTest) fail(e.toString());
}
} else {
log('<p>' + numTestsPassed + ' / ' + numTests + ' tests passed</p>');
}
}
function pass() {
log('<span class="pass">passed</span><br>');
numTestsPassed++;
numTests++;
nextTest();
}
function fail(text) {
log('<span class="fail">failed, ' + text + '</span><br>');
numTests++;
nextTest();
}
test('echo', function(pass, fail) {
var s = socket(pass, fail, ['test']);
s.on('connect', function() {
s.send('echo:test');
});
s.connect();
});
test('three clients', function(pass, fail) {
function partOne() {
// give server time process all new connections
setTimeout(function() {
b.send('send:a:a1');
c.send('send:b:b2');
a.send('send:c:c3');
}, 100);
}
function partTwo() {
c.send('send:c:c4');
a.send('send:b:b5');
b.send('send:a:a6');
}
var checkCount = 0;
function check() {
if (++checkCount == 3) pass();
}
var a = socket(check, fail, ['a1', 'a6']);
var b = socket(check, fail, ['b2', 'b5']);
var c = socket(check, fail, ['c3', 'c4']);
var connectCount = 0;
a.on('connect', function() {
a.send('setname:a');
if (++connectCount == 3) partOne();
});
b.on('connect', function() {
b.send('setname:b');
if (++connectCount == 3) partOne();
});
c.on('connect', function() {
c.send('setname:c');
if (++connectCount == 3) partOne();
});
var messageCount = 0;
function checkStart() {
if (++messageCount == 3) partTwo();
}
a.on('message', checkStart);
b.on('message', checkStart);
c.on('message', checkStart);
a.connect();
b.connect();
c.connect();
});
test('broadcast', function(pass, fail) {
function start() {
// give server time process all new connections
setTimeout(function() {
b.send('broadcast:stuff');
}, 100);
}
var checkCount = 0;
function check() {
if (++checkCount == 3) pass();
}
var a = socket(check, fail, ['stuff']);
var b = socket(check, fail, ['stuff']);
var c = socket(check, fail, ['stuff']);
var connectCount = 0;
function checkStart() {
if (++connectCount == 3) start();
}
a.on('connect', checkStart);
b.on('connect', checkStart);
c.on('connect', checkStart);
a.connect();
b.connect();
c.connect();
});
test('large buffer', function(pass, fail) {
var buffer = '';
for (var i = 0; i < 1024; i++) {
buffer += Math.random() + ',';
}
buffer += 'end';
var s = socket(pass, fail, ['a', buffer, 'b']);
s.on('connect', function() {
s.send('echo:a');
s.send('echo:' + buffer);
s.send('echo:b');
});
s.connect();
});
window.onload = nextTest;
</script>
</head><body>
<h1>Tests</h1>
<p>If the tests below don't work, try running <code>tests.py</code> and refreshing the page.</p>
</body></html>