Skip to content

Commit

Permalink
fixed tests to reflect client being nulled out on disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
joyja committed May 22, 2020
1 parent d5a56bd commit ad85fac
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
6 changes: 2 additions & 4 deletions src/device/__tests__/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const {
Modbus,
ModbusSource,
EthernetIP,
EthernetIPSource
EthernetIPSource,
} = require('../../relations')
const fromUnixTime = require('date-fns/fromUnixTime')

Expand Down Expand Up @@ -386,9 +386,7 @@ describe(`EthernetIP :`, () => {
test(`Disconnect calls client close and connected status becomes false.`, async () => {
ethernetip.client.destroy.mockImplementation(() => {})
await ethernetip.disconnect()
expect(ethernetip.client.destroy).toBeCalledTimes(1)
expect(ethernetip.connected).toBe(false)
ethernetip.client.destroy.mockClear()
})
})

Expand All @@ -397,13 +395,13 @@ describe(`EthernetIPSource: `, () => {
test.todo(`rewrite read tests to mock ethernet-ip module.`)
test(`read reads`, async () => {
Controller.prototype.connect.mockResolvedValueOnce({})
await ethernetip.connect()
ethernetip.client.readTag.mockImplementation(async (tagData) => {
return new Promise((resolve, reject) => {
tagData.value = 123.456
resolve()
})
})
await ethernetip.connect()
const tag = await Tag.create(
'testEthernetIP',
'Test Ethernet IP Tag',
Expand Down
5 changes: 4 additions & 1 deletion src/device/ethernetip.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class EthernetIP extends Model {
}
constructor(selector, checkExists = true) {
super(selector, checkExists)
this.client = new Controller()
}
async init() {
const result = await super.init()
Expand Down Expand Up @@ -90,7 +91,9 @@ class EthernetIP extends Model {
this.retryInterval = clearInterval(this.retryInterval)
logger.info(`Disconnecting from ethernetip device ${this.device.name}`)
const logText = `Closed connection to ethernetip device ${this.device.name}`
this.client.destroy()
if (this.client) {
this.client.destroy()
}
this.client = null
logger.info(logText)
this.connected = false
Expand Down

0 comments on commit ad85fac

Please sign in to comment.