node-serialport-bindings-cpp/lib/unix-read.test.ts
Nerivec 5d157b180e
Bring everything up-to-date (#194)
Ended up refactoring quite a bit while investigating a bug (_seems
prebuilds are failing on more and more setups_), figured a recap PR
might help...

- Fix outdated packages (that don't require more refactoring)
- Update node version in `test` to match that of `prebuild` action
- Update napi_versions to 8
- Update prebuildify target to 20
- Update (split) matrix
  - force tags where missing
  - add `win32-arm64`
- fix outdated image for linux-x64 (`centos7-devtoolset7` =>
`almalinux-devtoolset11`)
  - switch `linux-arm64-lts` to `linux-arm64`
- Update artifact names with `matrix.name` to avoid conflicts (merged on
download)
- Fix lint in tests

TODO:

- **Currently, this removes the `semantic-release` step in favor of a
`tree prebuilds` to finish debugging ([naming seems to have
changed](https://www.npmjs.com/package/@serialport/bindings-cpp?activeTab=code):
impact?), see action run:**
  - https://github.com/Nerivec/bindings-cpp/actions/runs/12331013585
- Can probably also add `dockcross/linux-riscv64`, which is becoming
more requested? (although not yet in `prebuild/docker-images`)

_Feel free to update/split as desired._

---------

Co-authored-by: Francis Gulotta <wizard@roborooter.com>
2024-12-23 14:38:28 -05:00

111 lines
4.2 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
import { BindingsError } from '.'
import { assert, shouldReject } from '../test/assert'
import { LinuxPortBinding } from './linux'
import { unixRead } from './unix-read'
const makeFsRead =
(bytesRead: number, fill: number): any =>
(fd: number, buffer: Buffer, offset: number, length: number) => {
buffer.fill(fill, offset, Math.min(length, bytesRead))
return {
buffer,
bytesRead,
}
}
const makeFsReadError = (code: string) => {
const err = new Error(`Error: ${code}`)
;(err as any).code = code
return () => {
throw err
}
}
// eslint-disable-next-line @typescript-eslint/ban-types
const sequenceCalls = (...functions: Function[]) => {
const funcs = [...functions]
return (...args: any[]) => {
const func = funcs.shift()
if (func) {
return func(...args)
} else {
throw new Error('"sequenceCalls" has no more functions')
}
}
}
const makeMockBinding = (): LinuxPortBinding => {
return {
isOpen: true,
fd: 1,
poller: {
once(event: any, func: () => void) {
setImmediate(func)
},
},
} as any
}
describe('unixRead', () => {
let mock: LinuxPortBinding
beforeEach(() => {
mock = makeMockBinding()
})
it('rejects when not open', async () => {
(mock as any).isOpen = false
const readBuffer = Buffer.alloc(8, 0)
await shouldReject(unixRead({ binding: mock, buffer: readBuffer, offset: 0, length: 8, fsReadAsync: makeFsRead(8, 255) }))
})
it('handles reading the requested number of bytes', async () => {
const readBuffer = Buffer.alloc(8, 0)
const { bytesRead, buffer } = await unixRead({ binding: mock, buffer: readBuffer, offset: 0, length: 8, fsReadAsync: makeFsRead(8, 255) })
assert.strictEqual(bytesRead, 8)
assert.strictEqual(buffer, readBuffer)
assert.deepStrictEqual(buffer, Buffer.alloc(8, 255))
})
it('handles reading less than requested number of bytes', async () => {
const readBuffer = Buffer.alloc(8, 0)
const { bytesRead, buffer } = await unixRead({ binding: mock, buffer: readBuffer, offset: 0, length: 8, fsReadAsync: makeFsRead(4, 255) })
assert.strictEqual(bytesRead, 4)
assert.strictEqual(buffer, readBuffer)
assert.deepStrictEqual(buffer, Buffer.from([255, 255, 255, 255, 0, 0, 0, 0]))
})
it('handles reading 0 bytes then requested number of bytes', async () => {
const readBuffer = Buffer.alloc(8, 0)
const fsReadAsync = sequenceCalls(makeFsRead(0, 0), makeFsRead(8, 255))
const { bytesRead, buffer } = await unixRead({ binding: mock, buffer: readBuffer, offset: 0, length: 8, fsReadAsync })
assert.strictEqual(bytesRead, 8)
assert.strictEqual(buffer, readBuffer)
assert.deepStrictEqual(buffer, Buffer.alloc(8, 255))
})
it('handles retryable errors', async () => {
const readBuffer = Buffer.alloc(8, 0)
const fsReadAsync = sequenceCalls(makeFsReadError('EAGAIN'), makeFsReadError('EWOULDBLOCK'), makeFsReadError('EINTR'), makeFsRead(8, 255))
const { bytesRead, buffer } = await unixRead({ binding: mock, buffer: readBuffer, offset: 0, length: 8, fsReadAsync })
assert.strictEqual(bytesRead, 8)
assert.strictEqual(buffer, readBuffer)
assert.deepStrictEqual(buffer, Buffer.alloc(8, 255))
})
it('rejects read errors', async () => {
const readBuffer = Buffer.alloc(8, 0)
await shouldReject(unixRead({ binding: mock, buffer: readBuffer, offset: 0, length: 8, fsReadAsync: makeFsReadError('Error') }))
})
it('rejects a canceled error if port closes after read a retryable error', async () => {
const readBuffer = Buffer.alloc(8, 0)
const fsReadAsync: any = () => {
(mock as any).isOpen = false
makeFsReadError('EAGAIN')()
}
const err = await shouldReject(unixRead({ binding: mock, buffer: readBuffer, offset: 0, length: 8, fsReadAsync })) as BindingsError
assert.isTrue(err.canceled)
})
it('rejects a disconnected error when fsread errors a disconnect error', async () => {
const readBuffer = Buffer.alloc(8, 0)
const fsReadAsync = makeFsReadError('EBADF')
const err = await shouldReject(unixRead({ binding: mock, buffer: readBuffer, offset: 0, length: 8, fsReadAsync })) as Error & {disconnect: boolean}
assert.isTrue(err.disconnect)
})
})