-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_rticonnextdds_discovery.js
More file actions
436 lines (392 loc) · 16.3 KB
/
test_rticonnextdds_discovery.js
File metadata and controls
436 lines (392 loc) · 16.3 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/******************************************************************************
* (c) 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 chai = require('chai')
const chaiAsPromised = require('chai-as-promised')
const expect = chai.expect
chai.config.includeStack = true
chai.use(chaiAsPromised)
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 */
// Create the connector at this level so it can be automatically closed after
// each test
let discoveryConnector = null
let discoveryConnectorNoEntityNames = null
let readerOnlyConnector = null
let writerOnlyConnector = null
// 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
// We provide a much shorter timeout to operations that we expect to timeout.
// This is to prevent us from hanging the tests for 10s
const testExpectFailureTimeout = 500
const getDiscoveryConnector = () => {
if (discoveryConnector === null) {
const xmlPath = path.join(__dirname, '/../xml/TestConnector.xml')
const profile = 'MyParticipantLibrary::DiscoveryTest'
discoveryConnector = new rti.Connector(profile, xmlPath)
}
expect(discoveryConnector).to.exist.and.be.and.instanceOf(rti.Connector)
return discoveryConnector
}
const getDiscoveryConnectorNoEntityNames = () => {
if (discoveryConnectorNoEntityNames === null) {
const xmlPath = path.join(__dirname, '/../xml/TestConnector.xml')
const profile = 'MyParticipantLibrary::DiscoveryTestNoEntityName'
discoveryConnectorNoEntityNames = new rti.Connector(profile, xmlPath)
}
expect(discoveryConnectorNoEntityNames).to.exist.and.be.and.instanceOf(rti.Connector)
return discoveryConnectorNoEntityNames
}
const getReaderOnlyConnector = () => {
if (readerOnlyConnector === null) {
const xmlPath = path.join(__dirname, '/../xml/TestConnector.xml')
const profile = 'MyParticipantLibrary::DiscoveryTestReaderOnly'
readerOnlyConnector = new rti.Connector(profile, xmlPath)
}
expect(readerOnlyConnector).to.exist.and.be.and.instanceOf(rti.Connector)
return readerOnlyConnector
}
const getWriterOnlyConnector = () => {
if (writerOnlyConnector === null) {
const xmlPath = path.join(__dirname, '/../xml/TestConnector.xml')
const profile = 'MyParticipantLibrary::DiscoveryTestWriterOnly'
writerOnlyConnector = new rti.Connector(profile, xmlPath)
}
expect(writerOnlyConnector).to.exist.and.be.and.instanceOf(rti.Connector)
return writerOnlyConnector
}
const getDiscoveryReaderOnlyInput = () => {
const connector = getReaderOnlyConnector()
const input = connector.getInput('TestSubscriber::TestReader')
expect(input).to.exist
return input
}
const getDiscoveryWriterOnlyOutput = () => {
const connector = getWriterOnlyConnector()
const output = connector.getOutput('TestPublisher::TestWriter')
expect(output).to.exist
return output
}
const cleanupConnectors = async () => {
if (discoveryConnector !== null) {
await discoveryConnector.close()
discoveryConnector = null
}
if (discoveryConnectorNoEntityNames !== null) {
await discoveryConnectorNoEntityNames.close()
discoveryConnectorNoEntityNames = null
}
if (readerOnlyConnector !== null) {
await readerOnlyConnector.close()
readerOnlyConnector = null
}
if (writerOnlyConnector !== null) {
await writerOnlyConnector.close()
writerOnlyConnector = null
}
}
describe('Discovery tests', function () {
// By default, mocha will kill all tests if they take longer than 2s. Some of
// the tests in this block can take up to 1.5s so to be safe we increase this
// timeout. Note, this means we cannot use fat arrow functions here (since 'this'
// is not binded in fat arrows).
this.timeout('30s')
afterEach(() => {
cleanupConnectors()
})
it('Create a Connector object with an input and no output', async function () {
const input = getDiscoveryReaderOnlyInput()
// At this point we should not have matched anything
const matches = input.matchedPublications
expect(matches.length).to.deep.equals(0)
// We should timeout if we attempt to wait for a match
try {
const newMatches = await input.waitForPublications(testExpectFailureTimeout)
console.log('Expected waitForPublications to timeout, but we matched ' + newMatches + ' publications')
// Should not get here - fail the test
expect(false).to.deep.equals(true)
} catch (err) {
expect(err).to.be.an.instanceof(rti.TimeoutError)
}
})
it('Create a Connector object with an output and no input', async function () {
const output = getDiscoveryWriterOnlyOutput()
// At this point we should not have matched anything
const matches = output.matchedSubscriptions
expect(matches.length).to.deep.equals(0)
// We should timeout if we attempt to wait for a match
try {
const newMatches = await output.waitForSubscriptions(testExpectFailureTimeout)
console.log('Expected waitForSubscriptions to timeout, but we matched ' + newMatches + ' subscriptions')
// Should not get here - fail the test
expect(false).to.deep.equals(true)
} catch (err) {
expect(err).to.be.an.instanceof(rti.TimeoutError)
}
})
it('Check matching between a single input and output', async function () {
const connector = getDiscoveryConnector()
const input = connector.getInput('MySubscriber::MyReader')
const output = connector.getOutput('MyPublisher::MyWriter')
// Both the input and output should match each other and nothing else
try {
let changesInMatches = await input.waitForPublications(testExpectSuccessTimeout)
expect(changesInMatches).to.deep.equals(1)
changesInMatches = await output.waitForSubscriptions(testExpectSuccessTimeout)
expect(changesInMatches).to.deep.equals(1)
} catch (err) {
console.log('Caught error: ' + err)
// Fail the test
expect(false).to.deep.equals(true)
}
let matches = input.matchedPublications
expect(matches.length).to.deep.equals(1)
expect(matches).to.deep.include.members([{ name: 'MyWriter' }])
matches = output.matchedSubscriptions
expect(matches.length).to.deep.equals(1)
expect(matches).to.deep.include.members([{ name: 'MyReader' }])
})
it('Check matching with multiple inputs', async () => {
const connector = getDiscoveryConnector()
const output = connector.getOutput('MyPublisher::MyWriter')
// Create / enable two inputs
getDiscoveryReaderOnlyInput()
connector.getInput('MySubscriber::MyReader')
let totalMatches = 0
// The output should match 2 inputs in total (but this may not happen
// straight away
while (totalMatches < 2) {
try {
totalMatches += await output.waitForSubscriptions(testExpectSuccessTimeout)
} catch (err) {
console.log('Caught error: ' + err)
// Fail the test
throw (err)
}
}
expect(totalMatches).to.be.at.least(2)
// Another call to waitForSubscriptions should timeout
try {
const newMatches = await output.waitForSubscriptions(testExpectFailureTimeout)
console.log('Expected waitForSubscriptions to timeout, but we matched ' + newMatches + ' subscriptions')
// Should not get here - fail the test
expect(false).to.deep.equals(true)
} catch (err) {
expect(err).to.be.an.instanceof(rti.TimeoutError)
}
const matches = output.matchedSubscriptions
expect(matches).to.deep.include.members([{ name: 'MyReader' }, { name: 'TestReader' }])
})
it('Check matching with multiple outputs', async () => {
const connector = getDiscoveryConnector()
const input = connector.getInput('MySubscriber::MyReader')
// Create / enable two outputs
connector.getOutput('MyPublisher::MyWriter')
getDiscoveryWriterOnlyOutput()
let totalMatches = 0
// The input should match 2 outputs in total (but this may not happen
// straight away
while (totalMatches < 2) {
try {
totalMatches += await input.waitForPublications(testExpectSuccessTimeout)
} catch (err) {
console.log('Caught error: ' + err)
// Fail the test
throw (err)
}
}
expect(totalMatches).to.be.at.least(2)
// Another call to waitForPublications should timeout
try {
const newMatches = await input.waitForPublications(testExpectFailureTimeout)
console.log('Expected waitForPublications to timeout, but we matched ' + newMatches + ' publications')
// Should not get here - fail the test
expect(false).to.deep.equals(true)
} catch (err) {
expect(err).to.be.an.instanceof(rti.TimeoutError)
}
const matches = input.matchedPublications
expect(matches).to.deep.include.members([{ name: 'MyWriter' }, { name: 'TestWriter' }])
})
it('Checking unmatching from an input', async function () {
const output = getDiscoveryWriterOnlyOutput()
// To begin with there is no matching
try {
const newMatches = await output.waitForSubscriptions(testExpectFailureTimeout)
console.log('Expected waitForSubscriptions to timeout, but we matched ' + newMatches + ' subscriptions')
// Should not get here - fail the test
expect(false).to.deep.equals(true)
} catch (err) {
expect(err).to.be.an.instanceof(rti.TimeoutError)
}
expect(output.matchedSubscriptions.length).to.deep.equals(0)
// Create the matching input
const input = getDiscoveryReaderOnlyInput()
// Check that both the input and output match each other
let changesInMatches = 0
let matches = []
try {
changesInMatches = await output.waitForSubscriptions(testExpectSuccessTimeout)
} catch (err) {
console.log('Caught error: ' + err)
// Fail the test
expect(false).to.deep.equals(true)
}
expect(changesInMatches).to.deep.equals(1)
matches = output.matchedSubscriptions
expect(matches.length).to.deep.equals(1)
expect(matches).to.deep.include.members([{ name: 'TestReader' }])
try {
changesInMatches = await input.waitForPublications(testExpectSuccessTimeout)
} catch (err) {
console.log('Caught error: ' + err)
// Fail the test
expect(false).to.deep.equals(true)
}
expect(changesInMatches).to.deep.equals(1)
matches = input.matchedPublications
expect(matches.length).to.deep.equals(1)
expect(matches).to.deep.include.members([{ name: 'TestWriter' }])
// Delete the Connector object that the input is within
await readerOnlyConnector.close()
readerOnlyConnector = null
// The output should unmatch from the input
try {
changesInMatches = await output.waitForSubscriptions(testExpectSuccessTimeout)
} catch (err) {
console.log('Caught error: ' + err)
// Fail the test
expect(false).to.deep.equals(true)
}
expect(changesInMatches).to.deep.equals(-1)
expect(output.matchedSubscriptions.length).to.deep.equals(0)
})
it('Checking unmatching from an output', async function () {
const input = getDiscoveryReaderOnlyInput()
// To begin with there is no matching
try {
const newMatches = await input.waitForPublications(testExpectFailureTimeout)
console.log('Expected waitForPublications to timeout, but we matched ' + newMatches + ' publications')
// Should not get here - fail the test
expect(false).to.deep.equals(true)
} catch (err) {
expect(err).to.be.an.instanceof(rti.TimeoutError)
}
expect(input.matchedPublications.length).to.deep.equals(0)
// Create the matching output
const output = getDiscoveryWriterOnlyOutput()
// Check that both the input and output match each other
let changesInMatches = 0
let matches = []
try {
changesInMatches = await input.waitForPublications(testExpectSuccessTimeout)
} catch (err) {
console.log('Caught error: ' + err)
// Fail the test
expect(false).to.deep.equals(true)
}
expect(changesInMatches).to.deep.equals(1)
matches = input.matchedPublications
expect(matches.length).to.deep.equals(1)
expect(matches).to.deep.include.members([{ name: 'TestWriter' }])
try {
changesInMatches = await output.waitForSubscriptions(testExpectSuccessTimeout)
} catch (err) {
console.log('Caught error: ' + err)
// Fail the test
expect(false).to.deep.equals(true)
}
expect(changesInMatches).to.deep.equals(1)
matches = output.matchedSubscriptions
expect(matches.length).to.deep.equals(1)
expect(matches).to.deep.include.members([{ name: 'TestReader' }])
// Delete the Connector object that the output is within
await writerOnlyConnector.close()
writerOnlyConnector = null
// The input should unmatch from the output
try {
changesInMatches = await input.waitForPublications(testExpectSuccessTimeout)
} catch (err) {
console.log('Caught error: ' + err)
// Fail the test
expect(false).to.deep.equals(true)
}
expect(changesInMatches).to.deep.equals(-1)
expect(input.matchedPublications.length).to.deep.equals(0)
})
it('Matching entities with empty entity names', async function () {
const connector = getDiscoveryConnectorNoEntityNames()
const output = connector.getOutput('MyPublisher::MyWriter')
// Ensure that the entities match
try {
const newMatches = await output.waitForSubscriptions(testExpectSuccessTimeout)
expect(newMatches).to.deep.equals(1)
} catch (err) {
console.log('Caught error: ' + err)
// Fail the test
throw (err)
}
// Get the entity names of the matched subs
const matches = output.matchedSubscriptions
expect(matches.length).to.deep.equals(1)
expect(matches).to.deep.include.members([{ name: '' }])
})
it('Matching entities with no entity names', async function () {
const output = getDiscoveryWriterOnlyOutput()
// Create a matching remote reader which has no entity name (this isn't possible
// with XML application creation)
const retcode = rti.connectorBinding.api.RTI_Connector_create_test_scenario(
output.connector.native,
0, // RTI_Connector_testScenario_createReader
output.native)
expect(retcode).to.deep.equals(0)
// Wait to match with the new reader
try {
const newMatches = await output.waitForSubscriptions(testExpectSuccessTimeout)
expect(newMatches).to.deep.equals(1)
} catch (err) {
console.log('Caught error: ' + err)
// Fail the test
throw (err)
}
const matches = output.matchedSubscriptions
expect(matches.length).to.deep.equals(1)
expect(matches).to.deep.include.members([{ name: null }])
// It is not necessary to delete the entities created by the call to createTestScenario
// since they were all created from the same DomainParticipant as output,
// which will have delete_contained_entities called on it.
})
it('waitForPublications timeout defaults to infinity', async function () {
const input = getDiscoveryReaderOnlyInput()
// Create the writer in 600ms
setTimeout(() => {
getDiscoveryWriterOnlyOutput()
}, 600)
await input.waitForPublications()
})
it('waitForSubscriptions timeout defaults to infinity', async function () {
const output = getDiscoveryWriterOnlyOutput()
// Create the reader in 600ms
setTimeout(() => {
getDiscoveryReaderOnlyInput()
}, 600)
await output.waitForSubscriptions()
})
it('waitForPublications timeout must be a valid number', function () {
const input = getDiscoveryReaderOnlyInput()
return expect(input.waitForPublications('NAN')).to.be.rejectedWith(TypeError)
})
it('waitForSubscriptions timeout must be a valid number', function () {
const output = getDiscoveryWriterOnlyOutput()
return expect(output.waitForSubscriptions('NAN')).to.be.rejectedWith(TypeError)
})
})