Skip to content

Commit

Permalink
test(coverage): wrong testings removed for now
Browse files Browse the repository at this point in the history
  • Loading branch information
biancode committed Jun 23, 2024
1 parent aa9b9c3 commit d36b135
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 93 deletions.
6 changes: 5 additions & 1 deletion src/modbus-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ module.exports = function (RED) {
node.stateService.send('STOP')
verboseLog('close node ' + nodeIdentifierName)
node.internalDebugLog('close node ' + nodeIdentifierName)
node.removeAllListeners()

if (node.client) {
if (node.client.isOpen) {
node.client.close(function (err) {
Expand All @@ -670,11 +670,15 @@ module.exports = function (RED) {
verboseLog('connection was closed ' + nodeIdentifierName)
done()
}

node.client.removeAllListeners()
} else {
/* istanbul ignore next */
verboseLog('Connection closed simple ' + nodeIdentifierName)
done()
}

node.removeAllListeners()
})

// handle using as config node
Expand Down
3 changes: 3 additions & 0 deletions src/modbus-getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ module.exports = function (RED) {
}
}
}

/* istanbul ignore next */
function verboseWarn (logMessage) {
if (RED.settings.verbose && node.showWarnings) {
Expand Down Expand Up @@ -178,6 +179,8 @@ module.exports = function (RED) {

node.on('close', function (done) {
mbBasics.setNodeStatusTo('closed', node)
node.resetInputDelayTimer()
node.removeAllListeners()
node.bufferMessageList.clear()
modbusClient.deregisterForModbus(node.id, done)
})
Expand Down
1 change: 1 addition & 0 deletions src/modbus-server.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
<p>Output 2: coils Buffer, type, msg</p>
<p>Output 3: input Buffer, type, msg</p>
<p>Output 4: discrete Buffer, type, msg</p>
<p>Output 5: request msg</p>
<br>
<p>Input:
On injecting a special payload, you can write directly to any register.
Expand Down
7 changes: 7 additions & 0 deletions src/modbus-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,19 @@ module.exports = function (RED) {

node.on('close', function (done) {
mbBasics.setNodeStatusTo('closed', node)

if (node.netServer) {
node.netServer.close(() => {
internalDebugLog('Modbus Server closed')
done()
node.removeAllListeners()
node.netServer.removeAllListeners()
})
} else {
done()
node.removeAllListeners()
}

node.modbusServer = null
})
}
Expand Down
225 changes: 134 additions & 91 deletions test/units/modbus-getter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,98 +92,133 @@ describe('Getter node Unit Testing', function () {
// })

it('should handle error protocol message correctly', function () {
helper.load(testGetterNodes, testFlows.testGetterNodeFlowExample, function () {
const modbusWriteNode = helper.getNode('09f8f0e2049ace2d')
modbusWriteNode.showErrors = true
const msg = {
payload: {
value: 'payloadValue'
const flow = Array.from(testFlows.testGetterNodeFlowExample)

getPort().then((port) => {
flow[1].serverPort = port
flow[5].tcpPort = port

helper.load(testGetterNodes, flow, function () {
const modbusWriteNode = helper.getNode('09f8f0e2049ace2d')
modbusWriteNode.showErrors = true
const msg = {
payload: {
value: 'payloadValue'
}
}
}
const err = new Error('test error')
const err = new Error('test error')

sinon.stub(mbBasics, 'logMsgError').returns()
sinon.stub(mbBasics, 'logMsgError').returns()

modbusWriteNode.errorProtocolMsg(err, msg)
modbusWriteNode.errorProtocolMsg(err, msg)

sinon.assert.calledOnce(mbBasics.logMsgError)
sinon.assert.calledWith(mbBasics.logMsgError, modbusWriteNode, err, msg)
sinon.assert.calledOnce(mbBasics.logMsgError)
sinon.assert.calledWith(mbBasics.logMsgError, modbusWriteNode, err, msg)

sinon.restore()
sinon.restore()
})
})
})

it('should build new message object correctly', function (done) {
helper.load(testGetterNodes, testFlows.testGetterNodeFlowExample, function () {
const modbusWriteNode = helper.getNode('09f8f0e2049ace2d')
const msg = {
topic: 'topic',
payload: {
value: 'payloadValue'
const flow = Array.from(testFlows.testGetterNodeFlowExample)

getPort().then((port) => {
flow[1].serverPort = port
flow[5].tcpPort = port

helper.load(testGetterNodes, flow, function () {
const modbusWriteNode = helper.getNode('09f8f0e2049ace2d')
const msg = {
topic: 'topic',
payload: {
value: 'payloadValue'
}
}
}

const newMsg = modbusWriteNode.buildNewMessageObject(modbusWriteNode, msg)
expect(newMsg.topic).to.equal('topic')
expect(newMsg.payload.value).to.equal('payloadValue')
expect(newMsg.payload.fc).to.equal(3)
expect(newMsg.payload.address).to.equal('1')
expect(newMsg.payload.quantity).to.equal('10')
done()

const newMsg = modbusWriteNode.buildNewMessageObject(modbusWriteNode, msg)
expect(newMsg.topic).to.equal('topic')
expect(newMsg.payload.value).to.equal('payloadValue')
expect(newMsg.payload.fc).to.equal(3)
expect(newMsg.payload.address).to.equal('1')
expect(newMsg.payload.quantity).to.equal('10')
done()
})
})
})

it('should handle onModbusCommandDone correctly', function (done) {
helper.load(testGetterNodes, testFlows.testGetterNodeFlowExample, function () {
const modbusWriteNode = helper.getNode('09f8f0e2049ace2d')
modbusWriteNode.showStatusActivities = true
const emitSpy = sinon.spy(modbusWriteNode, 'emit')
const resp = { data: [1, 2, 3, 4] }
const msg = { payload: 'test payload' }
modbusWriteNode.onModbusCommandDone(resp, msg)
sinon.assert.calledOnce(emitSpy)
sinon.assert.calledWith(emitSpy, 'modbusGetterNodeDone')
emitSpy.restore()

done()
})
})
const flow = Array.from(testFlows.testGetterNodeFlowExample)

it('should reset input delay timer correctly', function (done) {
helper.load(testGetterNodes, testFlows.testInjectGetterWithClientFlow, function () {
const modbusGetter = helper.getNode('cea01c8.36f8f6')
modbusGetter.inputDelayTimer = true
const clearTimeoutStub = sinon.stub(global, 'clearTimeout')
getPort().then((port) => {
flow[1].serverPort = port
flow[5].tcpPort = port

modbusGetter.resetInputDelayTimer()
sinon.assert.calledOnce(clearTimeoutStub)
helper.load(testGetterNodes, flow, function () {
const modbusWriteNode = helper.getNode('09f8f0e2049ace2d')
modbusWriteNode.showStatusActivities = true
const emitSpy = sinon.spy(modbusWriteNode, 'emit')
const resp = { data: [1, 2, 3, 4] }
const msg = { payload: 'test payload' }
modbusWriteNode.onModbusCommandDone(resp, msg)
sinon.assert.calledOnce(emitSpy)
sinon.assert.calledWith(emitSpy, 'modbusGetterNodeDone')
emitSpy.restore()

done()
done()
})
})
})

// it('should reset input delay timer correctly', function (done) {
// const flow = Array.from(testFlows.testInjectGetterWithClientFlow)
//
// getPort().then((port) => {
// flow[1].serverPort = port
// flow[5].tcpPort = port
//
// helper.load(testGetterNodes, flow, function () {
// const modbusGetter = helper.getNode('cea01c8.36f8f6')
// modbusGetter.inputDelayTimer = true
// const clearTimeoutStub = sinon.stub(global, 'clearTimeout')
//
// modbusGetter.resetInputDelayTimer()
// sinon.assert.calledOnce(clearTimeoutStub)
//
// done()
// })
// })
// })

it('should initialize input delay timer when delayOnStart is true', function (done) {
helper.load(testGetterNodes, testFlows.testInjectGetterWithClientFlow, function () {
const modbusGetter = helper.getNode('cea01c8.36f8f6')
modbusGetter.delayOnStart = true
const flow = Array.from(testFlows.testInjectGetterWithClientFlow)

const verboseWarnSpy = sinon.spy()
const resetInputDelayTimerSpy = sinon.spy(modbusGetter, 'resetInputDelayTimer')
getPort().then((port) => {
flow[1].serverPort = port
flow[5].tcpPort = port

const setTimeoutStub = sinon.stub(global, 'setTimeout').callsFake((callback, delay) => {
callback()
})
helper.load(testGetterNodes, flow, function () {
const modbusGetter = helper.getNode('cea01c8.36f8f6')
modbusGetter.delayOnStart = true

modbusGetter.verboseWarn = verboseWarnSpy
modbusGetter.initializeInputDelayTimer()
sinon.assert.calledOnce(resetInputDelayTimerSpy)
sinon.assert.calledOnce(setTimeoutStub)
sinon.assert.calledWith(setTimeoutStub, sinon.match.func, modbusGetter.INPUT_TIMEOUT_MILLISECONDS * modbusGetter.startDelayTime)
const verboseWarnSpy = sinon.spy()
const resetInputDelayTimerSpy = sinon.spy(modbusGetter, 'resetInputDelayTimer')

setTimeoutStub.restore()
resetInputDelayTimerSpy.restore()
const setTimeoutStub = sinon.stub(global, 'setTimeout').callsFake((callback, delay) => {
callback()
})

done()
modbusGetter.verboseWarn = verboseWarnSpy
modbusGetter.initializeInputDelayTimer()
sinon.assert.calledOnce(resetInputDelayTimerSpy)
sinon.assert.calledOnce(setTimeoutStub)
sinon.assert.calledWith(setTimeoutStub, sinon.match.func, modbusGetter.INPUT_TIMEOUT_MILLISECONDS * modbusGetter.startDelayTime)

setTimeoutStub.restore()
resetInputDelayTimerSpy.restore()

done()
})
})
})

Expand Down Expand Up @@ -396,33 +431,41 @@ describe('Getter node Unit Testing', function () {
describe('post', function () {
it('should handle input correctly and emit readModbus event', function (done) {
const msg = { payload: 'valid' }
helper.load(testGetterNodes, testFlows.testGetterNodeFlowExample, function () {
const modbusGetterNode = helper.getNode('09f8f0e2049ace2d')
const modbusClient = helper.getNode('80aeec4c.0cb9e8')
modbusGetterNode.showStatusActivities = true
const isNotReadyForInputStub = sinon.stub(modbusGetterNode, 'isNotReadyForInput').returns(false)
const isInactiveStub = sinon.stub(modbusClient, 'isInactive').returns(false)
const invalidPayloadInStub = sinon.stub(mbBasics, 'invalidPayloadIn').returns(false)

const buildNewMessageObjectStub = sinon.stub(modbusGetterNode, 'buildNewMessageObject').returns({ messageId: '12345' })
const buildNewMessageStub = sinon.stub(mbBasics, 'buildNewMessage').returns({ builtMessage: true })
const emitStub = sinon.stub(modbusClient, 'emit')

modbusGetterNode.emit('input', msg)

sinon.assert.calledOnce(buildNewMessageObjectStub)
sinon.assert.calledOnce(buildNewMessageStub)
sinon.assert.calledOnce(emitStub)

isNotReadyForInputStub.restore()
isInactiveStub.restore()
invalidPayloadInStub.restore()
buildNewMessageObjectStub.restore()
buildNewMessageStub.restore()
emitStub.restore()
done()
const flow = Array.from(testFlows.testGetterNodeFlowExample)

getPort().then((port) => {
flow[1].serverPort = port
flow[5].tcpPort = port

helper.load(testGetterNodes, flow, function () {
const modbusGetterNode = helper.getNode('09f8f0e2049ace2d')
const modbusClient = helper.getNode('80aeec4c.0cb9e8')
modbusGetterNode.showStatusActivities = true
const isNotReadyForInputStub = sinon.stub(modbusGetterNode, 'isNotReadyForInput').returns(false)
const isInactiveStub = sinon.stub(modbusClient, 'isInactive').returns(false)
const invalidPayloadInStub = sinon.stub(mbBasics, 'invalidPayloadIn').returns(false)

const buildNewMessageObjectStub = sinon.stub(modbusGetterNode, 'buildNewMessageObject').returns({ messageId: '12345' })
const buildNewMessageStub = sinon.stub(mbBasics, 'buildNewMessage').returns({ builtMessage: true })
const emitStub = sinon.stub(modbusClient, 'emit')

modbusGetterNode.emit('input', msg)

sinon.assert.calledOnce(buildNewMessageObjectStub)
sinon.assert.calledOnce(buildNewMessageStub)
sinon.assert.calledOnce(emitStub)

isNotReadyForInputStub.restore()
isInactiveStub.restore()
invalidPayloadInStub.restore()
buildNewMessageObjectStub.restore()
buildNewMessageStub.restore()
emitStub.restore()
done()
})
})
})

it('should fail for invalid node', function (done) {
helper.load(testGetterNodes, [], function () {
helper.request().post('/modbus-getter/invalid').expect(404).end(done)
Expand Down
2 changes: 1 addition & 1 deletion test/units/modbus-server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ describe('Server node Testing', function () {
expect(modbusServer.statusText).to.equal('error')
expect(err.message).to.equal('listen EADDRNOTAVAIL: address not available 127.0.0.2:' + port)
done()
}, 600)
}, 800)
})
})
})
Expand Down

0 comments on commit d36b135

Please sign in to comment.