Skip to content

Commit

Permalink
Fix flaky tests (#144)
Browse files Browse the repository at this point in the history
* Allow LAN connections in relay tests

* Add missing error handler

* Don't throw in `holepunch()` on client

* Tear down relay servers on test end

* Remove faulty relay socket check

* Bring back relay check

* Formatting
  • Loading branch information
kasperisager authored Oct 6, 2023
1 parent daecd1e commit 62506a7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 83 deletions.
1 change: 1 addition & 0 deletions lib/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ function relayConnection (c, relayThrough, payload, hs) {
onabort(null)
return
}

c.relayPaired = true

const {
Expand Down
4 changes: 2 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,11 @@ module.exports = class Server extends EventEmitter {
return
}

hs.relayPaired = true

if (hs.prepunching) clearTimeout(hs.prepunching)
hs.prepunching = null

hs.relayPaired = true

const {
remotePort,
remoteHost,
Expand Down
10 changes: 6 additions & 4 deletions test/connections.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,12 @@ test('relayed connection', async function (t) {
socket.end()
})

socket.on('open', () => {
t.pass('client connected')
socket.end()
})
socket
.on('error', () => {})
.on('open', () => {
t.pass('client connected')
socket.end()
})
})

test('relayed connection on same node', async function (t) {
Expand Down
120 changes: 43 additions & 77 deletions test/relaying.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('relay connections through node, client side', async function (t) {
const lc = t.test('socket lifecycle')
lc.plan(5)

const aServer = a.createServer({ shareLocalAddress: false }, function (socket) {
const aServer = a.createServer(function (socket) {
lc.pass('server socket opened')
socket
.on('data', (data) => {
Expand All @@ -33,18 +33,17 @@ test('relay connections through node, client side', async function (t) {
}
})

const bServer = b.createServer({ shareLocalAddress: false }, function (socket) {
t.teardown(() => relay.close())

const bServer = b.createServer(function (socket) {
const session = relay.accept(socket, { id: socket.remotePublicKey })
session
.on('error', (err) => t.comment(err.message))
})

await bServer.listen()

const aSocket = c.connect(aServer.publicKey, {
localConnection: false,
relayThrough: bServer.publicKey
})
const aSocket = c.connect(aServer.publicKey, { relayThrough: bServer.publicKey })

aSocket
.on('open', () => {
Expand Down Expand Up @@ -72,7 +71,7 @@ test('relay connections through node, client side, client aborts hole punch', as
const lc = t.test('socket lifecycle')
lc.plan(5)

const aServer = a.createServer({ shareLocalAddress: false }, function (socket) {
const aServer = a.createServer(function (socket) {
lc.pass('server socket opened')
socket
.on('data', (data) => {
Expand All @@ -92,20 +91,17 @@ test('relay connections through node, client side, client aborts hole punch', as
}
})

const bServer = b.createServer({ shareLocalAddress: false }, function (socket) {
t.teardown(() => relay.close())

const bServer = b.createServer(function (socket) {
const session = relay.accept(socket, { id: socket.remotePublicKey })
session
.on('error', (err) => t.comment(err.message))
})

await bServer.listen()

const aSocket = c.connect(aServer.publicKey, {
fastOpen: false,
localConnection: false,
holepunch: () => false,
relayThrough: bServer.publicKey
})
const aSocket = c.connect(aServer.publicKey, { holepunch: () => false, relayThrough: bServer.publicKey })

aSocket
.on('open', () => {
Expand Down Expand Up @@ -133,7 +129,7 @@ test('relay connections through node, client side, server aborts hole punch', as
const lc = t.test('socket lifecycle')
lc.plan(5)

const aServer = a.createServer({ shareLocalAddress: false, holepunch: () => false }, function (socket) {
const aServer = a.createServer({ holepunch: () => false }, function (socket) {
lc.pass('server socket opened')
socket
.on('data', (data) => {
Expand All @@ -153,19 +149,17 @@ test('relay connections through node, client side, server aborts hole punch', as
}
})

const bServer = b.createServer({ shareLocalAddress: false }, function (socket) {
t.teardown(() => relay.close())

const bServer = b.createServer(function (socket) {
const session = relay.accept(socket, { id: socket.remotePublicKey })
session
.on('error', (err) => t.comment(err.message))
})

await bServer.listen()

const aSocket = c.connect(aServer.publicKey, {
fastOpen: false,
localConnection: false,
relayThrough: bServer.publicKey
})
const aSocket = c.connect(aServer.publicKey, { relayThrough: bServer.publicKey })

aSocket
.on('open', () => {
Expand Down Expand Up @@ -199,18 +193,17 @@ test('relay connections through node, server side', async function (t) {
}
})

const aServer = a.createServer({ shareLocalAddress: false }, function (socket) {
t.teardown(() => relay.close())

const aServer = a.createServer(function (socket) {
const session = relay.accept(socket, { id: socket.remotePublicKey })
session
.on('error', (err) => t.comment(err.message))
})

await aServer.listen()

const bServer = b.createServer({
shareLocalAddress: false,
relayThrough: aServer.publicKey
}, function (socket) {
const bServer = b.createServer({ relayThrough: aServer.publicKey }, function (socket) {
lc.pass('server socket opened')
socket
.on('data', (data) => {
Expand All @@ -224,9 +217,7 @@ test('relay connections through node, server side', async function (t) {

await bServer.listen()

const bSocket = c.connect(bServer.publicKey, {
localConnection: false
})
const bSocket = c.connect(bServer.publicKey)

bSocket
.on('open', () => {
Expand Down Expand Up @@ -260,18 +251,17 @@ test('relay connections through node, server side, client aborts hole punch', as
}
})

const aServer = a.createServer({ shareLocalAddress: false }, function (socket) {
t.teardown(() => relay.close())

const aServer = a.createServer(function (socket) {
const session = relay.accept(socket, { id: socket.remotePublicKey })
session
.on('error', (err) => t.comment(err.message))
})

await aServer.listen()

const bServer = b.createServer({
shareLocalAddress: false,
relayThrough: aServer.publicKey
}, function (socket) {
const bServer = b.createServer({ relayThrough: aServer.publicKey }, function (socket) {
lc.pass('server socket opened')
socket
.on('data', (data) => {
Expand All @@ -285,11 +275,7 @@ test('relay connections through node, server side, client aborts hole punch', as

await bServer.listen()

const bSocket = c.connect(bServer.publicKey, {
fastOpen: false,
localConnection: false,
holepunch: () => false
})
const bSocket = c.connect(bServer.publicKey, { holepunch: () => false })

bSocket
.on('open', () => {
Expand Down Expand Up @@ -323,19 +309,17 @@ test('relay connections through node, server side, server aborts hole punch', as
}
})

const aServer = a.createServer({ shareLocalAddress: false }, function (socket) {
t.teardown(() => relay.close())

const aServer = a.createServer(function (socket) {
const session = relay.accept(socket, { id: socket.remotePublicKey })
session
.on('error', (err) => t.comment(err.message))
})

await aServer.listen()

const bServer = b.createServer({
shareLocalAddress: false,
holepunch: () => false,
relayThrough: aServer.publicKey
}, function (socket) {
const bServer = b.createServer({ holepunch: () => false, relayThrough: aServer.publicKey }, function (socket) {
lc.pass('server socket opened')
socket
.on('data', (data) => {
Expand All @@ -349,10 +333,7 @@ test('relay connections through node, server side, server aborts hole punch', as

await bServer.listen()

const bSocket = c.connect(bServer.publicKey, {
fastOpen: false,
localConnection: false
})
const bSocket = c.connect(bServer.publicKey)

bSocket
.on('open', () => {
Expand Down Expand Up @@ -386,7 +367,9 @@ test('relay connections through node, client and server side', async function (t
}
})

const aServer = a.createServer({ shareLocalAddress: false }, function (socket) {
t.teardown(() => relay.close())

const aServer = a.createServer(function (socket) {
const session = relay.accept(socket, { id: socket.remotePublicKey })
session
.on('error', (err) => t.comment(err.message))
Expand All @@ -395,7 +378,6 @@ test('relay connections through node, client and server side', async function (t
await aServer.listen()

const bServer = b.createServer({
shareLocalAddress: false,
relayThrough: aServer.publicKey
}, function (socket) {
lc.pass('server socket opened')
Expand All @@ -411,11 +393,7 @@ test('relay connections through node, client and server side', async function (t

await bServer.listen()

const bSocket = c.connect(bServer.publicKey, {
fastOpen: false,
localConnection: false,
relayThrough: aServer.publicKey
})
const bSocket = c.connect(bServer.publicKey, { relayThrough: aServer.publicKey })

bSocket
.on('open', () => {
Expand Down Expand Up @@ -443,7 +421,7 @@ test.skip('relay several connections through node with pool', async function (t)
const lc = t.test('socket lifecycle')
lc.plan(10)

const aServer = a.createServer({ shareLocalAddress: false }, function (socket) {
const aServer = a.createServer(function (socket) {
lc.pass('server socket opened')
socket
.on('data', (data) => {
Expand All @@ -463,7 +441,9 @@ test.skip('relay several connections through node with pool', async function (t)
}
})

const bServer = b.createServer({ shareLocalAddress: false }, function (socket) {
t.teardown(() => relay.close())

const bServer = b.createServer(function (socket) {
const session = relay.accept(socket, { id: socket.remotePublicKey })
session
.on('error', (err) => t.comment(err.message))
Expand All @@ -473,12 +453,7 @@ test.skip('relay several connections through node with pool', async function (t)

const pool = c.pool()

const aSocket = c.connect(aServer.publicKey, {
fastOpen: false,
localConnection: false,
relayThrough: bServer.publicKey,
pool
})
const aSocket = c.connect(aServer.publicKey, { relayThrough: bServer.publicKey, pool })

aSocket
.on('open', () => {
Expand All @@ -487,12 +462,7 @@ test.skip('relay several connections through node with pool', async function (t)
.on('close', () => {
lc.pass('1st client socket closed')

const aSocket = c.connect(aServer.publicKey, {
fastOpen: false,
localConnection: false,
relayThrough: bServer.publicKey,
pool
})
const aSocket = c.connect(aServer.publicKey, { relayThrough: bServer.publicKey, pool })

aSocket
.on('open', () => {
Expand Down Expand Up @@ -522,13 +492,13 @@ test.skip('server does not support connection relaying', async function (t) {
const lc = t.test('socket lifecycle')
lc.plan(4)

const aServer = a.createServer({ shareLocalAddress: false }, function () {
const aServer = a.createServer(function () {
t.fail()
})

await aServer.listen()

const bServer = b.createServer({ shareLocalAddress: false }, function (socket) {
const bServer = b.createServer(function (socket) {
lc.pass('server socket opened')
socket.on('error', () => {
lc.pass('server socket timed out')
Expand All @@ -537,11 +507,7 @@ test.skip('server does not support connection relaying', async function (t) {

await bServer.listen()

const aSocket = c.connect(aServer.publicKey, {
fastOpen: false,
localConnection: false,
relayThrough: bServer.publicKey
})
const aSocket = c.connect(aServer.publicKey, { relayThrough: bServer.publicKey })

aSocket.on('error', () => {
lc.pass('client socket timed out')
Expand Down

0 comments on commit 62506a7

Please sign in to comment.