Skip to content

Commit

Permalink
refactor(test): clean up cbs and fix IO Node tests
Browse files Browse the repository at this point in the history
  • Loading branch information
biancode committed Jun 16, 2024
1 parent 65176fa commit b7818b3
Show file tree
Hide file tree
Showing 22 changed files with 557 additions and 677 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [5.40.0](https://github.com/biancoroyal/node-red-contrib-modbus/compare/v5.27.2...v5.40.0) (2024-06-15)
# [5.40.0](https://github.com/biancoroyal/node-red-contrib-modbus/compare/v5.27.2...v5.40.0) (2024-06-16)


### Bug Fixes
Expand Down
7 changes: 4 additions & 3 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Version History

## Node-RED v3.x versions:
* Node.js 20.x will be supported with v5.40+
* Node.js 18.x will be supported with v5.22+
* Node.js 16.x will be supported with v5.22+

Expand All @@ -17,7 +18,7 @@

## Node-RED v0.x versions:
* Node.js 10.x is supported with v4.x
* Node.js 8.x is supported with v3.x
* Node.js 6.x is supported with v2.x
* Node.js 4.x is supported with v1.x
* Node.js 8.x is supported with v3.x
* Node.js 6.x is supported with v2.x
* Node.js 4.x is supported with v1.x

18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@
"test:slow": "npm run lint && mocha ./test --recursive --reporter dot",
"test:verbose": "npm run lint && mocha ./test --recursive",
"test:withStop": "npm run lint && mocha ./test --recursive --bail",
"test:e2e": "npm run lint && mocha ./test/core/modbus-io-core-test.js --parallel --recursive --reporter dot --timeout 100000",
"lint": "standard --fix",
"test:e2e": "npm run lint && mocha './test/e2e/*e2e-test.js' --parallel --recursive --reporter dot --timeout 100000",
"test:units": "npm run lint && mocha './test/units/*-test.js' --parallel --recursive",
"test:core": "npm run lint && mocha './test/core/*-test.js' --parallel --recursive",
"test-nyc": "nyc --reporter=html --reporter=text mocha --recursive",
"test-npx": "npx nyc@latest --reporter=html --reporter=text mocha ./test --parallel --recursive --reporter dot --timeout 10000",
"test-with-coverage": "nyc --reporter=lcovonly mocha --recursive --timeout 7000 -R spec && cat ./coverage/lcov.info | codacy-coverage --token $CODACY_COVERAGE_TOKEN && rm -rf ./coverage",
"test-with-coverage": "nyc --reporter=lcovonly mocha --recursive -R spec && cat ./coverage/lcov.info | codacy-coverage --token $CODACY_COVERAGE_TOKEN && rm -rf ./coverage",
"coverage": "npm run lint && nyc mocha ./test --parallel --recursive --reporter dot --timeout 10000",
"coverage-nyc": "npm run lint && nyc report --reporter=text-lcov | coveralls",
"build": "npm run lint && gulp",
Expand Down Expand Up @@ -134,7 +134,9 @@
"test": "test"
},
"standard": {
"env": ["mocha"],
"env": [
"mocha"
],
"globals": [
"chai",
"expect",
Expand All @@ -148,6 +150,7 @@
"modbus/",
"docs",
"extras",
"test/resources/**",
"test/e2e/flows/**",
"test/integrations/flows/**",
"test/units/flows/**"
Expand Down
77 changes: 40 additions & 37 deletions src/modbus-io-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@author <a href="mailto:[email protected]">Klaus Landsdorf</a> (Bianco Royal)
*/

module.exports = function (RED) {
'use strict'
// SOURCE-MAP-REQUIRED
Expand All @@ -25,29 +26,45 @@ module.exports = function (RED) {
const node = this
node.setMaxListeners(UNLIMITED_LISTENERS)
node.lastUpdatedAt = null
const lineReader = new coreIO.LineByLineReader(node.path)

if (!fs.existsSync(node.path)) {
coreIO.internalDebug('IO File Not Found ' + node.path)
node.warn('Modbus IO File Not Found ' + node.path)
return
}

node.lineReader = new coreIO.LineByLineReader(node.path)
coreIO.internalDebug('Read IO File ' + node.path)
node.configData = []

lineReader.on('error', function (err) {
coreIO.internalDebug(err.message)
})

lineReader.on('line', function (line) {
if (line) {
node.configData.push(line)
// node.configData.push(JSON.parse(line))
}
})
function setLineReaderEvents () {
node.lineReader.removeAllListeners()

node.lineReader.on('error', function (err) {
coreIO.internalDebug(err.message)
})

node.lineReader.on('line', function (line) {
if (line) {
node.configData.push(line)
}
})

node.lineReader.on('end', function () {
node.lastUpdatedAt = Date.now()
coreIO.internalDebug('Read IO Done From File ' + node.path)
node.warn({
payload: coreIO.allValueNamesFromIOFile(node),
name: 'Modbus Value Names From IO File',
path: node.path
})
node.emit('updatedConfig', node.configData)
})

lineReader.on('end', function () {
node.lastUpdatedAt = Date.now()
coreIO.internalDebug('Read IO Done From File ' + node.path)
node.warn({ payload: coreIO.allValueNamesFromIOFile(node), name: 'Modbus Value Names From IO File', path: node.path })
node.emit('updatedConfig', node.configData)
})
coreIO.internalDebug('Loading IO File Started For ' + node.path)
}

coreIO.internalDebug('Loading IO File Started For ' + node.path)
setLineReaderEvents()

node.watcher = fs.watchFile(node.path, (curr, prev) => {
coreIO.internalDebug(`the current mtime is: ${curr.mtime}`)
Expand All @@ -57,32 +74,18 @@ module.exports = function (RED) {
coreIO.internalDebug('Reload IO File ' + node.path)
node.configData = []
delete node.lastUpdatedAt

const lineReader = new coreIO.LineByLineReader(node.path)
lineReader.on('error', function (err) {
coreIO.internalDebug(err.message)
})

lineReader.on('line', function (line) {
if (line) {
node.configData.push(JSON.parse(line))
}
})

lineReader.on('end', function () {
node.lastUpdatedAt = Date.now()
coreIO.internalDebug('Reload IO Done From File ' + node.path)
node.warn({ payload: coreIO.allValueNamesFromIOFile(node), name: 'Modbus Value Names From IO File', path: node.path })
node.emit('updatedConfig', node.configData)
})

node.lineReader.removeAllListeners()
node.lineReader = new coreIO.LineByLineReader(node.path)
setLineReaderEvents()
coreIO.internalDebug('Reloading IO File Started For ' + node.path)
}
})

node.on('close', function (done) {
fs.unwatchFile(node.path)
node.watcher.stop()
node.lineReader.removeAllListeners()
node.removeAllListeners()
done()
})
}
Expand Down
15 changes: 8 additions & 7 deletions test/helper/test-helper-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,21 @@ class PortHelper {

getPort = async () => {
if (this.startPort === 0) {
this.startPort = this.getRandomArbitrary(15000, 40000)
this.startPort = this.getRandomArbitrary(30000, 50000)
}

let port = this.startPort++
this.startPort = this.startPort + 3

if (this.startPort >= 65535) {
this.startPort = 20000
if (this.startPort >= 50000) {
this.startPort = this.getRandomArbitrary(10000, 30000)
}

if (global.portList.includes(port)) {
port = await this.getPort()
if (global.portList.includes(this.startPort)) {
this.startPort = await this.getPort()
}

return port
global.portList.push(this.startPort)
return this.startPort
}

tryListen (port, maxPort, hostname, callback) {
Expand Down
Loading

0 comments on commit b7818b3

Please sign in to comment.