-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_rticonnextdds_events.js
More file actions
208 lines (191 loc) · 7.59 KB
/
test_rticonnextdds_events.js
File metadata and controls
208 lines (191 loc) · 7.59 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
/******************************************************************************
* (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 expect = require('chai').expect
const rti = require(path.join(__dirname, '/../../rticonnextdds-connector'))
const sinon = require('sinon')
const events = require('events')
// 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
describe('Connector EventEmitter tests', function () {
this.timeout(testExpectSuccessTimeout)
let connector = null
let input = null
let output = null
beforeEach(async () => {
// Create the connector object
const xmlPath = path.join(__dirname, '/../xml/TestConnector.xml')
const profile = 'MyParticipantLibrary::DataAccessTest'
connector = new rti.Connector(profile, xmlPath)
expect(connector).to.exist.and.be.an.instanceof(rti.Connector)
input = connector.getInput('TestSubscriber::TestReader')
expect(input).to.exist
output = connector.getOutput('TestPublisher::TestWriter')
expect(output).to.exist
// Wait for the entities to match
try {
const newMatches = await input.waitForPublications(testExpectSuccessTimeout)
expect(newMatches).to.be.at.least(1)
} catch (err) {
console.log('Caught err: ' + err)
throw err
}
})
afterEach(async () => {
await connector.close()
})
it('Callback should be called when event is emitted', (done) => {
var spy = sinon.spy()
connector.on('on_data_available', spy)
connector.emit('on_data_available')
expect(spy.calledOnce).to.be.true
connector.removeListener('on_data_available', spy)
done()
})
it('When no data is written, no event should be emitted', (done) => {
var spy = sinon.spy()
connector.on('on_data_available', spy)
setTimeout(() => {
expect(spy.notCalled).to.be.true
done()
}, 250)
})
it('It should not be possible to register the event listener and have a Promise waiting for data simultaneously', (done) => {
var spy = sinon.spy()
connector.on('on_data_available', spy)
// Internally, the connector's waitset is now busy
connector.wait(500)
.then(() => {
// This should not have been possible
console.log('Error occurred. Expected wait to fail due to waitSetBusy')
throw(err)
})
.catch((err) => {
expect(err.message).to.deep.equals('Can not concurrently wait on the same Connector object')
done()
})
})
it('Using .removeAllListeners() should remove all eventListeners', () => {
var spy1 = sinon.spy()
var spy2 = sinon.spy()
connector.on('on_data_available', spy1)
connector.on('on_data_available', spy2)
expect(connector.listenerCount('on_data_available')).to.deep.equals(2)
connector.removeAllListeners('on_data_available')
expect(connector.listenerCount('on_data_available')).to.deep.equals(0)
})
it('Should be possible to re-use a Connector after calling waitForCallbackFinalization', (done) => {
var spy = sinon.spy()
connector.on('on_data_available', spy)
expect(connector.listenerCount('on_data_available')).to.deep.equals(1)
connector.emit('on_data_available')
expect(spy.calledOnce).to.be.true
connector.removeListener('on_data_available', spy)
expect(connector.listenerCount('on_data_available')).to.deep.equals(0)
connector.waitForCallbackFinalization()
.then(() => {
connector.on('on_data_available', spy)
expect(connector.listenerCount('on_data_available')).to.deep.equals(1)
connector.emit('on_data_available')
expect(spy.calledTwice).to.be.true
done()
})
})
// We use the events.once() API to detect when an event has occured. It is not
// available in all versions of node (added in v11.12), so run these next
// test conditionally
if (typeof events.once === 'function') {
it('Event should be emitted when data is available on an input', (done) => {
var spy = sinon.spy()
connector.on('on_data_available', spy)
output.write()
events.once(connector, 'on_data_available')
.then(() => {
expect(spy.calledOnce).to.be.true
done()
})
})
it('Connector.once() should automatically unregister the callback after data is received', (done) => {
var spy = sinon.spy()
connector.once('on_data_available', spy)
output.write()
events.once(connector, 'on_data_available')
.then(() => {
expect(spy.calledOnce).to.be.true
expect(connector.listenerCount('on_data_available')).to.deep.equals(0)
// Writing again
output.write()
return events.once(connector, 'on_data_available')
})
.then(() => {
// Should still only have a single call
expect(spy.calledOnce).to.be.true
done()
})
})
it('Should be possible to add multiple callbacks for the same event', (done) => {
var spy1 = sinon.spy()
var spy2 = sinon.spy()
connector.on('on_data_available', spy1)
connector.on('on_data_available', spy2)
expect(connector.listenerCount('on_data_available')).to.deep.equals(2)
output.write()
events.once(connector, 'on_data_available')
.then(() => {
expect(spy1.calledOnce).to.be.true
expect(spy2.calledOnce).to.be.true
done()
})
})
it('Possible to uninstall the eventListener with .off()', (done) => {
var spy = sinon.spy()
connector.on('on_data_available', spy)
output.write()
events.once(connector, 'on_data_available')
.then(() => {
expect(spy.calledOnce).to.be.true
connector.removeListener('on_data_available', spy)
expect(connector.listenerCount('on_data_available')).to.deep.equals(0)
// Writing again
output.write()
setTimeout(() => {
// We should still only have a single call to the spy callback
expect(spy.calledOnce).to.be.true
done()
}, 250)
})
})
it('Using .off() should only unregister the supplied callback, if multiple are registered', (done) => {
var spy1 = sinon.spy()
var spy2 = sinon.spy()
connector.on('on_data_available', spy1)
connector.on('on_data_available', spy2)
expect(connector.listenerCount('on_data_available')).to.deep.equals(2)
output.write()
events.once(connector, 'on_data_available')
.then(() => {
expect(spy1.calledOnce).to.be.true
expect(spy2.calledOnce).to.be.true
connector.removeListener('on_data_available', spy1)
expect(connector.listenerCount('on_data_available')).to.deep.equals(1)
output.write()
return events.once(connector, 'on_data_available')
})
.then(() => {
expect(spy1.calledOnce).to.be.true
expect(spy2.calledTwice).to.be.true
done()
})
})
}
})