Improve serverTest check logic

try and debug flaky tests
This commit is contained in:
extremeheat 2024-05-26 16:47:40 -04:00 committed by GitHub
parent 10c4b56add
commit 9f22cf70e3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -126,23 +126,30 @@ for (const supportedVersion of mc.supportedVersions) {
version: version.minecraftVersion, version: version.minecraftVersion,
port: PORT port: PORT
}) })
let count = 2 let serverClosed, clientClosed
server.on('connection', function (client) { server.on('connection', function (client) {
client.on('end', function (reason) { client.on('end', function (reason) {
assert.strictEqual(reason, 'LoginTimeout') assert.strictEqual(reason, 'LoginTimeout')
server.close() server.close()
}) })
}) })
server.on('close', resolve) server.on('close', () => {
serverClosed = true
console.log('Server closed')
checkFinish()
})
server.on('listening', function () { server.on('listening', function () {
const client = new mc.Client(false, version.minecraftVersion) const client = new mc.Client(false, version.minecraftVersion)
client.on('end', resolve) client.on('end', () => {
clientClosed = true
console.log('Client closed')
checkFinish()
})
client.connect(PORT, '127.0.0.1') client.connect(PORT, '127.0.0.1')
}) })
function resolve () { function checkFinish () {
count -= 1 if (serverClosed && clientClosed) done()
if (count <= 0) done()
} }
}) })
@ -154,14 +161,19 @@ for (const supportedVersion of mc.supportedVersions) {
version: version.minecraftVersion, version: version.minecraftVersion,
port: PORT port: PORT
}) })
let count = 2 let serverClosed, clientClosed
server.on('connection', function (client) { server.on('connection', function (client) {
client.on('end', function (reason) { client.on('end', function (reason) {
assert.strictEqual(reason, 'KeepAliveTimeout') assert.strictEqual(reason, 'KeepAliveTimeout')
console.log('Server client disconnected')
server.close() server.close()
}) })
}) })
server.on('close', resolve) server.on('close', () => {
serverClosed = true
console.log('Server closed')
checkFinish()
})
server.on('listening', function () { server.on('listening', function () {
const client = mc.createClient({ const client = mc.createClient({
username: 'superpants', username: 'superpants',
@ -170,11 +182,14 @@ for (const supportedVersion of mc.supportedVersions) {
keepAlive: false, keepAlive: false,
version: version.minecraftVersion version: version.minecraftVersion
}) })
client.on('end', resolve) client.on('end', () => {
clientClosed = true
console.log('Client closed')
checkFinish()
})
}) })
function resolve () { function checkFinish () {
count -= 1 if (serverClosed && clientClosed) done()
if (count <= 0) done()
} }
}) })
@ -363,27 +378,36 @@ for (const supportedVersion of mc.supportedVersions) {
version: version.minecraftVersion, version: version.minecraftVersion,
port: PORT port: PORT
}) })
let count = 4 let serverPlayerDisconnected, serverClosed, clientClosed
server.on('connection', function (client) { server.on('connection', function (client) {
client.on('end', function (reason) { client.on('end', function (reason) {
resolve() serverPlayerDisconnected = true
console.log('Server player disconnected')
checkFinish()
server.close() server.close()
}) })
}) })
server.on('close', resolve) server.on('close', () => {
serverClosed = true
console.log('Server closed')
checkFinish()
})
server.on('listening', function () { server.on('listening', function () {
resolve() console.log('Server is listening')
const client = mc.createClient({ const client = mc.createClient({
username: 'lalalal', username: 'lalalal',
host: '127.0.0.1', host: '127.0.0.1',
version: version.minecraftVersion, version: version.minecraftVersion,
port: PORT port: PORT
}) })
client.on('end', resolve) client.on('end', () => {
clientClosed = true
console.log('Client closed')
checkFinish()
})
}) })
function resolve () { function checkFinish () {
count -= 1 if (serverPlayerDisconnected && clientClosed && serverClosed) done()
if (count <= 0) done()
} }
}) })
@ -393,15 +417,22 @@ for (const supportedVersion of mc.supportedVersions) {
version: version.minecraftVersion, version: version.minecraftVersion,
port: PORT port: PORT
}) })
let count = 2 let serverPlayerDisconnected, serverClosed
server.on('playerJoin', function (client) { server.on('playerJoin', function (client) {
console.log('Server got player join')
client.on('end', function (reason) { client.on('end', function (reason) {
assert.strictEqual(reason, 'ServerShutdown') assert.strictEqual(reason, 'ServerShutdown')
resolve() serverPlayerDisconnected = true
console.log('Server player disconnected')
checkFinish()
}) })
client.write('login', loginPacket(client, server)) client.write('login', loginPacket(client, server))
}) })
server.on('close', resolve) server.on('close', () => {
serverClosed = true
console.log('Server closed')
checkFinish()
})
server.on('listening', function () { server.on('listening', function () {
const client = mc.createClient({ const client = mc.createClient({
username: 'lalalal', username: 'lalalal',
@ -410,12 +441,12 @@ for (const supportedVersion of mc.supportedVersions) {
port: PORT port: PORT
}) })
client.on('playerJoin', function () { client.on('playerJoin', function () {
console.log('Client joined')
server.close() server.close()
}) })
}) })
function resolve () { function checkFinish () {
count -= 1 if (serverPlayerDisconnected && serverClosed) done()
if (count <= 0) done()
} }
}) })