|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +if (!common.hasQuic) |
| 5 | + common.skip('missing quic'); |
| 6 | + |
| 7 | +const assert = require('assert'); |
| 8 | +const { createSocket } = require('quic'); |
| 9 | +const fixtures = require('../common/fixtures'); |
| 10 | +const key = fixtures.readKey('agent1-key.pem', 'binary'); |
| 11 | +const cert = fixtures.readKey('agent1-cert.pem', 'binary'); |
| 12 | +const ca = fixtures.readKey('ca1-cert.pem', 'binary'); |
| 13 | + |
| 14 | +const kServerName = 'agent2'; |
| 15 | +const kALPN = 'zzz'; |
| 16 | +// Allow error for 500 milliseconds more. |
| 17 | +const error = common.platformTimeout(500); |
| 18 | +const idleTimeout = common.platformTimeout(1000); |
| 19 | + |
| 20 | +// Test client idle timeout. |
| 21 | +{ |
| 22 | + let client; |
| 23 | + const server = createSocket({ port: 0 }); |
| 24 | + server.listen({ |
| 25 | + key, |
| 26 | + cert, |
| 27 | + ca, |
| 28 | + alpn: kALPN, |
| 29 | + }); |
| 30 | + |
| 31 | + server.on('session', common.mustCall()); |
| 32 | + |
| 33 | + server.on('ready', common.mustCall(() => { |
| 34 | + client = createSocket({ |
| 35 | + port: 0, |
| 36 | + client: { |
| 37 | + key, |
| 38 | + cert, |
| 39 | + ca, |
| 40 | + alpn: kALPN, |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + const start = Date.now(); |
| 45 | + |
| 46 | + const clientSession = client.connect({ |
| 47 | + address: 'localhost', |
| 48 | + port: server.address.port, |
| 49 | + servername: kServerName, |
| 50 | + idleTimeout, |
| 51 | + }); |
| 52 | + |
| 53 | + clientSession.on('close', common.mustCall(() => { |
| 54 | + client.close(); |
| 55 | + server.close(); |
| 56 | + assert(Date.now() - start < idleTimeout + error); |
| 57 | + })); |
| 58 | + })); |
| 59 | +} |
| 60 | + |
| 61 | +// Test server idle timeout. |
| 62 | +{ |
| 63 | + let client; |
| 64 | + let start; |
| 65 | + const server = createSocket({ port: 0 }); |
| 66 | + server.listen({ |
| 67 | + key, |
| 68 | + cert, |
| 69 | + ca, |
| 70 | + alpn: kALPN, |
| 71 | + idleTimeout, |
| 72 | + }); |
| 73 | + |
| 74 | + server.on('session', common.mustCall(() => { |
| 75 | + client.close(); |
| 76 | + server.close(); |
| 77 | + assert(Date.now() - start < idleTimeout + error); |
| 78 | + })); |
| 79 | + |
| 80 | + server.on('ready', common.mustCall(() => { |
| 81 | + client = createSocket({ |
| 82 | + port: 0, |
| 83 | + client: { |
| 84 | + key, |
| 85 | + cert, |
| 86 | + ca, |
| 87 | + alpn: kALPN, |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + |
| 92 | + start = Date.now(); |
| 93 | + const clientSession = client.connect({ |
| 94 | + address: 'localhost', |
| 95 | + port: server.address.port, |
| 96 | + servername: kServerName, |
| 97 | + }); |
| 98 | + |
| 99 | + clientSession.on('close', common.mustCall()); |
| 100 | + })); |
| 101 | +} |
0 commit comments