/
should-connect.test.ts
283 lines (220 loc) · 7.88 KB
/
should-connect.test.ts
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import { describe, expect, test, vi } from 'vitest'
import { makeLogger, wsScenario } from './fixtures.js'
import * as Y from 'yjs'
import { waitForExpect } from './test-utils.js'
import { OPEN } from '../src/internal.js'
describe.concurrent('server shouldConnect', () => {
test('waits to sync docs after shouldConnect', async () => {
const { wss, makeYjsServer, makeClient } = wsScenario()
let proceedWithConnection!: (v: boolean) => void
const shouldConnect = new Promise<boolean>((resolve) => {
proceedWithConnection = resolve
})
const server = makeYjsServer()
wss.on('connection', (conn, req) => {
server.handleConnection(conn, req, shouldConnect)
})
const doc1 = new Y.Doc()
const doc2 = new Y.Doc()
doc1.getMap('root').set('foo', 'bar')
const client1 = makeClient(doc1)
const client2 = makeClient(doc2)
await waitForExpect(() => {
expect(client1.wsconnected).toBe(true)
expect(client2.wsconnected).toBe(true)
expect(doc2.getMap('root').toJSON()).toEqual({})
})
await new Promise((resolve) => setTimeout(resolve, 50))
doc2.getMap('root').set('foo2', 'bar2')
expect(client1.synced).toBe(false)
expect(client2.synced).toBe(false)
expect(doc1.getMap('root').toJSON()).toEqual({ foo: 'bar' })
expect(doc2.getMap('root').toJSON()).toEqual({ foo2: 'bar2' })
proceedWithConnection(true)
await waitForExpect(() => {
expect(client1.synced).toBe(true)
expect(client2.synced).toBe(true)
expect(doc1.getMap('root').toJSON()).toEqual({ foo: 'bar', foo2: 'bar2' })
expect(doc2.getMap('root').toJSON()).toEqual({ foo: 'bar', foo2: 'bar2' })
})
})
test('does not closes socket if shouldConnect resolves false', async () => {
const { wss, makeYjsServer, makeClient } = wsScenario()
const CLOSE_CODE = 4001
let closeConnection!: () => void
const shouldConnect = Promise.resolve(false)
const server = makeYjsServer()
wss.on('connection', (conn, req) => {
server.handleConnection(conn, req, shouldConnect)
void shouldConnect.then(() => {
closeConnection = () => conn.close(CLOSE_CODE)
expect(conn.readyState).toBe(OPEN)
})
})
const client = makeClient(new Y.Doc())
const connectionClose = new Promise<unknown>((resolve) => {
client.once('connection-close', (ev: unknown) => {
resolve(ev)
})
})
await waitForExpect(() => {
expect(client.wsconnected).toBe(true)
})
closeConnection()
const closeEvent = await connectionClose
expect((closeEvent as { code: number }).code).toBe(CLOSE_CODE)
})
test('awaiting shouldConnect socket does not interfere with room updates', async () => {
const { wss, makeYjsServer, makeClient } = wsScenario()
let shouldConnect = Promise.resolve(true)
const server = makeYjsServer()
wss.on('connection', (conn, req) => {
void shouldConnect.then((should) => {
if (!should) conn.close(4001)
})
server.handleConnection(conn, req, shouldConnect)
})
const doc1 = new Y.Doc()
const doc2 = new Y.Doc()
doc1.getMap('root').set('doc1_start', true)
const client1 = makeClient(doc1)
const client2 = makeClient(doc2)
await waitForExpect(() => {
expect(client1.synced).toBe(true)
expect(client2.synced).toBe(true)
})
let resolveShouldConnect!: (v: boolean) => void
shouldConnect = new Promise<boolean>((resolve) => {
resolveShouldConnect = resolve
})
const doc3 = new Y.Doc()
const client3 = makeClient(doc3)
doc3.getMap('root').set('doc3_start', true)
await waitForExpect(() => {
expect(client3.wsconnected).toBe(true)
})
doc1.getMap('root2').set('doc1_change1', true)
doc2.getMap('root').set('doc2_change1', true)
doc2.getMap('root2').set('doc2_change2', true)
await waitForExpect(() => {
expect(doc1.getMap('root').toJSON()).toEqual({ doc1_start: true, doc2_change1: true })
expect(doc1.getMap('root2').toJSON()).toEqual({ doc1_change1: true, doc2_change2: true })
expect(doc3.getMap('root').toJSON()).toEqual({ doc3_start: true })
expect(client3.synced).toBe(false)
})
resolveShouldConnect(false)
doc3.getMap('root').delete('doc3_start')
await waitForExpect(() => {
expect(client3.wsconnected).toBe(false)
})
await waitForExpect(() => {
expect(doc1.getMap('root').toJSON()).toEqual({ doc1_start: true, doc2_change1: true })
expect(doc1.getMap('root2').toJSON()).toEqual({ doc1_change1: true, doc2_change2: true })
expect(doc3.getMap('root').toJSON()).toEqual({})
})
shouldConnect = Promise.resolve(true)
client3.connect()
await waitForExpect(() => {
expect(client3.synced).toBe(true)
})
doc3.getMap('root').clear()
await waitForExpect(() => {
expect(doc1.getMap('root').toJSON()).toEqual({})
expect(doc2.getMap('root').toJSON()).toEqual({})
expect(doc3.getMap('root').toJSON()).toEqual({})
})
})
test('terminates connection if max size is exceeded', async () => {
const { wss, makeYjsServer, makeClient } = wsScenario()
const warn = vi.fn()
const server = makeYjsServer({
maxBufferedBytesBeforeConnect: 1024,
logger: makeLogger({ warn }),
})
wss.on('connection', (conn, req) => {
server.handleConnection(
conn,
req,
new Promise(() => {
// never resolve
}),
)
})
const doc = new Y.Doc()
const client = makeClient(doc)
await waitForExpect(() => {
expect(client.wsconnected).toBe(true)
})
doc.getMap('root').set('heavy', new Array(1024).fill('a'))
doc.getMap('root').set('heavy2', new Array(1024).fill('a'))
await waitForExpect(() => {
expect(client.wsconnected).toBe(false)
expect(warn).toHaveBeenCalledOnce()
expect(warn).toHaveBeenCalledWith(
expect.objectContaining({}),
expect.stringMatching(/message buffer exceeded/),
)
})
})
test('terminates connection if shouldConnect rejects', async () => {
const { wss, makeYjsServer, makeClient } = wsScenario()
const logError = vi.fn()
const server = makeYjsServer({
logger: makeLogger({ error: logError }),
})
const err = new Error('something happened')
wss.on('connection', (conn, req) => {
server.handleConnection(
conn,
req,
new Promise((resolve, reject) => {
setTimeout(() => {
reject(err)
}, 50)
}),
)
})
const client = makeClient(new Y.Doc())
client.on('connection-close', () => {
// currently, y-websocket client will infinitely retry without backoff during the 50ms window above
client.shouldConnect = false
})
await waitForExpect(() => {
expect(client.wsconnected).toBe(false)
expect(logError).toHaveBeenCalledOnce()
expect(logError).toHaveBeenCalledWith(
expect.objectContaining({ err }),
expect.stringMatching(/error handling new connection/),
)
})
})
test('terminates connection on string message shouldConnect is pending', async () => {
const { wss, makeYjsServer, makeClient } = wsScenario()
const warn = vi.fn()
const server = makeYjsServer({
logger: makeLogger({ warn: warn }),
})
wss.on('connection', (conn, req) => {
server.handleConnection(
conn,
req,
new Promise(() => {
// never resolve
}),
)
})
const client = makeClient(new Y.Doc())
await waitForExpect(() => {
expect(client.wsconnected).toBe(true)
})
client.ws.send('bad input')
await waitForExpect(() => {
expect(client.wsconnected).toBe(false)
expect(warn).toHaveBeenCalledOnce()
expect(warn).toHaveBeenCalledWith(
expect.objectContaining({}),
expect.stringMatching(/received a non-arraybuffer message/),
)
})
})
})