Skip to content

Commit 5561f41

Browse files
committed
wip: small fixes
socketCleanupFactory maintains a set of sockets it tracks. So it will hold sockets in memory. It's not needed since its only purpose is to clean up sockets during tests if they fail the test. We want to track each connection and stream on the server side and make sure they finish before moving to the next step of the loop.
1 parent 229f4d9 commit 5561f41

File tree

1 file changed

+28
-35
lines changed

1 file changed

+28
-35
lines changed

scripts/memtest.mjs

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,6 @@ async function generateKeyHMAC() {
2929
return key;
3030
}
3131

32-
function socketCleanupFactory() {
33-
const sockets = new Set();
34-
return {
35-
extractSocket: (thing) => sockets.add(thing.socket),
36-
stopSockets: async () => {
37-
const stopProms = [];
38-
for (const socket of sockets) {
39-
stopProms.push(socket.stop({ force: true }));
40-
}
41-
await Promise.all(stopProms);
42-
},
43-
sockets,
44-
};
45-
}
46-
4732
async function signHMAC(key, data) {
4833
const cryptoKey = await webcrypto.subtle.importKey(
4934
'raw',
@@ -316,15 +301,13 @@ const main = async () => {
316301
),
317302
]);
318303
const key = await generateKeyHMAC();
319-
let socketCleanMethods = socketCleanupFactory();
320304
const serverCrypto = {
321305
sign: signHMAC,
322306
verify: verifyHMAC,
323307
};
324308
const clientCrypto = {
325309
randomBytes: (data) => webcrypto.getRandomValues(new Uint8Array(data)),
326310
};
327-
let connectionEventProm = utils.promise();
328311
const tlsConfig = await generateTLSConfig();
329312
const server = new QUICServer({
330313
crypto: {
@@ -338,9 +321,31 @@ const main = async () => {
338321
verifyPeer: false,
339322
},
340323
});
341-
socketCleanMethods.extractSocket(server);
342-
server.addEventListener(events.EventQUICServerConnection.name, (e) =>
343-
connectionEventProm.resolveP(e),
324+
let activeStream = undefined;
325+
let activeConn = undefined;
326+
// Lets keep all the handling in once place but track when it is done with promises
327+
server.addEventListener(events.EventQUICServerConnection.name, (e) =>{
328+
const conn = e.detail;
329+
conn.addEventListener(
330+
events.EventQUICConnectionStream.name,
331+
(streamEvent) => {
332+
const stream = streamEvent.detail;
333+
const streamProm = stream.readable.pipeTo(stream.writable);
334+
if (activeStream != null) throw Error('Active stream should be null')
335+
activeStream = streamProm;
336+
},
337+
{ 'once': true },
338+
);
339+
if (activeConn != null) throw Error('Active stream should be null')
340+
activeConn = utils.promise();
341+
conn.addEventListener(
342+
events.EventQUICClientDestroyed.name,
343+
() => {
344+
activeConn.resolveP()
345+
},
346+
{ once: true},
347+
);
348+
}
344349
);
345350
await server.start({ host: '127.0.0.1' });
346351

@@ -350,7 +355,6 @@ const main = async () => {
350355
// if (i % 500 == 0) console.error('loop', i);
351356
console.error('loop', i);
352357

353-
connectionEventProm = utils.promise();
354358
const client = await QUICClient.createQUICClient({
355359
host: '127.0.0.1',
356360
port: server.port,
@@ -363,19 +367,6 @@ const main = async () => {
363367
verifyPeer: false,
364368
},
365369
});
366-
socketCleanMethods.extractSocket(client);
367-
const conn = (await connectionEventProm.p).detail;
368-
let activeStream = undefined;
369-
conn.addEventListener(
370-
events.EventQUICConnectionStream.name,
371-
(streamEvent) => {
372-
const stream = streamEvent.detail;
373-
const streamProm = stream.readable.pipeTo(stream.writable);
374-
activeStream = streamProm;
375-
},
376-
{ 'once': true },
377-
);
378-
379370
const stream = client.connection.newStream();
380371
const writer = stream.writable.getWriter();
381372
await writer.write(data);
@@ -384,8 +375,10 @@ const main = async () => {
384375
// do nothing
385376
}
386377
await activeStream;
387-
await conn.stop({ force: true });
388378
await client.destroy({ force: true });
379+
await activeConn;
380+
activeConn = undefined;
381+
activeStream = undefined;
389382
}
390383

391384
await server.stop({ force: true });

0 commit comments

Comments
 (0)