-
Notifications
You must be signed in to change notification settings - Fork 11
/
client-server.js
120 lines (110 loc) · 3.38 KB
/
client-server.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
import { assert } from 'chai'
import Client from '../src/client'
import Server from '../src/server'
const address = 'tcp://127.0.0.1:5001'
describe('Client/Server', () => {
let client, server
beforeEach((done) => {
client = new Client({})
server = new Server({})
done()
})
afterEach(async () => {
await client.close()
await server.close()
client = null
server = null
})
it('tickToServer', done => {
let expectedMessage = 'xndzor'
server.bind(address)
.then(() => {
return client.connect(address)
})
.then(() => {
server.onTick('tandz', (message) => {
assert.equal(message, expectedMessage)
done()
})
client.tick({ event: 'tandz', data: expectedMessage })
})
})
it('requesttoServer-timeout', done => {
let expectedMessage = 'xndzor'
server.bind(address)
.then(() => {
return client.connect(address)
})
.then(() => {
return client.request({ event: 'tandz', data: expectedMessage, timeout: 500 })
})
.catch(err => {
assert.include(err.message, 'timeouted')
done()
})
})
it('requestToServer-response', done => {
let expectedMessage = 'xndzor'
server.bind(address)
.then(() => {
return client.connect(address)
})
.then(() => {
server.onRequest('tandz', ({body, reply}) => {
assert.equal(body, expectedMessage)
reply(expectedMessage)
})
return client.request({ event: 'tandz', data: expectedMessage, timeout: 2000 })
})
.then((message) => {
assert.equal(message, expectedMessage)
done()
})
})
it('tickToClient', done => {
let expectedMessage = 'xndzor'
server.bind(address)
.then(() => {
return client.connect(address)
})
.then(() => {
client.onTick('tandz', message => {
assert.equal(message, expectedMessage)
done()
})
server.tick({ to: client.getId(), event: 'tandz', data: expectedMessage })
})
})
it('requestToClient-timeout', done => {
let expectedMessage = 'xndzor'
server.bind(address)
.then(() => {
return client.connect(address)
})
.then(() => {
return server.request({ to: client.getId(), event: 'tandz', data: expectedMessage, timeout: 500 })
})
.catch(err => {
assert.include(err.message, 'timeouted')
done()
})
})
it('requestToClient-response', done => {
let expectedMessage = 'xndzor'
server.bind(address)
.then(() => {
return client.connect(address)
})
.then(() => {
client.onRequest('tandz', ({body, reply}) => {
assert.equal(body, expectedMessage)
reply(body)
})
return server.request({ to: client.getId(), event: 'tandz', data: expectedMessage })
})
.then(message => {
assert.equal(message, expectedMessage)
done()
})
})
})