-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadapter-lifecycle.test.ts
More file actions
260 lines (229 loc) Β· 8.79 KB
/
Copy pathadapter-lifecycle.test.ts
File metadata and controls
260 lines (229 loc) Β· 8.79 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
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
import { expect, it } from 'vitest';
import type { NamespaceContract, ServerSocketContract } from './contract';
import { Adapter, Server } from './index';
import { observeDisconnect, receive } from './test-events';
type RuntimeNamespace = NamespaceContract & {
sockets: Map<string, ServerSocketContract>;
};
class RemovalAdapter extends Adapter {
onRemove: ((sid: string) => void) | undefined;
readonly removals: Array<{
sid: string;
hasRoomMember: boolean;
hasSidEntry: boolean;
inNamespaceRoster: boolean;
}> = [];
constructor(private readonly namespace: RuntimeNamespace) {
super();
}
removeSocket(sid: string): void {
this.onRemove?.(sid);
this.removals.push({
sid,
hasRoomMember: [...this.rooms.values()].some((members) => members.has(sid)),
hasSidEntry: this.sids.has(sid),
inNamespaceRoster: this.namespace.sockets.has(sid),
});
}
}
function registerRemovalAdapters(io: Server): Map<string, RemovalAdapter> {
const adapters = new Map<string, RemovalAdapter>();
io.adapter((namespace) => {
const adapter = new RemovalAdapter(namespace as RuntimeNamespace);
adapters.set(namespace.name, adapter);
return adapter;
});
return adapters;
}
it('builds isolated adapters for root, existing, future static, and dynamic namespaces', async () => {
const io = new Server('http://localhost');
const existing = io.of('/existing');
const adapters = new Map<string, Adapter>();
io.adapter((namespace) => {
const adapter = new Adapter();
adapters.set(namespace.name, adapter);
return adapter;
});
const future = io.of('/future');
io.of(/^\/tenant-/);
io.connect();
const rootSocket = await io.nextConnection();
io.connect('/existing');
const existingSocket = await io.nextConnection('/existing');
io.connect('/future');
const futureSocket = await io.nextConnection('/future');
const tenant = io.connect('/tenant-a');
await receive(tenant, 'connect');
expect([...adapters.keys()]).toEqual(['/', '/existing', '/future', '/tenant-a']);
expect(io.of('/').adapter).toBe(adapters.get('/'));
expect(existing.adapter).toBe(adapters.get('/existing'));
expect(future.adapter).toBe(adapters.get('/future'));
expect(io.of('/tenant-a').adapter).toBe(adapters.get('/tenant-a'));
expect(new Set(adapters.values()).size).toBe(4);
expect([...(adapters.get('/')?.sids.keys() ?? [])]).toEqual([rootSocket.id]);
expect([...(adapters.get('/existing')?.sids.keys() ?? [])]).toEqual([existingSocket.id]);
expect([...(adapters.get('/future')?.sids.keys() ?? [])]).toEqual([futureSocket.id]);
expect(adapters.get('/tenant-a')?.sids.size).toBe(1);
await io.close();
});
it('keeps every existing adapter unchanged when replacement construction fails', () => {
const io = new Server('http://localhost');
const root = io.of('/');
const existing = io.of('/existing');
io.adapter(() => new Adapter());
const originalRoot = root.adapter;
const originalExisting = existing.adapter;
expect(() =>
io.adapter((namespace) => {
if (namespace.name === '/existing') throw new Error('factory failed');
return new Adapter();
}),
).toThrow('factory failed');
expect(root.adapter).toBe(originalRoot);
expect(existing.adapter).toBe(originalExisting);
});
it('rejects one adapter instance shared by multiple namespaces without partial replacement', () => {
const io = new Server('http://localhost');
const root = io.of('/');
const existing = io.of('/existing');
const originalRoot = root.adapter;
const originalExisting = existing.adapter;
const shared = new Adapter();
expect(() => io.adapter(() => shared)).toThrow(
'adapter factory must return a fresh instance for each namespace',
);
expect(root.adapter).toBe(originalRoot);
expect(existing.adapter).toBe(originalExisting);
});
it('reports dynamic adapter construction failure and lets the client retry', async () => {
const io = new Server('http://localhost');
let childAttempts = 0;
const created: string[] = [];
io.adapter((namespace) => {
if (namespace.name === '/tenant-a' && childAttempts++ < 2) {
if (childAttempts === 1) throw new Error('child adapter failed');
throw 'child adapter failed again';
}
created.push(namespace.name);
return new Adapter();
});
io.of(/^\/tenant-/);
const client = io.connect('/tenant-a');
await expect(receive(client, 'connect_error')).resolves.toMatchObject({
message: 'child adapter failed',
});
const secondError = receive(client, 'connect_error');
client.connect();
await expect(secondError).resolves.toMatchObject({ message: 'child adapter failed again' });
const connected = receive(client, 'connect');
client.connect();
await connected;
expect(created).toEqual(['/', '/tenant-a']);
expect(io.of('/tenant-a').adapter).toBeInstanceOf(Adapter);
});
it('does not register a future static namespace when its adapter construction fails', () => {
const io = new Server('http://localhost');
let attempts = 0;
io.adapter((namespace) => {
if (namespace.name === '/future' && attempts++ === 0) throw new Error('future failed');
return new Adapter();
});
expect(() => io.of('/future')).toThrow('future failed');
const future = io.of('/future');
expect(attempts).toBe(2);
expect(future.adapter).toBeInstanceOf(Adapter);
});
it('rejects reusing an existing adapter for a future namespace', () => {
const io = new Server('http://localhost');
const shared = new Adapter();
io.adapter(() => shared);
expect(() => io.of('/future')).toThrow(
'adapter factory must return a fresh instance for each namespace',
);
io.adapter(() => new Adapter());
expect(io.of('/future').adapter).toBeInstanceOf(Adapter);
});
it('closes adapter registration at the first connection attempt, including rejection', async () => {
const io = new Server('http://localhost');
const client = io.connect('/missing');
await expect(receive(client, 'connect_error')).resolves.toMatchObject({
message: 'Invalid namespace',
});
expect(() => io.adapter(() => new Adapter())).toThrow(
'adapter must be registered before the first connection attempt',
);
});
it.each(['client', 'server', 'manager', 'close'] as const)(
'signals whole-socket removal once for the %s teardown path',
async (path) => {
const io = new Server(`http://localhost:${4100 + path.length}`);
const adapters = registerRemovalAdapters(io);
const client = io.connect();
const socket = await io.nextConnection();
socket.join('room');
const order: string[] = [];
socket.on('disconnecting', () => order.push('disconnecting'));
socket.on('disconnect', () => order.push('disconnect'));
const adapter = adapters.get('/');
if (!adapter) throw new Error('root adapter was not created');
adapter.onRemove = () => order.push('removeSocket');
const { disconnected } = observeDisconnect(socket);
if (path === 'client') client.disconnect();
if (path === 'server') socket.disconnect();
if (path === 'manager') socket.disconnect(true);
if (path === 'close') await io.close();
await disconnected;
client.disconnect();
socket.disconnect(true);
await io.close();
expect(order).toEqual(['disconnecting', 'removeSocket', 'disconnect']);
expect(adapter.removals).toEqual([
{
sid: socket.id,
hasRoomMember: false,
hasSidEntry: false,
inNamespaceRoster: true,
},
]);
},
);
it('signals cleanup once for rejected and cancelled admission without lifecycle events', async () => {
for (const mode of ['rejected', 'cancelled'] as const) {
const io = new Server(`http://localhost:${4200 + mode.length}`);
const adapters = registerRemovalAdapters(io);
let provisional: ServerSocketContract | undefined;
const lifecycle: string[] = [];
let markMiddlewareEntered!: () => void;
const middlewareEntered = new Promise<void>((resolve) => {
markMiddlewareEntered = resolve;
});
io.use((socket, next) => {
provisional = socket;
socket.on('disconnecting', () => lifecycle.push('disconnecting'));
socket.on('disconnect', () => lifecycle.push('disconnect'));
socket.join('temporary');
markMiddlewareEntered();
if (mode === 'rejected') next(new Error('denied'));
});
const client = io.connect();
if (mode === 'rejected') {
await expect(receive(client, 'connect_error')).resolves.toMatchObject({ message: 'denied' });
} else {
await middlewareEntered;
client.disconnect();
}
expect(provisional).toBeDefined();
expect(lifecycle).toEqual([]);
expect(adapters.get('/')?.removals).toEqual([
{
sid: provisional?.id,
hasRoomMember: false,
hasSidEntry: false,
inNamespaceRoster: false,
},
]);
client.disconnect();
await io.close();
expect(adapters.get('/')?.removals).toHaveLength(1);
}
});