forked from soyuka/signalhubws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
135 lines (115 loc) · 3.33 KB
/
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
var server = require('./server')()
var client = require('./index')
var tape = require('tape')
global.window = {}
window.WebSocket = require('websocket').w3cwebsocket
server.listen(9000, function () {
tape('subscribe', function (t) {
var c = client('app', ['localhost:9000'])
c.subscribe('hello').on('data', function (message) {
t.same(message, {hello: 'world'})
t.end()
c.close()
}).on('open', function () {
c.broadcast('hello', {hello: 'world'})
})
})
tape('subscribe two apps', function (t) {
t.plan(2)
var missing = 2
var c1 = client('app1', ['localhost:9000'])
c1.subscribe('hello').on('data', function (message) {
t.same(message, {hello: 'world'})
done()
}).on('open', function () {
c1.broadcast('hello', {hello: 'world'})
})
var c2 = client('app2', ['localhost:9000'])
c2.subscribe('hello').on('data', function (message) {
t.same(message, {hello: 'world'})
done()
}).on('open', function () {
c2.broadcast('hello', {hello: 'world'})
})
function done () {
if (--missing) return
setTimeout(function () {
c1.close()
c2.close()
t.end()
}, 100)
}
})
tape('subscribe with trailing /', function (t) {
var c = client('app', ['localhost:9000/'])
c.subscribe('hello').on('data', function (message) {
t.same(message, {hello: 'world'})
t.end()
c.close()
}).on('open', function () {
c.broadcast('hello', {hello: 'world'})
})
})
tape('subscribe to many', function (t) {
var c = client('app', ['localhost:9000'])
var msgs = ['stranger', 'friend']
c.subscribe(['hello', 'goodbye']).on('data', function (message) {
t.same(message, {msg: msgs.shift()})
if (msgs.length === 0) {
c.close(function () {
t.equal(c.subscribers.length, 0, 'all subscribers closed')
t.end()
})
}
}).on('open', function () {
c.broadcast('hello', {msg: 'stranger'}, function () {
c.broadcast('goodbye', {msg: 'friend'})
})
})
})
tape('close multiple', function (t) {
var c = client('app', ['localhost:9000'])
c.subscribe(['hello', 'goodbye'])
c.subscribe(['hi', 'bye'])
c.close(function () {
t.equal(c.subscribers.length, 0, 'all subscribers closed')
t.end()
})
})
tape('subscribe to channels with slash in the name', function (t) {
var c = client('app', ['localhost:9000'])
c.subscribe('hello/people').on('data', function (message) {
t.same(message, [1, 2, 3])
t.end()
c.close()
}).on('open', function () {
c.broadcast('hello/people', [1, 2, 3])
})
})
tape('open emitted with multiple hubs', function (t) {
var c = client('app', [
'localhost:9000',
'localhost:9000'
])
c.subscribe('hello').on('open', function () {
t.ok(true, 'got an open event')
c.close()
t.end()
})
})
tape('subscribe to channels with slash in the url', function (t) {
var c = client('app', ['localhost:9000/foo'])
c.subscribe('hello/bar').on('data', function (message) {
t.same(message, [1, 2, 3])
t.end()
c.close()
}).on('open', function () {
c.broadcast('hello/bar', [1, 2, 3])
})
})
tape('end', function (t) {
server.close()
t.ok(true)
t.end()
})
})