-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_rticonnextdds_data_access.js
More file actions
1131 lines (1032 loc) · 41.1 KB
/
test_rticonnextdds_data_access.js
File metadata and controls
1131 lines (1032 loc) · 41.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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* (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 os = require('os')
const ffi = require('ffi')
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 */
// 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
// These tests test the different ways to access data in Instance and SampleIterator
describe('Data access tests with a pre-populated input', function () {
let connector = null
let output = null
let prepopulatedInput = null
let sample = null
const testJsonObject = {
my_long: 10,
my_double: 3.3,
my_optional_bool: true,
my_enum: 1,
my_string: 'hello',
my_point: { x: 3, y: 4 },
my_point_alias: { x: 30, y: 40 },
my_union: { my_int_sequence: [10, 20, 30] },
my_int_union: { my_long: 222 },
my_point_sequence: [{ x: 10, y: 20 }, { x: 11, y: 21 }],
my_int_sequence: [1, 2, 3],
my_point_array: [{ x: 0, y: 0 }, { x: 0, y: 0 }, { x: 0, y: 0 }, { x: 0, y: 0 }, { x: 5, y: 15 }],
my_boolean: false,
my_int64: -18014398509481984,
my_uint64: 18014398509481984,
my_key_string: 'hello'
}
beforeEach(async () => {
const participantProfile = 'MyParticipantLibrary::DataAccessTest'
const xmlPath = path.join(__dirname, '/../xml/TestConnector.xml')
connector = new rti.Connector(participantProfile, xmlPath)
expect(connector).to.exist.and.be.an.instanceof(rti.Connector)
prepopulatedInput = connector.getInput('TestSubscriber::TestReader2')
expect(prepopulatedInput).to.exist
output = connector.getOutput('TestPublisher::TestWriter2')
expect(output).to.exist
// Wait for the input and output to dicovery each other
try {
const matches = await output.waitForSubscriptions(testExpectSuccessTimeout)
expect(matches).to.be.at.least(1)
} catch (err) {
console.log('Caught err: ' + err)
// Fail the test
throw(err)
}
// Write data on the the output
output.instance.setFromJson(testJsonObject)
output.write()
// Wait for data to arrive on input
try {
await prepopulatedInput.wait(testExpectSuccessTimeout)
} catch (err) {
console.log('Caught err: ' + err)
// Fail the test
throw(err)
}
// Take the data on the input so that we can access it from the test
prepopulatedInput.take()
expect(prepopulatedInput.samples.length).to.deep.equals(1)
sample = prepopulatedInput.samples.get(0)
expect(sample.validData).to.be.true
})
afterEach(() => {
// Take all samples here to ensure that next test case has a clean input
prepopulatedInput.take()
connector.close()
})
it('getNumber should return a number', () => {
expect(sample.getNumber('my_long')).to.deep.equals(10).and.is.a('number')
expect(sample.get('my_long')).to.deep.equals(10).and.is.a('number')
})
it('getString on a number field should return a string', () => {
expect(sample.getString('my_long')).to.deep.equals('10').and.is.a('string')
expect(sample.getString('my_double')).to.deep.equals('3.3').and.is.a('string')
})
it('getBoolean should return a boolean', () => {
expect(sample.getBoolean('my_optional_bool')).to.be.true.and.is.a('boolean')
expect(sample.get('my_optional_bool')).to.be.true.and.is.a('boolean')
})
it('getNumber on a boolean field should return a number', () => {
expect(sample.getNumber('my_optional_bool')).to.deep.equals(1).and.is.a('number')
})
it('getNumber on an enum should return the set value', () => {
expect(sample.getNumber('my_enum')).to.deep.equals(1).and.is.a('number')
})
it('access a value nested within a struct', () => {
expect(sample.getNumber('my_point.x')).to.deep.equals(3).and.is.a('number')
expect(sample.getNumber('my_point.y')).to.deep.equals(4).and.is.a('number')
})
it('access values and sizes of sequences and arrays', () => {
expect(sample.getNumber('my_point_sequence[0].y')).to.deep.equals(20).and.is.a('number')
expect(sample.get('my_point_sequence[0].y')).to.deep.equals(20).and.is.a('number')
expect(sample.getNumber('my_int_sequence[1]')).to.deep.equals(2).and.is.a('number')
expect(sample.get('my_int_sequence[1]')).to.deep.equals(2).and.is.a('number')
// The '#' appended to the type name should provide the length
expect(sample.getNumber('my_point_sequence#')).to.deep.equals(2).and.is.a('number')
expect(sample.get('my_point_sequence#')).to.deep.equals(2).and.is.a('number')
expect(sample.getNumber('my_int_sequence#')).to.deep.equals(3).and.is.a('number')
expect(sample.get('my_int_sequence#')).to.deep.equals(3).and.is.a('number')
expect(sample.getNumber('my_point_array[4].x')).to.deep.equals(5).and.is.a('number')
expect(sample.get('my_point_array[4].x')).to.deep.equals(5).and.is.a('number')
})
it('access values past the end of a sequence', () => {
expect(sample.getNumber('my_point_sequence[9].y')).to.deep.equals(null)
expect(sample.getNumber('my_int_sequence[9]')).to.deep.equals(null)
})
it('attempt to access non-existent members', () => {
expect(() => {
sample.getNumber('my_nonexistent_member')
}).to.throw(rti.DDSError)
})
it('attempt to access members with bad sequence syntax', () => {
expect(() => {
sample.getNumber('my_point_sequence[9[.y')
}).to.throw(rti.DDSError)
})
it('attempt to access the negative member of a sequence', () => {
expect(() => {
sample.getNumber('my_point_sequence[-1].y')
}).to.throw(rti.DDSError)
})
it('getNumber on unions', () => {
expect(sample.getNumber('my_union.my_int_sequence#')).to.deep.equals(3).and.is.a('number')
expect(sample.getNumber('my_union.my_int_sequence[1]')).to.deep.equals(20).and.is.a('number')
expect(sample.getNumber('my_int_union.my_long')).to.deep.equals(222).and.is.a('number')
})
it('obtain the selected member of a union with # syntax', () => {
expect(sample.getString('my_int_union#')).to.deep.equals('my_long').and.is.a('string')
expect(sample.getString('my_union#')).to.deep.equals('my_int_sequence').and.is.a('string')
expect(sample.get('my_union#')).to.deep.equals('my_int_sequence').and.is.a('string')
})
it('obtain an unset optional member', () => {
expect(sample.getNumber('my_optional_long')).to.deep.equals(null)
expect(sample.get('my_optional_long')).to.deep.equals(null)
expect(sample.getJson().my_optional_long).to.deep.equals(undefined)
})
it('obtain an unset optional member as a string', () => {
expect(sample.getString('my_optional_long')).to.deep.equals(null)
})
it('obtain an unset optional complex member', () => {
expect(sample.getNumber('my_optional_point.x')).to.deep.equals(null)
})
it('unset optional members should not be in JSON objects returned by getJSON', () => {
expect(sample.getNumber('my_optional_point.x')).to.deep.equals(null)
const jsonObj = sample.getJson()
expect(Object.prototype.hasOwnProperty.call(jsonObj, 'my_optional_point')).to.be.false
})
it('get non-existent member with getJson', () => {
expect(() => {
sample.getJson('IDoNotExist')
}).to.throw(rti.DDSError)
})
it('attempt to get non-complex members with getJson', () => {
expect(() => {
sample.getJson('my_long')
}).to.throw(rti.DDSError)
expect(() => {
sample.getJson('my_double')
}).to.throw(rti.DDSError)
expect(() => {
sample.getJson('my_optional_bool')
}).to.throw(rti.DDSError)
expect(() => {
sample.getJson('my_optional_long')
}).to.throw(rti.DDSError)
expect(() => {
sample.getJson('my_string')
}).to.throw(rti.DDSError)
expect(() => {
sample.getJson('my_enum')
}).to.throw(rti.DDSError)
expect(() => {
sample.getJson('my_point.x')
}).to.throw(rti.DDSError)
})
it('get complex members using getJson', () => {
const thePoint = sample.getJson('my_point')
expect(JSON.parse(JSON.stringify(thePoint))).to.deep.equals(thePoint)
expect(thePoint.x).to.deep.equals(3).and.is.a('number')
expect(thePoint.y).to.deep.equals(4).and.is.a('number')
const thePointAlias = sample.getJson('my_point_alias')
expect(JSON.parse(JSON.stringify(thePointAlias))).to.deep.equals(thePointAlias)
expect(thePointAlias.x).to.deep.equals(30).and.is.a('number')
expect(thePointAlias.y).to.deep.equals(40).and.is.a('number')
const theUnion = sample.getJson('my_union')
expect(JSON.parse(JSON.stringify(theUnion))).to.deep.equals(theUnion)
expect(theUnion.my_int_sequence).to.be.an.instanceof([].constructor)
.and.to.deep.equals([10, 20, 30])
const thePointSequence = sample.getJson('my_point_sequence')
expect(JSON.parse(JSON.stringify(thePointSequence))).to.deep.equals(thePointSequence)
expect(thePointSequence).to.be.an.instanceof([].constructor)
.and.to.deep.equals([{ x: 10, y: 20 }, { x: 11, y: 21 }])
const thePointSequence0 = sample.getJson('my_point_sequence[0]')
expect(JSON.parse(JSON.stringify(thePointSequence0))).to.deep.equals(thePointSequence0)
expect(thePointSequence0.x).to.deep.equals(10)
expect(thePointSequence0.y).to.deep.equals(20)
const theArray = sample.getJson('my_point_array')
expect(JSON.parse(JSON.stringify(theArray))).to.deep.equals(theArray)
const theArray0 = sample.getJson('my_point_array[0]')
expect(JSON.parse(JSON.stringify(theArray0))).to.deep.equals(theArray0)
expect(theArray0.x).to.deep.equals(0)
expect(theArray0.y).to.deep.equals(0)
})
it('get an unset optional complex member using getJson', () => {
const unsetOptionalComplex = sample.getJson('my_optional_point')
expect(unsetOptionalComplex).to.deep.equals(null)
})
// We do not run these tests on Windows since the symbols are not exported in the DLL
if (os.platform() !== 'win32') {
it('access native dynamic data pointer', () => {
const additionalApi = ffi.Library(rti.connectorBinding.library, {
DDS_DynamicData_get_member_count: ['uint', ['pointer']]
})
const memberCount = additionalApi.DDS_DynamicData_get_member_count(sample.native)
expect(memberCount).to.be.greaterThan(0)
})
}
it('get complex members using get', () => {
const thePoint = sample.get('my_point')
// Since my_point is a struct it should have been converted to a JSON object
expect(JSON.parse(JSON.stringify(thePoint))).to.deep.equals(thePoint)
expect(thePoint.x).to.deep.equals(3)
expect(thePoint.y).to.deep.equals(4)
const thePointSequence = sample.get('my_point_sequence')
expect(JSON.parse(JSON.stringify(thePointSequence))).to.deep.equals(thePointSequence)
expect(thePointSequence).to.be.an.instanceof([].constructor)
expect(thePointSequence[0]).to.deep.equals({ x: 10, y: 20 })
expect(thePointSequence[1]).to.deep.equals({ x: 11, y: 21 })
const thePointArray = sample.get('my_point_array')
expect(JSON.parse(JSON.stringify(thePointArray))).to.deep.equals(thePointArray)
expect(thePointArray).to.be.an.instanceof([].constructor)
expect(thePointArray[0]).to.deep.equals({ x: 0, y: 0 })
expect(thePointArray[4]).to.deep.equals({ x: 5, y: 15 })
const thePointAlias = sample.get('my_point_alias')
// Alias should be resolved so we now have a JSON object
expect(JSON.parse(JSON.stringify(thePointAlias))).to.deep.equals(thePointAlias)
expect(thePointAlias.x).to.deep.equals(30)
expect(thePointAlias.y).to.deep.equals(40)
const theOptionalPoint = sample.get('my_optional_point')
// Unset optional should return null
expect(theOptionalPoint).to.deep.equals(null)
const theUnion = sample.get('my_union')
// Since no trailing '#' was supplied we should now have the JSON object
expect(JSON.parse(JSON.stringify(theUnion))).to.deep.equals(theUnion)
expect(theUnion).to.deep.equals({ my_int_sequence: [10, 20, 30] })
})
it('Try to obtain complex members with getNumber', () => {
expect(() => {
sample.getNumber('my_point')
}).to.throw(rti.DDSError)
})
it('Try to obtain complex members with getBoolean', () => {
expect(() => {
sample.getBoolean('my_point')
}).to.throw(rti.DDSError)
})
it('Try to obtain complex members with getString', () => {
// It should be possible to complex members with getString, but the returned
// object will have a type of 'string', not a JSON object
const complexString = sample.getString('my_point')
expect(complexString).to.be.a('string')
// The string should be parsable by JSON
const complexJson = JSON.parse(complexString)
expect(complexJson).to.be.an.instanceof({}.constructor)
expect(complexJson.x).to.deep.equals(3)
})
it('Try to obtain complex arrays with getString', () => {
// It should be possible to complex members with getString, but the returned
// object will have a type of 'string', not a JSON object
const complexString = sample.getString('my_point_array')
expect(complexString).to.be.a('string')
// The string should be parsable by JSON
const complexJson = JSON.parse(complexString)
expect(complexJson).to.be.an.instanceof([].constructor)
expect(complexJson[0].x).to.deep.equals(0)
})
it('Obtain JSON string of dictionary', () => {
const jsonInstance = output.instance.getJson()
expect(jsonInstance).to.deep.equals(testJsonObject)
})
})
describe('Tests with a testOutput and testInput', () => {
let connector = null
let testOutput = null
let testInput = null
const testJsonObject = {
my_long: 10,
my_double: 3.3,
my_optional_bool: true,
my_enum: 1,
my_string: 'hello',
my_point: { x: 3, y: 4 },
my_point_alias: { x: 30, y: 40 },
my_union: { my_int_sequence: [10, 20, 30] },
my_int_union: { my_long: 222 },
my_point_sequence: [{ x: 10, y: 20 }, { x: 11, y: 21 }],
my_int_sequence: [1, 2, 3],
my_point_array: [{ x: 0, y: 0 }, { x: 0, y: 0 }, { x: 0, y: 0 }, { x: 0, y: 0 }, { x: 5, y: 15 }],
my_boolean: false,
my_int64: -18014398509481984,
my_uint64: 18014398509481984,
my_key_string: 'hello'
}
beforeEach(async () => {
const participantProfile = 'MyParticipantLibrary::DataAccessTest'
const xmlProfile = path.join(__dirname, '/../xml/TestConnector.xml')
connector = new rti.Connector(participantProfile, xmlProfile)
expect(connector).to.exist.and.be.an.instanceof(rti.Connector)
testInput = connector.getInput('TestSubscriber::TestReader2')
expect(testInput).to.exist
testOutput = connector.getOutput('TestPublisher::TestWriter2')
expect(testOutput).to.exist
// Wait for the input and output to dicovery each other
try {
const newMatches = await testOutput.waitForSubscriptions(testExpectSuccessTimeout)
expect(newMatches).to.deep.equals(1)
} catch (err) {
console.log('Caught err ' + err)
throw(err)
}
})
afterEach(() => {
// Take all samples here to ensure that next test case has a clean input
testInput.take()
connector.close()
})
if (os.platform() !== 'win32') {
it('test native API on output', () => {
const additionalApi = ffi.Library(rti.connectorBinding.library, {
DDS_DynamicData_get_member_count: ['uint', ['pointer']]
})
const memberCount = additionalApi.DDS_DynamicData_get_member_count(testOutput.instance.native)
expect(memberCount).to.be.greaterThan(0)
})
}
it('pass null as field name to setX APIs on output', () => {
expect(() => {
testOutput.instance.setBoolean(null, true)
}).to.throw(TypeError)
expect(() => {
testOutput.instance.setNumber(null, 1)
}).to.throw(TypeError)
expect(() => {
testOutput.instance.setString(null, 'hello')
}).to.throw(TypeError)
})
it('try to set a number with a string', () => {
expect(() => {
testOutput.instance.setNumber('my_long', 'hello')
}).to.throw(TypeError)
})
it('try to set a boolean with a string', () => {
expect(() => {
testOutput.instance.setBoolean('my_optional_bool', 'hello')
}).to.throw(TypeError)
})
it('try to set non-existent field names', () => {
expect(() => {
testOutput.instance.setNumber('NonExistent', 1)
}).to.throw(rti.DDSError)
expect(() => {
testOutput.instance.setBoolean('NonExistent', false)
}).to.throw(rti.DDSError)
expect(() => {
testOutput.instance.setString('NonExistent', 'hello')
}).to.throw(rti.DDSError)
})
it('Supply a JSON object where everything is a string', async () => {
testOutput.instance.setFromJson({
my_long: '10',
my_double: '3.3',
my_optional_bool: true,
my_enum: '1',
my_string: 'hello',
my_point: { x: '3', y: '4' },
my_point_alias: { x: '30', y: '40' },
my_union: { my_int_sequence: ['10', '20', '30'] },
my_int_union: { my_long: '222' },
my_point_sequence: [{ x: '10', y: '20' }, { x: '11', y: '21' }],
my_int_sequence: ['1', '2', '3'],
my_point_array: [{ x: '0', y: '0' }, { x: '0', y: '0' }, { x: '0', y: '0' }, { x: '0', y: '0' }, { x: '5', y: '15' }],
my_boolean: false,
my_int64: '-18014398509481984',
my_uint64: '18014398509481984',
my_key_string: 'hello'
})
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
console.log('Caught err: ' + err)
// Fail the test
expect(false).to.deep.equals(true)
}
testInput.take()
const receivedJsonObject = testInput.samples.get(0).getJson()
expect(receivedJsonObject).to.deep.equals(testJsonObject)
})
it('Bad conversion from string in JSON object', () => {
// For each numeric field, test that setFromJson fails when the value we provide
// does not represent a number
const fieldNames = [
'my_long',
'my_int64',
'my_double',
'my_point_array[1]',
'my_int_sequence[1]',
'my_enum',
'my_uint64']
for (const field of fieldNames) {
expect(() => {
testOutput.instance.setFromJson({ field: 'this is not a number' })
console.log(field + ' did not raise an exception')
}).to.throw(rti.DDSError)
}
})
it('Attempt to access past the end of a sequence using setFromJson', async () => {
expect(() => {
// my_int_sequence has a bound of 10 and we are supplying 11 elements
testOutput.instance.setFromJson({ my_int_sequence: [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10] })
}).to.throw(rti.DDSError)
// Ensure that the previous error didn't corrupt the instance
const sent = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
testOutput.instance.set('my_int_sequence', sent)
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
console.log('Caught err: ' + err)
throw(err)
}
testInput.take()
const received = testInput.samples.get(0).get('my_int_sequence')
expect(received).to.deep.equals(sent)
})
it('Attempt to pass an invalid JSON object to setFromJson', async () => {
expect(() => {
testOutput.instance.setFromJson({ my_point_sequence: [{ x: 1, y: 2 }, { x: 3, bad: 4 }] })
}).to.throw(rti.DDSError)
// Ensure that the previous error did not corrupt the instance
const sent = [{ x: 1, y: 2 }, { x: 3, y: 4 }]
testOutput.instance.set('my_point_sequence', sent)
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
console.log('Caught error: ' + err)
throw(err)
}
testInput.take()
const received = testInput.samples.get(0).get('my_point_sequence')
expect(received).to.deep.equals(sent)
})
it('The type-independent get should return the same result as getJson', async () => {
testOutput.instance.setFromJson({ my_point_sequence: [{ x: 1, y: 2 }, { x: 3, y: 4 }] })
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
console.log('Caught error: ' + err)
throw(err)
}
testInput.take()
const sample = testInput.samples.get(0)
expect(sample.getJson('my_point_sequence')).to.deep.equals(sample.get('my_point_sequence'))
})
it('Set a boolean field using setNumber and check the resultant value on an input', async () => {
testOutput.instance.setNumber('my_optional_bool', 1)
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
console.log('Caught err: ' + err)
// Fail the test
expect(false).to.deep.equals(true)
}
testInput.take()
const theOptionalBool = testInput.samples.get(0).get('my_optional_bool')
expect(theOptionalBool).to.be.a('boolean').and.deep.equals(true)
})
it('Set a string with a number and check the resultant value on an input', async () => {
testOutput.instance.setString('my_string', '1234')
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
console.log('Caught err: ' + err)
// Fail the test
expect(false).to.deep.equals(true)
}
testInput.take()
const theNumericString = testInput.samples.get(0).get('my_string')
// Due to CON-139 get returns strings as numbers if they represent a number
expect(theNumericString).to.be.a('number').and.deep.equals(1234)
})
it('Test output sequences', async () => {
testOutput.instance.setNumber('my_point_sequence[0].y', 20)
testOutput.instance.setNumber('my_int_sequence[1]', 2)
testOutput.instance.setNumber('my_point_array[4].x', 5)
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
// Fail the test
console.log('Caught error: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
const sample = testInput.samples.get(0)
expect(sample.get('my_point_sequence[0].y')).to.be.a('number').and.deep.equals(20)
expect(sample.get('my_int_sequence[1]')).to.be.a('number').and.deep.equals(2)
expect(sample.get('my_point_array[4].x')).to.be.a('number').and.deep.equals(5)
expect(sample.get('my_point_sequence#')).to.be.a('number').and.deep.equals(1)
expect(sample.get('my_int_sequence#')).to.be.a('number').and.deep.equals(2)
})
it('Change union members', async () => {
testOutput.instance.setNumber('my_union.my_int_sequence[1]', 3)
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
// Fail the test
console.log('Caught error: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
let sample = testInput.samples.get(0)
expect(sample.getString('my_union#')).to.be.a('string').that.deep.equals('my_int_sequence')
// Change the union to long
testOutput.instance.setNumber('my_union.my_long', 3)
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
// Fail the test
console.log('Caught error: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
sample = testInput.samples.get(0)
expect(sample.getString('my_union#')).to.be.a('string').that.deep.equals('my_long')
expect(sample.getNumber('my_union.my_long')).to.be.a('number').that.deep.equals(3)
})
it('Change union members', async () => {
testOutput.instance.setNumber('my_union.my_int_sequence[1]', 3)
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
// Fail the test
console.log('Caught error: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
let sample = testInput.samples.get(0)
expect(sample.getString('my_union#')).to.deep.equals('my_int_sequence')
.and.is.a('string')
// Change the union
testOutput.instance.setNumber('my_union.my_long', 3)
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
console.log('Caught error: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
sample = testInput.samples.get(0)
expect(sample.getString('my_union#')).to.deep.equals('my_long')
.and.is.a('string')
})
it('Set an optional', async () => {
testOutput.instance.setNumber('my_optional_point.x', 101)
testOutput.instance.setNumber('my_point_alias.x', 202)
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
// Fail the test
console.log('Caught error: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
const sample = testInput.samples.get(0)
expect(sample.getNumber('my_optional_point.x')).to.deep.equals(101)
.and.is.a('number')
expect(sample.getNumber('my_point_alias.x')).to.deep.equals(202)
.and.is.a('number')
})
it('Get an unset optional boolean', async () => {
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
// Fail the test
console.log('Caught error: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
const unsetOptional = testInput.samples.get(0).getBoolean('my_optional_bool')
expect(unsetOptional).to.deep.equals(null)
})
it('Reset an optional number', async () => {
testOutput.instance.setNumber('my_optional_long', 33)
testOutput.instance.setNumber('my_optional_long', null)
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
// Fail the test
console.log('Error caught: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
const sample = testInput.samples.get(0)
expect(sample.getNumber('my_optional_long'))
.to.deep.equals(null)
expect(Object.prototype.hasOwnProperty.call(sample.getJson(), 'my_optional_long'))
.to.be.false
})
it('Reset an optional bool', async () => {
testOutput.instance.setBoolean('my_optional_bool', true)
testOutput.instance.setBoolean('my_optional_bool', null)
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
// Fail the test
console.log('Error caught: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
const sample = testInput.samples.get(0)
expect(sample.getNumber('my_optional_bool'))
.to.deep.equals(null)
expect(Object.prototype.hasOwnProperty.call(sample.getJson(), 'my_optional_bool'))
.to.be.false
})
it('Reset an optional complex', async () => {
testOutput.instance.setNumber('my_optional_point.x', 44)
testOutput.instance.setNumber('my_point_alias.x', 55)
testOutput.instance.clearMember('my_optional_point')
testOutput.instance.clearMember('my_point_alias')
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
// Fail the test
console.log('Error caught: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
const sample = testInput.samples.get(0)
expect(sample.getNumber('my_optional_point.x'))
.to.deep.equals(null)
expect(Object.prototype.hasOwnProperty.call(sample.getJson(), 'my_optional_point'))
.to.be.false
expect(sample.getNumber('my_point_alias.x'))
.to.deep.equals(null)
expect(Object.prototype.hasOwnProperty.call(sample.getJson(), 'my_point_alias'))
.to.be.false
})
it('Clear a sequence', async () => {
testOutput.instance.setNumber('my_union.my_int_sequence[2]', 10)
testOutput.instance.setNumber('my_point.x', 3)
testOutput.instance.clearMember('my_union.my_int_sequence')
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
// Fail the test
console.log('Error caught: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
const sample = testInput.samples.get(0)
expect(sample.getNumber('my_union.my_int_sequence#')).to.deep.equals(0)
expect(sample.getNumber('my_point.x')).to.deep.equals(3)
})
it('Clear a sequence with a JSON object', async () => {
// Set the non-default values
testOutput.instance.setFromJson(testJsonObject)
testOutput.instance.setBoolean('my_optional_bool', true)
// Reset members using null in a JSON object. Optional members are set to null,
// other members are initialized to their default value
testOutput.instance.setFromJson({
my_optional_point: null,
my_optional_long: null,
my_point: null,
my_point_alias: null,
my_long: null,
my_optional_bool: null,
my_point_sequence: null,
my_string: null,
my_union: null,
my_enum: null
})
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
// Fail the test
console.log('Error caught: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
const sample = testInput.samples.get(0)
expect(sample.getNumber('my_optional_point.x')).to.deep.equals(null)
expect(sample.getNumber('my_optional_long')).to.deep.equals(null)
expect(sample.getNumber('my_point.x')).to.deep.equals(0)
expect(sample.getNumber('my_point.y')).to.deep.equals(0)
expect(sample.getNumber('my_point_alias.y')).to.deep.equals(null)
expect(sample.getNumber('my_long')).to.deep.equals(0)
expect(sample.getBoolean('my_optional_bool')).to.deep.equals(null)
expect(sample.getNumber('my_point_sequence#')).to.deep.equals(0)
expect(sample.getString('my_string')).to.deep.equals('')
expect(sample.getString('my_union#')).to.deep.equals('point')
expect(sample.getNumber('my_enum')).to.deep.equals(2)
const jsonObj = sample.getJson()
expect(Object.prototype.hasOwnProperty.call(jsonObj, 'my_optional_bool')).to.be.false
expect(Object.prototype.hasOwnProperty.call(jsonObj, 'my_optional_long')).to.be.false
expect(Object.prototype.hasOwnProperty.call(jsonObj, 'my_point_alias')).to.be.false
expect(Object.prototype.hasOwnProperty.call(jsonObj, 'my_optional_point')).to.be.false
})
it('Clear a non-existent member', () => {
expect(() => {
testOutput.instance.clearMember('nonexistent_member')
}).to.throw(rti.DDSError)
})
it('Reset a sequence', async () => {
testOutput.instance.setNumber('my_union.my_int_sequence[2]', 10)
testOutput.instance.setNumber('my_point.x', 3)
testOutput.instance.setNumber('my_point_sequence[1].x', 44)
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
// Fail the test
console.log('Error caught: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
let sample = testInput.samples.get(0)
expect(sample.getNumber('my_union.my_int_sequence#')).to.deep.equals(3)
expect(sample.getNumber('my_point.x')).to.deep.equals(3)
expect(sample.getNumber('my_point_sequence#')).to.deep.equals(2)
testOutput.instance.setFromJson({ my_int_sequence: [] })
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
// Fail the test
console.log('Error caught: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
sample = testInput.samples.get(0)
expect(sample.getNumber('my_int_sequence#')).to.deep.equals(0)
// The other fields are unchanged
expect(sample.getNumber('my_point.x')).to.deep.equals(3)
expect(sample.getNumber('my_point_sequence#')).to.deep.equals(2)
})
it('Check that setFromJson shrinks a sequence when it receives a smaller one', async () => {
// Set the length to 3
testOutput.instance.setNumber('my_int_sequence[2]', 10)
testOutput.instance.setNumber('my_point_sequence[0].x', 11)
testOutput.instance.setNumber('my_point_sequence[0].y', 12)
testOutput.instance.setNumber('my_point_sequence[2].x', 10)
testOutput.instance.setFromJson({
my_point_array: [
{ x: 10, y: 20 },
{ x: 11, y: 21 },
{ x: 12, y: 22 },
{ x: 13, y: 23 },
{ x: 14, y: 24 }]
})
// Reduce sequences to a length of 1 (arrays should retain existing values)
testOutput.instance.setFromJson({
my_int_sequence: [40],
my_point_sequence: [{ y: 2 }],
my_point_array: [{ x: 100 }, { y: 200 }]
})
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
// Fail the test
console.log('Error caught: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
const sample = testInput.samples.get(0)
expect(sample.getNumber('my_int_sequence#')).to.deep.equals(1)
expect(sample.getNumber('my_point_sequence#')).to.deep.equals(1)
expect(sample.getNumber('my_int_sequence[0]')).to.deep.equals(40)
expect(sample.getNumber('my_point_sequence[0].y')).to.deep.equals(2)
expect(sample.getNumber('my_point_sequence[0].x')).to.deep.equals(0)
expect(sample.getNumber('my_point_array[0].x')).to.deep.equals(100)
expect(sample.getNumber('my_point_array[0].y')).to.deep.equals(20)
expect(sample.getNumber('my_point_array[4].x')).to.deep.equals(14)
})
it('Check the type-independent Instance.set and Sample.get method', async () => {
// Set one of each type using the type-independent set API
testOutput.instance.set('my_string', 'Hello, World!')
testOutput.instance.set('my_boolean', true)
testOutput.instance.set('my_int64', 42)
testOutput.instance.set('my_point_sequence[0].x', 3)
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
// Fail the test
console.log('Error caught: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
const sample = testInput.samples.get(0)
expect(sample.get('my_string')).to.deep.equals('Hello, World!')
expect(sample.get('my_boolean')).to.deep.equals(true)
expect(sample.get('my_int64')).to.deep.equals(42)
expect(sample.get('my_point_sequence[0].x')).to.deep.equals(3)
})
it('Reset an optional member using the type independent set method', async () => {
testOutput.instance.set('my_optional_bool', null)
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
// Fail the test
console.log('Error caught: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
expect(testInput.samples.get(0).get('my_optional_bool')).to.deep.equals(null)
})
it('Test nested JSON object syntax', async () => {
testOutput.instance.setFromJson({ 'my_point_sequence[2].y': 153 })
testOutput.instance.setFromJson({ 'my_point_sequence[2].x': 111 })
testOutput.instance.set('my_point_sequence[3]', { x: 444, y: 555 })
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
console.log('Error caught: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
expect(testInput.samples.get(0).get('my_point_sequence[2]')).to.deep.equals({ x: 111, y: 153 })
expect(testInput.samples.get(0).get('my_point_sequence[3]')).to.deep.equals({ x: 444, y: 555 })
})
// Confirm desired behaviour for this
it('Use Instance.set to set a complex member', async () => {
const jsonObj = { x: 9, y: 12 }
testOutput.instance.set('my_point', jsonObj)
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
// Fail the test
console.log('Error caught: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
expect(testInput.samples.get(0).get('my_point')).to.deep.equals(jsonObj)
})
it('Use Instance.set to set a list', async () => {
const intSeq = [11, 22, 33]
const pointSeq = [{ x: 100, y: 200 }, { x: 300, y: 400 }]
testOutput.instance.set('my_int_sequence', intSeq)
testOutput.instance.set('my_point_sequence', pointSeq)
testOutput.write()
try {
await testInput.wait(testExpectSuccessTimeout)
} catch (err) {
console.log('Error caught: ' + err)
expect(false).to.deep.equals(true)
}
testInput.take()
expect(testInput.samples.get(0).get('my_int_sequence')).to.deep.equals(intSeq)
expect(testInput.samples.get(0).get('my_point_sequence')).to.deep.equals(pointSeq)
})
})
describe('Tests with two readers and two writers', () => {
let connector = null
let testOutput1 = null
let testInput1 = null
let testOutput2 = null
let testInput2 = null
beforeEach(async () => {