-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_rticonnextdds_connector.js
More file actions
205 lines (185 loc) · 8.52 KB
/
test_rticonnextdds_connector.js
File metadata and controls
205 lines (185 loc) · 8.52 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/******************************************************************************
* (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'))
const sinon = require('sinon')
// 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 */
describe('Connector Tests', function () {
it('Connector should throw an error for invalid xml path', function () {
const participantProfile = 'MyParticipantLibrary::Zero'
const invalidXmlPath = 'invalid/path/to/xml'
expect(function () {
new rti.Connector(participantProfile, invalidXmlPath) // eslint-disable-line no-new
}).to.throw(Error)
})
it('Connector should throw an error for invalid participant profile', function () {
const invalidParticipantProfile = 'InvalidParticipantProfile'
const xmlPath = path.join(__dirname, '/../xml/TestConnector.xml')
expect(function () {
new rti.Connector(invalidParticipantProfile, xmlPath) // eslint-disable-line no-new
}).to.throw(Error)
})
it('Connector should throw an error for invalid xml profile', function () {
const participantProfile = 'MyParticipantLibrary::Zero'
const invalidXml = path.join(__dirname, '/../xml/InvalidXml.xml')
expect(function () {
new rti.Connector(participantProfile, invalidXml) // eslint-disable-line no-new
}).to.throw(Error)
})
it('Connector should get instantiated for valid' +
'xml and participant profile', async function () {
const participantProfile = 'MyParticipantLibrary::Zero'
const xmlProfile = path.join(__dirname, '/../xml/TestConnector.xml')
const connector = new rti.Connector(participantProfile, xmlProfile)
expect(connector).to.exist
expect(connector).to.be.instanceOf(rti.Connector)
await connector.close()
})
it('Multiple Connector objects can be instantiated', async () => {
const participantProfile = 'MyParticipantLibrary::Zero'
const xmlProfile = path.join(__dirname, '/../xml/TestConnector.xml')
const connectors = []
for (let i = 0; i < 3; i++) {
connectors.push(new rti.Connector(participantProfile, xmlProfile))
}
connectors.forEach(async (connector) => {
expect(connector).to.exist
expect(connector).to.be.instanceOf(rti.Connector)
await connector.close()
})
})
// Test for CON-163
it('Multiple Connector obejcts can be instantiated without participant QoS', async () => {
const participantProfile = 'MyParticipantLibrary::MyParticipant'
const xmlProfile = path.join(__dirname, '/../xml/TestConnector3.xml')
const connectors = []
for (let i = 0; i < 2; i++) {
connectors.push(new rti.Connector(participantProfile, xmlProfile))
}
connectors.forEach(async (connector) => {
expect(connector).to.exist
expect(connector).to.be.instanceOf(rti.Connector)
await connector.close()
})
})
it('Load two XML files using the url group syntax', async function () {
const xmlProfile1 = path.join(__dirname, '/../xml/TestConnector.xml')
const xmlProfile2 = path.join(__dirname, '/../xml/TestConnector2.xml')
const fullXmlPath = xmlProfile1 + ';' + xmlProfile2
const connector = new rti.Connector('MyParticipantLibrary2::MyParticipant2', fullXmlPath)
expect(connector).to.exist
expect(connector).to.be.instanceOf(rti.Connector)
const output = connector.getOutput('MyPublisher2::MySquareWriter2')
expect(output).to.exist
await connector.close()
})
it('Should be possible to create a Connector with participant qos', function () {
const xmlProfile = path.join(__dirname, '/../xml/TestConnector.xml')
const connector = new rti.Connector(
'MyParticipantLibrary::ConnectorWithParticipantQos',
xmlProfile)
expect(connector).to.exist
expect(connector).to.be.instanceOf(rti.Connector)
})
it('is possible to obtain the current version of Connector', function () {
const version = rti.Connector.getVersion()
expect(version).to.be.a.string
// The returned version string should contain four pieces of information:
// - the API version of Connector
// - the build ID of core.1.0
// - the build ID of dds_c.1.0
// - the build ID of lua_binding.1.0
// Expect "RTI Connector for JavaScript, version X.X.X"
let regex = /RTI Connector for JavaScript, version ([0-9][.]){2}[0-9]/
expect(regex.test(version)).deep.equals(true)
// Expect "NDDSCORE_BUILD_<VERSION>_<DATE>T<TIMESTAMP>Z"
regex = /.*NDDSCORE_BUILD_([0-9][.]){2}[0-9]_[0-9]{8}T[0-9]{6}Z/
expect(regex.test(version)).deep.equals(true)
// Expect "NDDSC_BUILD_<VERSION>_<DATE>T<TIMESTAMP>Z"
regex = /.*NDDSC_BUILD_([0-9][.]){2}[0-9]_[0-9]{8}T[0-9]{6}Z/
expect(regex.test(version)).deep.equals(true)
// Expect "RTICONNECTOR_BUILD_<VERSION>_<DATE>T<TIMESTAMP>Z"
regex = /.*RTICONNECTOR_BUILD_([0-9][.]){2}[0-9]_[0-9]{8}T[0-9]{6}Z/
expect(regex.test(version)).deep.equals(true)
})
// Test for CON-200
it('Connector should not segfault if deleted twice', async function () {
const xmlProfile1 = path.join(__dirname, '/../xml/TestConnector.xml')
const xmlProfile2 = path.join(__dirname, '/../xml/TestConnector2.xml')
const fullXmlPath = xmlProfile1 + ';' + xmlProfile2
const connector = new rti.Connector('MyParticipantLibrary2::MyParticipant2', fullXmlPath)
expect(connector).to.exist
expect(connector).to.be.instanceOf(rti.Connector)
await connector.close()
await connector.close()
})
describe('Connector callback test', function () {
let connector
// Initialization before all tests are executed
before(() => {
const participantProfile = 'MyParticipantLibrary::Zero'
const xmlProfile = path.join(__dirname, '/../xml/TestConnector.xml')
connector = new rti.Connector(participantProfile, xmlProfile)
})
// Cleanup after all tests have executed
after(async () => {
await connector.delete()
})
it('on_data_available callback gets called when data is available', function (done) {
// spies are used for testing callbacks
const spy = sinon.spy()
setTimeout(() => {
expect(spy.calledOnce).to.be.true
done() // Pattern for async testing: next test won't execute until done gets called.
}, 1000) // Expectation Test will execute after 1000 milisec
connector.once('on_data_available', spy)
output = connector.getOutput('MyPublisher::MySquareWriter')
testMsg = '{"x":1,"y":1,"z":true,"color":"BLUE","shapesize":5}'
output.instance.setFromJson(JSON.parse(testMsg))
output.write()
})
it('on_data_available emits the error event on error', function (done) {
const errorSpy = sinon.spy()
// We expect the "error" event to be emitted within the next second
setTimeout(() => {
expect(errorSpy.calledOnce).to.be.true
connector.removeAllListeners('on_data_available')
done()
}, 1000)
connector.once('error', errorSpy)
// Need to cause the onDataAvailable callback to throw an error, we do
// this by concurrently waiting on the same connector object
connector.wait(500)
connector.once('on_data_available', () => {})
})
it('internal waitset is waited on repeatedly within on_data_available', function (done) {
const spy = sinon.spy()
// We expect the data to be received within the next second
setTimeout(() => {
expect(spy.calledOnce).to.be.true
done()
}, 1500)
// Set the listener
connector.once('on_data_available', spy)
// Internally, on_data_available calls connector.wait every 500ms.
// Test that if no data is received within the first 500ms, we call wait
// multiple times
output = connector.getOutput('MyPublisher::MySquareWriter')
testMsg = '{"x":1,"y":1,"z":true,"color":"BLUE","shapesize":5}'
output.instance.setFromJson(JSON.parse(testMsg))
// In 500ms, write the data
setTimeout(() => {
output.write()
}, 1000)
})
})
})