-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtestSerialResponseSmlTimeout.js
84 lines (74 loc) · 3.27 KB
/
testSerialResponseSmlTimeout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*jshint expr: true*/
var chai = require('chai');
var expect = chai.expect;
var mock = require('mock-require');
mock('serialport', 'virtual-serialport');
describe('test SerialResponseTransport Timeout with SmlProtocol', function() {
it('check timeout', function(done) {
this.timeout(600000); // because of first install from npm
var SmartmeterObis = require('../index.js');
var options = {
'protocol': 'SmlProtocol',
'transport': 'SerialResponseTransport',
'transportSerialPort': '/dev/ir-usb0',
'transportSerialBaudrate': 9600,
'requestInterval': 10,
'transportHttpRequestUrl': '',
'obisNameLanguage': 'en',
'transportSerialMessageTimeout': 10000,
'debug': 2
};
var counter = 0;
var errCounter = 0;
function testStoreData(err, obisResult) {
if (err) {
expect(obisResult).to.be.null;
errCounter++;
console.log('ERROR: ' + err);
}
else {
console.log('Received data ' + counter + ': ' + Object.keys(obisResult));
counter++;
for (var obisId in obisResult) {
console.log(obisResult[obisId].idToString() + ': ' + SmartmeterObis.ObisNames.resolveObisName(obisResult[obisId], options.obisNameLanguage).obisName + ' = ' + obisResult[obisId].valueToString());
}
}
}
var smTransport = SmartmeterObis.init(options, testStoreData);
/*var errorHandled = false;
var originalException = process.listeners('uncaughtException').pop();
if (originalException) process.removeListener('uncaughtException', originalException);
process.once("uncaughtException", function (err) {
console.log('CATCHED');
if (originalException) process.listeners('uncaughtException').push(originalException);
expect(err).to.be.an.instanceof(Error);
expect(err.message).to.be.equal('No or too long message from Serial Device.');
expect(counter).to.be.equal(0);
smTransport.stop();
setTimeout(done, 1000);
});*/
setTimeout(function() {
expect(counter).to.be.equal(0);
expect(errCounter).to.be.equal(1);
expect(smTransport.serialConnected).to.be.false;
expect(smTransport.serialComm).to.be.null;
expect(smTransport.stopRequests).to.be.false;
setTimeout(function() {
expect(smTransport.serialConnected).to.be.true;
expect(smTransport.serialComm).not.to.be.null;
expect(smTransport.stopRequests).to.be.false;
setTimeout(function() {
expect(counter).to.be.equal(0);
expect(errCounter).to.be.equal(2);
expect(smTransport.serialConnected).to.be.false;
expect(smTransport.serialComm).to.be.null;
expect(smTransport.stopRequests).to.be.false;
smTransport.stop(function() {
done();
});
}, 12000);
}, 9000);
}, 12000);
smTransport.process();
});
});