chore: more testing around stream pool and reading (#1942)

This commit is contained in:
Francis Gulotta 2019-09-24 23:22:06 -04:00 • committed by GitHub
parent 145b906d9c
commit 4c19eb2219
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 10 deletions

View file

@ -532,23 +532,22 @@ function testBinding(bindingName, Binding, testPort) {
}
let binding, buffer
beforeEach(() => {
beforeEach(async () => {
buffer = Buffer.alloc(readyData.length)
binding = new Binding({ disconnect })
return binding.open(testPort, defaultOpenOptions)
await binding.open(testPort, defaultOpenOptions)
})
afterEach(() => binding.close())
it("doesn't throw if the port is open", () => {
return binding.read(buffer, 0, buffer.length)
it("doesn't throw if the port is open", async () => {
await binding.read(buffer, 0, buffer.length)
})
it('returns at maximum the requested number of bytes and the buffer', () => {
return binding.read(buffer, 0, 1).then(({ bytesRead, buffer: returnedBuffer }) => {
assert.equal(bytesRead, 1)
assert.equal(buffer, returnedBuffer)
})
it('returns at maximum the requested number of bytes and the buffer', async () => {
const { bytesRead, buffer: returnedBuffer } = await binding.read(buffer, 0, 1)
assert.equal(bytesRead, 1)
assert.equal(buffer, returnedBuffer)
})
})

View file

@ -358,7 +358,7 @@ SerialPort.prototype._read = function(bytesToRead) {
}
if (!this._pool || this._pool.length - this._pool.used < this._kMinPoolSpace) {
debug('_read', 'discarding the read buffer pool')
debug('_read', 'discarding the read buffer pool because it is below kMinPoolSpace')
this._pool = allocNewReadPool(this.settings.highWaterMark)
}

View file

@ -1,5 +1,6 @@
const chai = require('chai')
const sinon = require('sinon')
const { randomBytes } = require('crypto')
chai.use(require('chai-subset'))
const assert = chai.assert
@ -599,6 +600,14 @@ describe('SerialPort', () => {
done()
})
it('can be called without callback', done => {
const port = new SerialPort('/dev/exists', () => {
assert.equal(port.baudRate, 9600)
port.update({ baudRate: 14400 })
done()
})
})
it('sets the baudRate on the port', done => {
const port = new SerialPort('/dev/exists', () => {
assert.equal(port.baudRate, 9600)
@ -938,6 +947,33 @@ describe('SerialPort', () => {
const data2 = port.read()
assert.deepEqual(Buffer.concat([data1, data2]), Buffer.concat([testData, testData]))
})
it('reads more data than the high water mark', async () => {
const testData = randomBytes((64 * 1024) / 2 + 1)
const port = new SerialPort('/dev/exists', { bindingOptions: { echo: true } })
await new Promise(resolve => port.on('open', resolve))
await new Promise(resolve => port.write(testData, resolve))
await new Promise(resolve => port.once('readable', resolve))
const data1 = port.read()
await new Promise(resolve => port.write(testData, resolve))
await new Promise(resolve => port.once('readable', resolve))
const data2 = port.read()
await new Promise(resolve => port.once('readable', resolve))
const data3 = port.read()
assert.deepEqual(Buffer.concat([data1, data2, data3]), Buffer.concat([testData, testData]))
})
it('reads more data than the high water mark at once', async () => {
const testData = randomBytes(64 * 1024 + 1)
const port = new SerialPort('/dev/exists', { bindingOptions: { echo: true } })
await new Promise(resolve => port.on('open', resolve))
await new Promise(resolve => port.write(testData, resolve))
await new Promise(resolve => port.once('readable', resolve))
const data1 = port.read()
await new Promise(resolve => port.once('readable', resolve))
const data2 = port.read()
assert.deepEqual(Buffer.concat([data1, data2]), testData)
})
})
describe('disconnect close errors', () => {