mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2024-11-14 19:04:59 -05:00
automatic port "discovery" for packets and server tests (#870)
* automatic port "discovery" for packets and server tests * Semi-colon begone! * move getport to commons file - u9g * Merge code
This commit is contained in:
parent
75e5316e8b
commit
4d5dbb118e
4 changed files with 38 additions and 19 deletions
|
@ -2,7 +2,6 @@
|
|||
|
||||
const mc = require('../')
|
||||
const os = require('os')
|
||||
const net = require('net')
|
||||
const path = require('path')
|
||||
const assert = require('power-assert')
|
||||
const SURVIVE_TIME = 10000
|
||||
|
@ -13,14 +12,7 @@ const Wrap = require('minecraft-wrap').Wrap
|
|||
|
||||
const download = util.promisify(require('minecraft-wrap').download)
|
||||
|
||||
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))
|
||||
})
|
||||
})
|
||||
const { getPort } = require('./common/util')
|
||||
|
||||
for (const supportedVersion of mc.supportedVersions) {
|
||||
let PORT = null
|
||||
|
|
12
test/common/util.js
Normal file
12
test/common/util.js
Normal file
|
@ -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 }
|
|
@ -8,6 +8,8 @@ const assert = require('power-assert')
|
|||
const getFieldInfo = require('protodef').utils.getFieldInfo
|
||||
const getField = require('protodef').utils.getField
|
||||
|
||||
const { getPort } = require('./common/util')
|
||||
|
||||
function evalCount (count, fields) {
|
||||
if (fields[count.field] in count.map) { return count.map[fields[count.field]] }
|
||||
return count.default
|
||||
|
@ -188,24 +190,29 @@ function getValue (_type, packet) {
|
|||
}
|
||||
|
||||
for (const supportedVersion of mc.supportedVersions) {
|
||||
const PORT = Math.round(30000 + Math.random() * 20000)
|
||||
let PORT
|
||||
|
||||
const mcData = require('minecraft-data')(supportedVersion)
|
||||
const version = mcData.version
|
||||
const packets = mcData.protocol
|
||||
|
||||
describe('packets ' + version.minecraftVersion, function () {
|
||||
let client, server, serverClient
|
||||
before(function (done) {
|
||||
before(async function () {
|
||||
PORT = await getPort()
|
||||
server = new Server(version.minecraftVersion)
|
||||
server.once('listening', function () {
|
||||
server.once('connection', function (c) {
|
||||
serverClient = c
|
||||
done()
|
||||
return new Promise((resolve) => {
|
||||
console.log(`Using port for tests: ${PORT}`)
|
||||
server.once('listening', function () {
|
||||
server.once('connection', function (c) {
|
||||
serverClient = c
|
||||
resolve()
|
||||
})
|
||||
client = new Client(false, version.minecraftVersion)
|
||||
client.setSocket(net.connect(PORT, 'localhost'))
|
||||
})
|
||||
client = new Client(false, version.minecraftVersion)
|
||||
client.setSocket(net.connect(PORT, 'localhost'))
|
||||
server.listen(PORT, 'localhost')
|
||||
})
|
||||
server.listen(PORT, 'localhost')
|
||||
})
|
||||
after(function (done) {
|
||||
client.on('end', function () {
|
||||
|
|
|
@ -4,6 +4,8 @@ const mc = require('../')
|
|||
const assert = require('power-assert')
|
||||
const { once } = require('events')
|
||||
|
||||
const { getPort } = require('./common/util')
|
||||
|
||||
const w = {
|
||||
piglin_safe: {
|
||||
type: 'byte',
|
||||
|
@ -60,11 +62,17 @@ const w = {
|
|||
}
|
||||
|
||||
for (const supportedVersion of mc.supportedVersions) {
|
||||
const PORT = Math.round(30000 + Math.random() * 20000)
|
||||
let PORT
|
||||
const mcData = require('minecraft-data')(supportedVersion)
|
||||
const version = mcData.version
|
||||
|
||||
describe('mc-server ' + version.minecraftVersion, function () {
|
||||
|
||||
this.beforeAll(async function() {
|
||||
PORT = await getPort()
|
||||
console.log(`Using port for tests: ${PORT}`)
|
||||
})
|
||||
|
||||
this.timeout(5000)
|
||||
it('starts listening and shuts down cleanly', function (done) {
|
||||
const server = mc.createServer({
|
||||
|
|
Loading…
Reference in a new issue