mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-26 02:47:17 +00:00
fix: bindings now error when closed during empty writes (#1872)
Also fix tests not running!
This commit is contained in:
parent
524a272900
commit
9d01492968
5 changed files with 60 additions and 68 deletions
|
|
@ -119,6 +119,8 @@ The in progress writes must error when the port is closed with an error object t
|
|||
|
||||
debug('write', buffer.length, 'bytes')
|
||||
if (!this.isOpen) {
|
||||
debug('write', 'error port is not open')
|
||||
|
||||
throw new Error('Port is not open')
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,6 +38,17 @@ function disconnect(err) {
|
|||
throw err || new Error('Unknown disconnection')
|
||||
}
|
||||
|
||||
function shouldReject(promise, errType = Error, message = 'Should have rejected') {
|
||||
return promise.then(
|
||||
() => {
|
||||
throw new Error(message)
|
||||
},
|
||||
err => {
|
||||
assert.instanceOf(err, errType)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
// All bindings are required to work with an "echo" firmware
|
||||
// The echo firmware should respond with this data when it's
|
||||
// ready to echo. This allows for remote device bootup.
|
||||
|
|
@ -156,22 +167,12 @@ function testBinding(bindingName, Binding, testPort) {
|
|||
})
|
||||
})
|
||||
|
||||
it('throws when not given a path', done => {
|
||||
try {
|
||||
binding.open('')
|
||||
} catch (e) {
|
||||
assert.instanceOf(e, TypeError)
|
||||
done()
|
||||
}
|
||||
it('throws when not given a path', async () => {
|
||||
await shouldReject(binding.open(''), TypeError)
|
||||
})
|
||||
|
||||
it('throws when not given options', done => {
|
||||
try {
|
||||
binding.open('COMBAD')
|
||||
} catch (e) {
|
||||
assert.instanceOf(e, TypeError)
|
||||
done()
|
||||
}
|
||||
it('throws when not given options', async () => {
|
||||
await shouldReject(binding.open('COMBAD'), TypeError)
|
||||
})
|
||||
|
||||
if (!testPort) {
|
||||
|
|
@ -278,15 +279,9 @@ function testBinding(bindingName, Binding, testPort) {
|
|||
})
|
||||
|
||||
describe('#update', () => {
|
||||
it('throws when not given an object', done => {
|
||||
it('throws when not given an object', async () => {
|
||||
const binding = new Binding({ disconnect })
|
||||
|
||||
try {
|
||||
binding.update()
|
||||
} catch (e) {
|
||||
assert.instanceOf(e, TypeError)
|
||||
done()
|
||||
}
|
||||
await shouldReject(binding.update(), TypeError)
|
||||
})
|
||||
|
||||
it('errors asynchronously when not open', done => {
|
||||
|
|
@ -315,22 +310,12 @@ function testBinding(bindingName, Binding, testPort) {
|
|||
|
||||
afterEach(() => binding.close())
|
||||
|
||||
it('throws errors when updating nothing', done => {
|
||||
try {
|
||||
binding.update({})
|
||||
} catch (err) {
|
||||
assert.instanceOf(err, Error)
|
||||
done()
|
||||
}
|
||||
it('throws errors when updating nothing', async () => {
|
||||
await shouldReject(binding.update({}), Error)
|
||||
})
|
||||
|
||||
it('errors when not called with options', done => {
|
||||
try {
|
||||
binding.set(() => {})
|
||||
} catch (e) {
|
||||
assert.instanceOf(e, Error)
|
||||
done()
|
||||
}
|
||||
it('errors when not called with options', async () => {
|
||||
await shouldReject(binding.set(() => {}), Error)
|
||||
})
|
||||
|
||||
it('updates baudRate', () => {
|
||||
|
|
@ -338,30 +323,34 @@ function testBinding(bindingName, Binding, testPort) {
|
|||
})
|
||||
})
|
||||
|
||||
describe('#write', () => {
|
||||
describe.only('#write', () => {
|
||||
it('errors asynchronously when not open', done => {
|
||||
const binding = new Binding({
|
||||
disconnect,
|
||||
})
|
||||
let noZalgo = false
|
||||
binding.write(Buffer.from([])).catch(err => {
|
||||
assert.instanceOf(err, Error)
|
||||
assert(noZalgo)
|
||||
done()
|
||||
})
|
||||
binding
|
||||
.write(Buffer.from([]))
|
||||
.then(
|
||||
data => {
|
||||
console.log({ data })
|
||||
throw new Error('Should have errored')
|
||||
},
|
||||
err => {
|
||||
assert.instanceOf(err, Error)
|
||||
assert(noZalgo)
|
||||
done()
|
||||
}
|
||||
)
|
||||
.catch(done)
|
||||
noZalgo = true
|
||||
})
|
||||
|
||||
it('throws when not given a buffer', done => {
|
||||
it('throws when not given a buffer', async () => {
|
||||
const binding = new Binding({
|
||||
disconnect,
|
||||
})
|
||||
try {
|
||||
binding.write(null)
|
||||
} catch (e) {
|
||||
assert.instanceOf(e, TypeError)
|
||||
done()
|
||||
}
|
||||
await shouldReject(binding.write(null), TypeError)
|
||||
})
|
||||
|
||||
if (!testPort) {
|
||||
|
|
@ -494,16 +483,11 @@ function testBinding(bindingName, Binding, testPort) {
|
|||
noZalgo = true
|
||||
})
|
||||
|
||||
it('throws when not called with options', done => {
|
||||
it('throws when not called with options', async () => {
|
||||
const binding = new Binding({
|
||||
disconnect,
|
||||
})
|
||||
try {
|
||||
binding.set(() => {})
|
||||
} catch (e) {
|
||||
assert.instanceOf(e, TypeError)
|
||||
done()
|
||||
}
|
||||
await shouldReject(binding.set(() => {}), TypeError)
|
||||
})
|
||||
|
||||
if (!testPort) {
|
||||
|
|
@ -65,12 +65,14 @@ class DarwinBinding extends AbstractBinding {
|
|||
}
|
||||
|
||||
async write(buffer) {
|
||||
if (buffer.length === 0) {
|
||||
return
|
||||
}
|
||||
this.writeOperation = super
|
||||
.write(buffer)
|
||||
.then(() => unixWrite.call(this, buffer))
|
||||
.then(() => {
|
||||
if (buffer.length === 0) {
|
||||
return
|
||||
}
|
||||
return unixWrite.call(this, buffer)
|
||||
})
|
||||
.then(() => {
|
||||
this.writeOperation = null
|
||||
})
|
||||
|
|
|
|||
|
|
@ -65,12 +65,14 @@ class LinuxBinding extends AbstractBinding {
|
|||
}
|
||||
|
||||
async write(buffer) {
|
||||
if (buffer.length === 0) {
|
||||
return
|
||||
}
|
||||
this.writeOperation = super
|
||||
.write(buffer)
|
||||
.then(() => unixWrite.call(this, buffer))
|
||||
.then(() => {
|
||||
if (buffer.length === 0) {
|
||||
return
|
||||
}
|
||||
return unixWrite.call(this, buffer)
|
||||
})
|
||||
.then(() => {
|
||||
this.writeOperation = null
|
||||
})
|
||||
|
|
|
|||
|
|
@ -74,12 +74,14 @@ class WindowsBinding extends AbstractBinding {
|
|||
}
|
||||
|
||||
async write(buffer) {
|
||||
if (buffer.length === 0) {
|
||||
return
|
||||
}
|
||||
this.writeOperation = super
|
||||
.write(buffer)
|
||||
.then(() => asyncWrite(this.fd, buffer))
|
||||
.then(() => {
|
||||
if (buffer.length === 0) {
|
||||
return
|
||||
}
|
||||
return asyncWrite(this.fd, buffer)
|
||||
})
|
||||
.then(() => {
|
||||
this.writeOperation = null
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue