-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtest_datawriter.py
More file actions
563 lines (475 loc) · 18.4 KB
/
Copy pathtest_datawriter.py
File metadata and controls
563 lines (475 loc) · 18.4 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
import datetime
import fastdds
import pytest
import time
class DataWriterListener (fastdds.DataWriterListener):
def __init__(self):
super().__init__()
@pytest.fixture(params=['no_module', 'module'], autouse=True)
def data_type(request):
if request.param == 'no_module':
pytest.dds_type = __import__("test_complete")
else:
pytest.dds_type = __import__("eprosima.test.test_modules",
fromlist=["test_modules"])
@pytest.fixture
def participant():
factory = fastdds.DomainParticipantFactory.get_instance()
return factory.create_participant(
0, fastdds.PARTICIPANT_QOS_DEFAULT)
@pytest.fixture
def publisher(participant):
return participant.create_publisher(fastdds.PUBLISHER_QOS_DEFAULT)
@pytest.fixture
def test_type():
return fastdds.TypeSupport(
pytest.dds_type.CompleteTestTypePubSubType())
@pytest.fixture
def test_keyed_type(test_type):
test_type.set(pytest.dds_type.KeyedCompleteTestTypePubSubType())
@pytest.fixture
def topic(participant, test_type):
participant.register_type(test_type, test_type.get_type_name())
return participant.create_topic(
"Complete", test_type.get_type_name(), fastdds.TOPIC_QOS_DEFAULT)
@pytest.fixture
def datawriter_qos():
return fastdds.DataWriterQos()
@pytest.fixture
def manual_liveliness_datawriter_qos(datawriter_qos):
datawriter_qos.liveliness().kind = \
fastdds.MANUAL_BY_PARTICIPANT_LIVELINESS_QOS
return datawriter_qos
@pytest.fixture
def keep_all_datawriter_qos(datawriter_qos):
datawriter_qos.history().kind = \
fastdds.KEEP_ALL_HISTORY_QOS
return datawriter_qos
@pytest.fixture
def datawriter(participant, topic, publisher, datawriter_qos):
datawriter = publisher.create_datawriter(topic, datawriter_qos)
yield datawriter
assert(fastdds.RETCODE_OK ==
publisher.delete_datawriter(datawriter))
assert(fastdds.RETCODE_OK ==
participant.delete_topic(topic))
assert(fastdds.RETCODE_OK ==
participant.delete_publisher(publisher))
factory = fastdds.DomainParticipantFactory.get_instance()
assert(fastdds.RETCODE_OK ==
factory.delete_participant(participant))
@pytest.fixture
def reader_participant():
factory = fastdds.DomainParticipantFactory.get_instance()
return factory.create_participant(
0, fastdds.PARTICIPANT_QOS_DEFAULT)
@pytest.fixture
def reader_topic(reader_participant, test_type):
reader_participant.register_type(test_type, test_type.get_type_name())
return reader_participant.create_topic(
"Complete", test_type.get_type_name(), fastdds.TOPIC_QOS_DEFAULT)
@pytest.fixture
def subscriber(reader_participant):
return reader_participant.create_subscriber(fastdds.SUBSCRIBER_QOS_DEFAULT)
def test_assert_liveliness(manual_liveliness_datawriter_qos, datawriter):
"""
This test checks:
- DataWriter::assert_liveliness
"""
assert(fastdds.RETCODE_OK ==
datawriter.assert_liveliness())
def test_clear_history(keep_all_datawriter_qos, datawriter):
"""
This test checks:
- DataWriter::clear_history
"""
sample = pytest.dds_type.CompleteTestType()
sample.int16_field(4)
assert(fastdds.RETCODE_OK == datawriter.write(sample))
assert([fastdds.RETCODE_OK, 1] ==
datawriter.clear_history())
def test_dispose(test_keyed_type, datawriter):
"""
This test checks:
- DataWriter::dispose
- DataWriter::dispose_w_timestamp
- DataWriter::unregister_instance
"""
# Overload 1
sample = pytest.dds_type.KeyedCompleteTestType()
sample.id(1)
ih = datawriter.register_instance(sample)
assert(fastdds.c_InstanceHandle_Unknown != ih)
sample2 = pytest.dds_type.KeyedCompleteTestType()
sample2.id(2)
ih2 = datawriter.register_instance(sample2)
assert(fastdds.c_InstanceHandle_Unknown != ih2)
assert(ih2 != ih)
assert(fastdds.RETCODE_OK ==
datawriter.dispose(sample, ih))
assert(fastdds.RETCODE_OK ==
datawriter.dispose(sample2, ih2))
# Overload 2
sample = pytest.dds_type.KeyedCompleteTestType()
sample.id(3)
now = datetime.datetime.now().time()
timestamp = fastdds.Time_t()
timestamp.seconds = now.second
ih = datawriter.register_instance_w_timestamp(sample, timestamp)
assert(fastdds.c_InstanceHandle_Unknown != ih)
assert(fastdds.RETCODE_OK ==
datawriter.dispose_w_timestamp(sample, ih, timestamp))
def test_get_instance_handle(datawriter):
"""
This test checks:
- DataWriter::guid
- DataWriter::get_instance_handle
"""
guid = datawriter.guid()
assert(fastdds.c_Guid_Unknown != guid)
ih = datawriter.get_instance_handle()
assert(fastdds.c_InstanceHandle_Unknown != ih)
for i in range(0, 12):
assert(guid.guidPrefix.value[i] == ih.value[i])
for i in range(0, 4):
assert(guid.entityId.value[i] == ih.value[12+i])
def test_get_key_value(test_keyed_type, datawriter):
"""
This test checks:
- DataWriter::get_key_value
"""
# Prepare test variables
sample = pytest.dds_type.KeyedCompleteTestType()
sample.id(1)
ih = datawriter.register_instance(sample)
assert(fastdds.c_InstanceHandle_Unknown != ih)
# Check wrong handle
test_sample = pytest.dds_type.KeyedCompleteTestType()
assert(fastdds.RETCODE_BAD_PARAMETER ==
datawriter.get_key_value(test_sample, fastdds.c_InstanceHandle_Unknown))
# Check wrong sample
assert(fastdds.RETCODE_BAD_PARAMETER ==
datawriter.get_key_value(None, ih))
# Check correct case
test_sample = pytest.dds_type.KeyedCompleteTestType()
assert(fastdds.RETCODE_OK ==
datawriter.get_key_value(test_sample, ih))
assert(test_sample.id() == sample.id());
# Calling get_key_value on an unregistered instance should fail
test_sample = pytest.dds_type.KeyedCompleteTestType()
assert(fastdds.RETCODE_OK ==
datawriter.unregister_instance(sample, ih))
assert(fastdds.RETCODE_BAD_PARAMETER ==
datawriter.get_key_value(test_sample, ih))
# Calling get_key_value with a valid instance should work
test_sample = pytest.dds_type.KeyedCompleteTestType()
assert(fastdds.RETCODE_OK ==
datawriter.write(sample, fastdds.c_InstanceHandle_Unknown))
assert(fastdds.RETCODE_OK ==
datawriter.get_key_value(test_sample, ih))
assert(test_sample.id() == sample.id());
# Calling get_key_value on a disposed instance should work
test_sample = pytest.dds_type.KeyedCompleteTestType()
assert(fastdds.RETCODE_OK ==
datawriter.dispose(sample, ih))
assert(fastdds.RETCODE_OK ==
datawriter.get_key_value(test_sample, ih))
assert(test_sample.id() == sample.id());
def test_get_sending_locators(datawriter):
"""
This test checks:
- DataWriter::get_sending_locators
"""
locator_list = fastdds.LocatorList()
assert(fastdds.RETCODE_OK ==
datawriter.get_sending_locators(locator_list))
assert(0 < locator_list.size())
def test_get_set_listener(datawriter):
"""
This test checks:
- DataWriter::get_listener
- DataWriter::set_listener
- DataWriter::get_status_mask
- StatusMask::operator ==
- StatusMask::operator <<
"""
# Overload 1
listener = DataWriterListener()
assert(listener is not None)
assert(fastdds.RETCODE_OK ==
datawriter.set_listener(listener))
assert(datawriter.get_listener() == listener)
assert(fastdds.StatusMask.all() == datawriter.get_status_mask())
def test(status_mask):
"""
Test the entity creation using the type of StatusMask.
"""
listener = DataWriterListener()
assert(listener is not None)
assert(fastdds.RETCODE_OK ==
datawriter.set_listener(listener, status_mask))
assert(datawriter.get_listener() == listener)
assert(status_mask == datawriter.get_status_mask())
# Overload 2: Different status masks
test(fastdds.StatusMask.all())
test(fastdds.StatusMask.none())
test(fastdds.StatusMask.data_available())
test(fastdds.StatusMask.data_on_readers())
test(fastdds.StatusMask.inconsistent_topic())
test(fastdds.StatusMask.liveliness_changed())
test(fastdds.StatusMask.liveliness_lost())
test(fastdds.StatusMask.offered_deadline_missed())
test(fastdds.StatusMask.offered_incompatible_qos())
test(fastdds.StatusMask.publication_matched())
test(fastdds.StatusMask.requested_deadline_missed())
test(fastdds.StatusMask.requested_incompatible_qos())
test(fastdds.StatusMask.sample_lost())
test(fastdds.StatusMask.sample_rejected())
test(fastdds.StatusMask.subscription_matched())
test(fastdds.StatusMask.data_available() <<
fastdds.StatusMask.data_on_readers() <<
fastdds.StatusMask.inconsistent_topic() <<
fastdds.StatusMask.liveliness_changed() <<
fastdds.StatusMask.liveliness_lost() <<
fastdds.StatusMask.offered_deadline_missed() <<
fastdds.StatusMask.offered_incompatible_qos() <<
fastdds.StatusMask.publication_matched() <<
fastdds.StatusMask.requested_deadline_missed() <<
fastdds.StatusMask.requested_incompatible_qos() <<
fastdds.StatusMask.sample_lost() <<
fastdds.StatusMask.sample_rejected() <<
fastdds.StatusMask.subscription_matched())
def test_get_liveliness_lost_status(datawriter):
"""
This test checks:
- DataWriter::get_liveliness_lost_status
"""
status = fastdds.LivelinessLostStatus()
assert(fastdds.RETCODE_OK ==
datawriter.get_liveliness_lost_status(status))
assert(0 == status.total_count)
assert(0 == status.total_count_change)
def test_get_matched_subscription_data(datawriter, reader_topic, subscriber):
"""
This test checks:
- DataWriter::get_matched_subscription_data
"""
# Check with an invalid instance handle
sub_data = fastdds.SubscriptionBuiltinTopicData()
ih = fastdds.InstanceHandle_t()
assert(fastdds.RETCODE_BAD_PARAMETER ==
datawriter.get_matched_subscription_data(sub_data, ih))
# Add a reader and check that the datawriter has matched subscriptions
datareader = subscriber.create_datareader(reader_topic, fastdds.DATAREADER_QOS_DEFAULT)
time.sleep(1)
assert(fastdds.RETCODE_OK ==
datawriter.get_matched_subscription_data(sub_data, datareader.get_instance_handle()))
assert(sub_data.guid == datareader.guid())
assert(fastdds.RETCODE_OK ==
subscriber.delete_datareader(datareader))
def test_get_matched_subscriptions(datawriter, reader_topic, subscriber):
"""
This test checks:
- DataWriter::get_matched_subscriptions
"""
ihs = fastdds.InstanceHandleVector()
# Add a reader and check that the datawriter has matched subscriptions
datareader = subscriber.create_datareader(reader_topic, fastdds.DATAREADER_QOS_DEFAULT)
time.sleep(1)
assert(fastdds.RETCODE_OK == datawriter.get_matched_subscriptions(ihs))
assert(1 == ihs.size())
assert(ihs[0] == datareader.get_instance_handle())
assert(fastdds.RETCODE_OK ==
subscriber.delete_datareader(datareader))
def test_get_offered_deadline_missed_status(datawriter):
"""
This test checks:
- DataWriter::get_offered_deadline_missed_status
"""
status = fastdds.OfferedDeadlineMissedStatus()
assert(fastdds.RETCODE_OK ==
datawriter.get_offered_deadline_missed_status(status))
assert(0 == status.total_count)
assert(0 == status.total_count_change)
assert(fastdds.c_InstanceHandle_Unknown == status.last_instance_handle)
def test_get_offered_incompatible_qos_status(datawriter):
"""
This test checks:
- DataWriter::get_offered_deadline_missed_status
"""
status = fastdds.OfferedIncompatibleQosStatus()
assert(fastdds.RETCODE_OK ==
datawriter.get_offered_incompatible_qos_status(status))
assert(0 == status.total_count)
assert(0 == status.total_count_change)
assert(fastdds.INVALID_QOS_POLICY_ID == status.last_policy_id)
assert(fastdds.NEXT_QOS_POLICY_ID == status.policies.size())
id = 0
for policy in status.policies:
assert(0 == policy.count)
assert(id == policy.policy_id)
id += 1
def test_get_publication_matched_status(datawriter):
"""
This test checks:
- DataWriter::get_publication_matched_status
"""
status = fastdds.PublicationMatchedStatus()
assert(fastdds.RETCODE_OK ==
datawriter.get_publication_matched_status(status))
assert(0 == status.total_count)
assert(0 == status.total_count_change)
assert(0 == status.current_count)
assert(0 == status.current_count_change)
assert(fastdds.c_InstanceHandle_Unknown == status.last_subscription_handle)
def test_get_publisher(publisher, datawriter):
"""
This test checks:
- DataWriter::get_publisher
"""
pub = datawriter.get_publisher()
assert(pub == publisher)
def test_get_type(test_type, datawriter):
"""
This test checks:
- DataWriter::get_type
"""
test_type_aux = datawriter.get_type()
assert(test_type == test_type_aux)
assert(test_type.get_type_name() == test_type_aux.get_type_name())
def test_get_topic(topic, datawriter):
"""
This test checks:
- DataWriter::get_topic
"""
topic_aux = datawriter.get_topic()
assert(topic == topic_aux)
assert(topic.get_type_name() == topic_aux.get_type_name())
def test_lookup_instance(test_keyed_type, datawriter):
"""
This test checks:
- DataWriter::lookup_instance
"""
sample = pytest.dds_type.KeyedCompleteTestType()
sample.id(3)
ih = datawriter.lookup_instance(sample)
assert(fastdds.c_InstanceHandle_Unknown == ih)
def test_register_instance(test_keyed_type, datawriter):
"""
This test checks:
- DataWriter::register_instance
- DataWriter::register_instance_w_timestamp
- DataWriter::unregister_instance
- DataWriter::unregister_instance_w_timestamp
"""
# Overload 1
sample = pytest.dds_type.KeyedCompleteTestType()
sample.id(1)
ih = datawriter.register_instance(sample)
assert(fastdds.c_InstanceHandle_Unknown != ih)
sample2 = pytest.dds_type.KeyedCompleteTestType()
sample2.id(2)
ih2 = datawriter.register_instance(sample2)
assert(fastdds.c_InstanceHandle_Unknown != ih2)
assert(ih2 != ih)
assert(fastdds.RETCODE_OK ==
datawriter.unregister_instance(sample, ih))
assert(fastdds.RETCODE_OK ==
datawriter.unregister_instance(sample2, ih2))
assert(fastdds.RETCODE_PRECONDITION_NOT_MET ==
datawriter.unregister_instance(
sample, fastdds.c_InstanceHandle_Unknown))
# Overload 2
sample = pytest.dds_type.KeyedCompleteTestType()
sample.id(3)
now = datetime.datetime.now().time()
timestamp = fastdds.Time_t()
timestamp.seconds = now.second
ih = datawriter.register_instance_w_timestamp(sample, timestamp)
assert(fastdds.c_InstanceHandle_Unknown != ih)
assert(fastdds.RETCODE_OK ==
datawriter.unregister_instance_w_timestamp(sample, ih, timestamp))
def test_wait_for_acknowledgments(test_keyed_type, datawriter):
"""
This test checks:
- DataWriter::wait_for_acknowledgments
"""
# Overload 1
sample = pytest.dds_type.KeyedCompleteTestType()
sample.id(3)
assert(fastdds.RETCODE_OK == datawriter.write(sample))
assert(fastdds.RETCODE_OK ==
datawriter.wait_for_acknowledgments(fastdds.Duration_t(1, 0)))
# Overload 2
ih = datawriter.register_instance(sample)
assert(fastdds.c_InstanceHandle_Unknown != ih)
assert(fastdds.RETCODE_OK ==
datawriter.wait_for_acknowledgments(
sample, ih, fastdds.Duration_t(1, 0)))
def test_write(test_keyed_type, datawriter):
"""
This test checks:
- DataWriter::write
- DataWriter::write_w_timestamp
"""
# Overload 1
sample = pytest.dds_type.KeyedCompleteTestType()
assert(fastdds.RETCODE_OK == datawriter.write(sample))
# Overload 2
sample = pytest.dds_type.KeyedCompleteTestType()
params = fastdds.WriteParams()
guid = fastdds.GUID_t()
guid.guidPrefix.value = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
guid.entityId.value = (13, 14, 15, 16)
sequence_number = fastdds.SequenceNumber_t()
sequence_number.high = 0
sequence_number.low = 1
params.related_sample_identity().writer_guid(guid)
params.related_sample_identity().sequence_number(sequence_number)
assert(fastdds.RETCODE_OK == datawriter.write(sample, params))
# Overload 3
sample = pytest.dds_type.KeyedCompleteTestType()
sample.id(1)
ih = fastdds.InstanceHandle_t()
assert(fastdds.RETCODE_OK ==
datawriter.write(sample, ih))
ih = fastdds.InstanceHandle_t()
ih.value = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
assert(fastdds.RETCODE_PRECONDITION_NOT_MET ==
datawriter.write(sample, ih))
# Overload 4
sample = pytest.dds_type.KeyedCompleteTestType()
sample.id(1)
ih = fastdds.InstanceHandle_t()
now = datetime.datetime.now().time()
timestamp = fastdds.Time_t()
timestamp.seconds = now.second
assert(fastdds.RETCODE_OK ==
datawriter.write_w_timestamp(sample, ih, timestamp))
def test_listener_ownership(participant, reader_participant, topic,
reader_topic, subscriber, publisher):
def create_datawriter():
listener = DataWriterListener()
return publisher.create_datawriter(
topic, fastdds.DATAWRITER_QOS_DEFAULT, listener)
datawriter = create_datawriter()
datareader = subscriber.create_datareader(
reader_topic, fastdds.DATAREADER_QOS_DEFAULT)
time.sleep(1)
factory = fastdds.DomainParticipantFactory.get_instance()
assert(fastdds.RETCODE_OK ==
subscriber.delete_datareader(datareader))
assert(fastdds.RETCODE_OK ==
reader_participant.delete_topic(reader_topic))
assert(fastdds.RETCODE_OK ==
reader_participant.delete_subscriber(subscriber))
assert(fastdds.RETCODE_OK ==
factory.delete_participant(reader_participant))
assert(fastdds.RETCODE_OK ==
publisher.delete_datawriter(datawriter))
assert(fastdds.RETCODE_OK ==
participant.delete_topic(topic))
assert(fastdds.RETCODE_OK ==
participant.delete_publisher(publisher))
assert(fastdds.RETCODE_OK ==
factory.delete_participant(participant))