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 mc = require('../')
|
||||||
const os = require('os')
|
const os = require('os')
|
||||||
const net = require('net')
|
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const assert = require('power-assert')
|
const assert = require('power-assert')
|
||||||
const SURVIVE_TIME = 10000
|
const SURVIVE_TIME = 10000
|
||||||
|
@ -13,14 +12,7 @@ const Wrap = require('minecraft-wrap').Wrap
|
||||||
|
|
||||||
const download = util.promisify(require('minecraft-wrap').download)
|
const download = util.promisify(require('minecraft-wrap').download)
|
||||||
|
|
||||||
const getPort = () => new Promise(resolve => {
|
const { getPort } = require('./common/util')
|
||||||
const server = net.createServer()
|
|
||||||
server.listen(0, '127.0.0.1')
|
|
||||||
server.on('listening', () => {
|
|
||||||
const { port } = server.address()
|
|
||||||
server.close(() => resolve(port))
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
for (const supportedVersion of mc.supportedVersions) {
|
for (const supportedVersion of mc.supportedVersions) {
|
||||||
let PORT = null
|
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 getFieldInfo = require('protodef').utils.getFieldInfo
|
||||||
const getField = require('protodef').utils.getField
|
const getField = require('protodef').utils.getField
|
||||||
|
|
||||||
|
const { getPort } = require('./common/util')
|
||||||
|
|
||||||
function evalCount (count, fields) {
|
function evalCount (count, fields) {
|
||||||
if (fields[count.field] in count.map) { return count.map[fields[count.field]] }
|
if (fields[count.field] in count.map) { return count.map[fields[count.field]] }
|
||||||
return count.default
|
return count.default
|
||||||
|
@ -188,25 +190,30 @@ function getValue (_type, packet) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const supportedVersion of mc.supportedVersions) {
|
for (const supportedVersion of mc.supportedVersions) {
|
||||||
const PORT = Math.round(30000 + Math.random() * 20000)
|
let PORT
|
||||||
|
|
||||||
const mcData = require('minecraft-data')(supportedVersion)
|
const mcData = require('minecraft-data')(supportedVersion)
|
||||||
const version = mcData.version
|
const version = mcData.version
|
||||||
const packets = mcData.protocol
|
const packets = mcData.protocol
|
||||||
|
|
||||||
describe('packets ' + version.minecraftVersion, function () {
|
describe('packets ' + version.minecraftVersion, function () {
|
||||||
let client, server, serverClient
|
let client, server, serverClient
|
||||||
before(function (done) {
|
before(async function () {
|
||||||
|
PORT = await getPort()
|
||||||
server = new Server(version.minecraftVersion)
|
server = new Server(version.minecraftVersion)
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
console.log(`Using port for tests: ${PORT}`)
|
||||||
server.once('listening', function () {
|
server.once('listening', function () {
|
||||||
server.once('connection', function (c) {
|
server.once('connection', function (c) {
|
||||||
serverClient = c
|
serverClient = c
|
||||||
done()
|
resolve()
|
||||||
})
|
})
|
||||||
client = new Client(false, version.minecraftVersion)
|
client = new Client(false, version.minecraftVersion)
|
||||||
client.setSocket(net.connect(PORT, 'localhost'))
|
client.setSocket(net.connect(PORT, 'localhost'))
|
||||||
})
|
})
|
||||||
server.listen(PORT, 'localhost')
|
server.listen(PORT, 'localhost')
|
||||||
})
|
})
|
||||||
|
})
|
||||||
after(function (done) {
|
after(function (done) {
|
||||||
client.on('end', function () {
|
client.on('end', function () {
|
||||||
server.on('close', done)
|
server.on('close', done)
|
||||||
|
|
|
@ -4,6 +4,8 @@ const mc = require('../')
|
||||||
const assert = require('power-assert')
|
const assert = require('power-assert')
|
||||||
const { once } = require('events')
|
const { once } = require('events')
|
||||||
|
|
||||||
|
const { getPort } = require('./common/util')
|
||||||
|
|
||||||
const w = {
|
const w = {
|
||||||
piglin_safe: {
|
piglin_safe: {
|
||||||
type: 'byte',
|
type: 'byte',
|
||||||
|
@ -60,11 +62,17 @@ const w = {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const supportedVersion of mc.supportedVersions) {
|
for (const supportedVersion of mc.supportedVersions) {
|
||||||
const PORT = Math.round(30000 + Math.random() * 20000)
|
let PORT
|
||||||
const mcData = require('minecraft-data')(supportedVersion)
|
const mcData = require('minecraft-data')(supportedVersion)
|
||||||
const version = mcData.version
|
const version = mcData.version
|
||||||
|
|
||||||
describe('mc-server ' + version.minecraftVersion, function () {
|
describe('mc-server ' + version.minecraftVersion, function () {
|
||||||
|
|
||||||
|
this.beforeAll(async function() {
|
||||||
|
PORT = await getPort()
|
||||||
|
console.log(`Using port for tests: ${PORT}`)
|
||||||
|
})
|
||||||
|
|
||||||
this.timeout(5000)
|
this.timeout(5000)
|
||||||
it('starts listening and shuts down cleanly', function (done) {
|
it('starts listening and shuts down cleanly', function (done) {
|
||||||
const server = mc.createServer({
|
const server = mc.createServer({
|
||||||
|
|
Loading…
Reference in a new issue