-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_rticonnextdds_dataflow.js
More file actions
146 lines (125 loc) · 5.1 KB
/
test_rticonnextdds_dataflow.js
File metadata and controls
146 lines (125 loc) · 5.1 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/******************************************************************************
* (c) 2005-2019 Copyright, Real-Time Innovations. All rights reserved. *
* No duplications, whole or partial, manual or electronic, may be made *
* without express written permission. Any such copies, or revisions thereof, *
* must display this notice unaltered. *
* This code contains trade secrets of Real-Time Innovations, Inc. *
******************************************************************************/
const path = require('path')
const expect = require('chai').expect
const rti = require(path.join(__dirname, '/../../rticonnextdds-connector'))
// We have to do this due to the expect() syntax of chai and the fact
// that we install mocha globally
/* eslint-disable no-unused-expressions */
/* eslint-disable no-undef */
// We provide a timeout of 10s to operations that we expect to succeed. This
// is so that if they fail, we know for sure something went wrong
const testExpectSuccessTimeout = 10000
// Test Parameterization- describe block will execute once for each param
const params = ['read', 'take']
params.forEach((retrievalMethod) => {
describe('DataflowTests for ' + retrievalMethod, function () {
var input, output, testMsg
// Initialization before all tests execute
before(async function () {
testMsg = { x: 1, y: 1, z: true, color: 'BLUE', shapesize: 5 }
const participantProfile = 'MyParticipantLibrary::Zero'
const xmlProfile = path.join(__dirname, '/../xml/TestConnector.xml')
connector = new rti.Connector(participantProfile, xmlProfile)
input = connector.getInput('MySubscriber::MySquareReader')
output = connector.getOutput('MyPublisher::MySquareWriter')
try {
const matches = await input.waitForPublications(testExpectSuccessTimeout)
expect(matches).to.be.at.least(1)
} catch (err) {
console.log('Caught err: ' + err)
throw(err)
}
})
// Clean-up after all tests execute
after(function () {
this.timeout(0)
connector.close()
})
// Initialization done before each test executes
beforeEach(async function () {
output.instance.setFromJSON(testMsg)
output.write()
try {
await input.wait(testExpectSuccessTimeout)
} catch (err) {
console.log('Caught err: ' + err)
throw(err)
}
input[retrievalMethod]()
expect(input.samples.length).to.be.at.least(1)
})
afterEach(function () {
// take any samples from middleware chache
input.take()
})
it('samples length should be 1', function () {
const len = input.samples.getLength()
expect(len).to.equal(1)
})
it('infos length should be 1', function () {
const len = input.infos.getLength()
expect(len).to.equal(1)
})
it('data received should be valid', function () {
const validity = input.infos.isValid(0)
expect(validity).to.equal(1)
})
it('received JSON representation of data should be the same as ' +
'the JSON object sent', function () {
const receivedJson = input.samples.getJSON(0)
expect(receivedJson).to.deep.equal(JSON.parse(JSON.stringify(testMsg)))
})
it('received fields of data should be the same as ' +
'that of the JSON object sent', function () {
const x = input.samples.getNumber(0, 'x')
const y = input.samples.getNumber(0, 'y')
const z = input.samples.getBoolean(0, 'z')
const color = input.samples.getString(0, 'color')
const shapesize = input.samples.getNumber(0, 'shapesize')
expect(x).to.equal(testMsg.x)
expect(y).to.equal(testMsg.y)
// NOTE: getBoolean returns an Integer representation of Boolean (legacy reasons)
expect(z).to.equal(+testMsg.z)
expect(shapesize).to.equal(testMsg.shapesize)
expect(color).to.equal(testMsg.color)
})
it('getting a number or string field as a boolean should fail in the core', () => {
const numberField = 'x'
const stringField = 'color'
expect(() => {
input.samples.getBoolean(0, numberField)
}).to.throw(rti.DDSError)
expect(() => {
input.samples.getBoolean(0, stringField)
}).to.throw(rti.DDSError)
})
it('should be possible to obtain a number as a string', () => {
const numberField = 'x'
const numberAsString = input.samples.getString(0, numberField)
expect(numberAsString).to.equal('1')
})
it('should not be possible to obtain a boolean as a string', () => {
const booleanField = 'z'
expect(() => {
input.samples.getString(0, booleanField)
}).to.throw(rti.DDSError)
})
it('should be possible to get a boolean field as a number', () => {
const booleanField = 'z'
const booleanAsNumber = input.samples.getNumber(0, booleanField)
expect(booleanAsNumber).to.equal(1)
})
it('should not be possible to get a string field as a number', () => {
const stringField = 'color'
expect(() => {
input.samples.getNumber(0, stringField)
}).to.throw(rti.DDSError)
})
})
})