Skip to content

Commit

Permalink
find free port in tests like nmp/mineflayer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rom1504 committed Dec 17, 2023
1 parent 0f09674 commit 695f7d6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 12 additions & 0 deletions test/common/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const net = require('net')

const getPort = () => new Promise(resolve => {
const server = net.createServer()
server.listen(0, '127.0.0.1')
server.on('listening', () => {
const { port } = server.address()
server.close(() => resolve(port))
})
})

module.exports = { getPort }
10 changes: 7 additions & 3 deletions test/viewer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ const os = require('os')
const Wrap = require('minecraft-wrap').Wrap

const { firstVersion, lastVersion } = require('./parallel')
const { getPort } = require('./common/util')

const download = require('minecraft-wrap').download

supportedVersions.forEach(function (supportedVersion, i) {
if (!(i >= firstVersion && i <= lastVersion)) { return }

const PORT = Math.round(30000 + Math.random() * 20000)
let PORT = null
const mcData = require('minecraft-data')(supportedVersion)
const version = mcData.version
const MC_SERVER_JAR_DIR = process.env.MC_SERVER_JAR_DIR || os.tmpdir()
Expand All @@ -29,10 +30,13 @@ supportedVersions.forEach(function (supportedVersion, i) {
console.log(line)
})

const TIMEOUT = 5 * 60 * 1000
const TIMEOUT = 2 * 60 * 1000

describe('client ' + version.minecraftVersion, function () {
beforeAll(download.bind(null, version.minecraftVersion, MC_SERVER_JAR), TIMEOUT)
beforeAll(async () => {
download(version.minecraftVersion, MC_SERVER_JAR)
PORT = await getPort()
}, TIMEOUT)

afterAll(function (done) {
wrap.deleteServerData(function (err) {
Expand Down

0 comments on commit 695f7d6

Please sign in to comment.