-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathNode_TEST.cc
More file actions
2636 lines (2037 loc) · 72.3 KB
/
Copy pathNode_TEST.cc
File metadata and controls
2636 lines (2037 loc) · 72.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
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
/*
* Copyright (C) 2014 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <google/protobuf/text_format.h>
#include "gtest/gtest.h"
#include <gz/msgs/int32.pb.h>
#include <gz/msgs/stringmsg.pb.h>
#include <gz/msgs/vector3d.pb.h>
#include <atomic>
#include <chrono>
#include <csignal>
#include <cstdlib>
#include <functional>
#include <memory>
#include <string>
#include <thread>
#include "gz/transport/MessageInfo.hh"
#include "gz/transport/Node.hh"
#include "gz/transport/NodeShared.hh"
#include "gz/transport/TransportTypes.hh"
#include <gz/utils/Environment.hh>
#include "test_utils.hh"
using namespace gz;
static std::string partition; // NOLINT(*)
static std::string g_FQNPartition; // NOLINT(*)
static std::string g_topic = "/foo"; // NOLINT(*)
static std::string g_topic_remap = "/bar"; // NOLINT(*)
static std::mutex exitMutex;
static std::mutex cbMutex;
static std::condition_variable cbCondition;
static int data = 5;
static std::atomic<bool> cbExecuted;
static std::atomic<bool> cb2Executed;
static std::atomic<bool> genericCbExecuted;
static std::atomic<bool> cbVectorExecuted;
static std::atomic<bool> srvExecuted;
static std::atomic<bool> responseExecuted;
static std::atomic<bool> wrongResponseExecuted;
static std::atomic<int> counter{0};
static bool terminatePub = false;
//////////////////////////////////////////////////
/// \brief Initialize some global variables.
void reset()
{
cbExecuted = false;
cb2Executed = false;
genericCbExecuted = false;
srvExecuted = false;
cbVectorExecuted = false;
responseExecuted = false;
wrongResponseExecuted = false;
counter = 0;
terminatePub = false;
}
//////////////////////////////////////////////////
/// \brief Function called each time a topic update is received.
void cb(const msgs::Int32 &_msg)
{
EXPECT_EQ(_msg.data(), data);
cbExecuted = true;
++counter;
std::lock_guard<std::mutex> lk(cbMutex);
cbCondition.notify_all();
}
//////////////////////////////////////////////////
/// \brief Function called each time a topic update is received.
void cb2(const msgs::Int32 &_msg)
{
EXPECT_EQ(_msg.data(), data);
cb2Executed = true;
}
//////////////////////////////////////////////////
/// \brief Function called each time a topic update is received.
/// This callback includes message information.
void cbInfo(const msgs::Int32 &_msg,
const transport::MessageInfo &_info)
{
EXPECT_EQ(_info.Topic(), g_topic);
EXPECT_EQ(_msg.data(), data);
EXPECT_EQ(g_FQNPartition, _info.Partition());
EXPECT_EQ(_msg.GetTypeName(), _info.Type());
EXPECT_TRUE(_info.IntraProcess());
cbExecuted = true;
++counter;
}
//////////////////////////////////////////////////
void rawCbInfo(const char *_msgData, const size_t _size,
const transport::MessageInfo &_info)
{
EXPECT_EQ(_info.Topic(), g_topic);
EXPECT_EQ(g_FQNPartition, _info.Partition());
EXPECT_TRUE(_info.IntraProcess());
cbExecuted = true;
msgs::Int32 msg;
EXPECT_TRUE(msg.ParseFromArray(_msgData, _size));
EXPECT_EQ(msg.data(), data);
++counter;
}
//////////////////////////////////////////////////
/// \brief A generic callback.
void genericCb(const transport::ProtoMsg &_msg,
const transport::MessageInfo &_info)
{
std::string content;
ASSERT_TRUE(google::protobuf::TextFormat::PrintToString(_msg, &content));
EXPECT_TRUE(content.find(std::to_string(data)) != std::string::npos);
genericCbExecuted = true;
EXPECT_EQ(_info.Topic().find("@"), std::string::npos);
}
//////////////////////////////////////////////////
/// \brief Provide a service call.
bool srvEcho(const msgs::Int32 &_req,
msgs::Int32 &_rep)
{
srvExecuted = true;
EXPECT_EQ(_req.data(), data);
_rep.set_data(_req.data());
return true;
}
//////////////////////////////////////////////////
/// \brief Provide a service call without input.
bool srvWithoutInput(msgs::Int32 &_rep)
{
srvExecuted = true;
_rep.set_data(data);
return true;
}
//////////////////////////////////////////////////
/// \brief Provide a service call without waiting for response.
void srvWithoutOutput(const msgs::Int32 &_req)
{
srvExecuted = true;
EXPECT_EQ(_req.data(), data);
++counter;
}
//////////////////////////////////////////////////
/// \brief Service call response callback.
void response(const msgs::Int32 &_rep, const bool _result)
{
EXPECT_EQ(_rep.data(), data);
EXPECT_TRUE(_result);
responseExecuted = true;
++counter;
}
//////////////////////////////////////////////////
/// \brief Service call response callback.
void wrongResponse(const msgs::Vector3d &/*_rep*/, bool /*_result*/)
{
wrongResponseExecuted = true;
}
//////////////////////////////////////////////////
/// \brief Callback for receiving Vector3d data.
void cbVector(const msgs::Vector3d &/*_msg*/)
{
cbVectorExecuted = true;
}
//////////////////////////////////////////////////
/// \brief A class for testing subscription, advertisement, and request passing
/// a member function as a callback.
class MyTestClass
{
/// \brief Class constructor.
public: MyTestClass()
: callbackExecuted(false),
callbackSrvExecuted(false),
responseExecuted(false)
{
}
/// \brief Create a subscriber.
public: void Subscribe()
{
// Subscribe to an illegal topic.
EXPECT_FALSE(this->node.Subscribe("Bad Topic", &MyTestClass::Cb, this));
EXPECT_TRUE(this->node.Subscribe(g_topic, &MyTestClass::Cb, this));
}
/// \brief Create a subscriber with a callback that receives message info.
public: void SubscribeWithMessageInfo()
{
// Subscribe to an illegal topic.
EXPECT_FALSE(this->node.Subscribe("Bad Topic", &MyTestClass::CbInfo, this));
EXPECT_TRUE(this->node.Subscribe(g_topic, &MyTestClass::CbInfo, this));
}
/// \brief Member function used as a callback for responding to a service
/// call.
public: bool Echo(const msgs::Int32 &_req,
msgs::Int32 &_rep)
{
EXPECT_EQ(_req.data(), data);
_rep.set_data(_req.data());
this->callbackSrvExecuted = true;
return true;
}
/// \brief Member function used as a callback for responding to a service
/// call without input.
public: bool WithoutInput(msgs::Int32 &_rep)
{
_rep.set_data(data);
this->callbackSrvExecuted = true;
return true;
}
// Member function used as a callback for responding to a service call
// without without waiting for a response.
public: void WithoutOutput(const msgs::Int32 &_req)
{
EXPECT_EQ(_req.data(), data);
this->callbackSrvExecuted = true;
}
/// \brief Response callback to a service request.
public: void EchoResponse(const msgs::Int32 &_rep,
const bool _result)
{
EXPECT_EQ(_rep.data(), data);
EXPECT_TRUE(_result);
this->responseExecuted = true;
}
/// \brief Response callback to a service request without input.
public: void WithoutInputResponse(const msgs::Int32 &_rep,
const bool _result)
{
EXPECT_EQ(_rep.data(), data);
EXPECT_TRUE(_result);
this->responseExecuted = true;
}
/// \brief Member function called each time a topic update is received.
public: void Cb(const msgs::Int32 &_msg)
{
EXPECT_EQ(_msg.data(), data);
this->callbackExecuted = true;
};
/// \brief Member function called each time a topic update is received.
/// This callback accepts a parameter with some message information.
public: void CbInfo(const msgs::Int32 &_msg,
const transport::MessageInfo &_info)
{
EXPECT_EQ(_info.Topic(), g_topic);
EXPECT_EQ(_msg.data(), data);
EXPECT_EQ(g_FQNPartition, _info.Partition());
EXPECT_EQ(_msg.GetTypeName(), _info.Type());
EXPECT_TRUE(_info.IntraProcess());
this->callbackExecuted = true;
};
/// \brief Advertise a topic and publish a message.
public: void SendSomeData()
{
msgs::Int32 msg;
msg.set_data(data);
// Advertise an illegal topic.
auto pub = this->node.Advertise<msgs::Int32>("invalid topic");
EXPECT_FALSE(pub);
EXPECT_FALSE(pub.HasConnections());
auto pubId = this->node.Advertise<msgs::Int32>("invalid topic");
EXPECT_FALSE(pubId);
EXPECT_FALSE(pubId.Valid());
pubId = this->node.Advertise<msgs::Int32>(g_topic);
EXPECT_TRUE(pubId);
EXPECT_TRUE(pubId.Valid());
EXPECT_TRUE(pubId.HasConnections());
EXPECT_TRUE(pubId.Publish(msg));
}
/// \brief Advertise a service, request a service using non-blocking and
/// blocking call.
public: void TestServiceCall()
{
msgs::Int32 req;
msgs::Int32 rep;
msgs::Vector3d wrongReq;
msgs::Vector3d wrongRep;
int timeout = 500;
bool result;
req.set_data(data);
this->Reset();
// Advertise an illegal service name.
EXPECT_FALSE(this->node.Advertise("Bad Srv", &MyTestClass::Echo, this));
// Advertise and request a valid service.
EXPECT_TRUE(this->node.Advertise(g_topic, &MyTestClass::Echo, this));
EXPECT_TRUE(this->node.Request(g_topic, req, timeout, rep, result));
ASSERT_TRUE(result);
EXPECT_EQ(rep.data(), data);
EXPECT_TRUE(this->callbackSrvExecuted);
this->Reset();
// Request a valid service using a member function callback.
this->node.Request(g_topic, req, &MyTestClass::EchoResponse, this);
EXPECT_TRUE(this->responseExecuted);
this->Reset();
// Request a valid service using RequestRaw.
std::string reqStr, repStr, repTypeName;
ASSERT_TRUE(req.SerializeToString(&reqStr));
EXPECT_TRUE(this->node.RequestRaw(g_topic, reqStr,
std::string(req.GetTypeName()),
std::string(rep.GetTypeName()), timeout, repStr, result));
ASSERT_TRUE(rep.ParseFromString(repStr));
ASSERT_TRUE(result);
EXPECT_EQ(rep.data(), data);
EXPECT_TRUE(this->callbackSrvExecuted);
this->Reset();
// Service requests with wrong types.
EXPECT_FALSE(this->node.Request(g_topic, wrongReq, timeout, rep, result));
EXPECT_FALSE(this->node.Request(g_topic, req, timeout, wrongRep, result));
EXPECT_TRUE(this->node.Request(g_topic, wrongReq, response));
EXPECT_TRUE(this->node.Request(g_topic, req, wrongResponse));
std::this_thread::sleep_for(std::chrono::milliseconds(300));
EXPECT_FALSE(this->callbackSrvExecuted);
}
/// \brief Advertise a service without input, request a service without input
/// using non-blocking and blocking call.
public: void TestServiceCallWithoutInput()
{
msgs::Int32 rep;
msgs::Vector3d wrongRep;
int timeout = 500;
bool result;
this->Reset();
// Advertise an illegal service name without input.
EXPECT_FALSE(this->node.Advertise("Bad Srv", &MyTestClass::WithoutInput,
this));
// Advertise and request a valid service without input.
EXPECT_TRUE(this->node.Advertise(g_topic, &MyTestClass::WithoutInput,
this));
EXPECT_TRUE(this->node.Request(g_topic, timeout, rep, result));
ASSERT_TRUE(result);
EXPECT_EQ(rep.data(), data);
EXPECT_TRUE(this->callbackSrvExecuted);
this->Reset();
// Request a valid service without input using a member function callback.
this->node.Request(g_topic, &MyTestClass::WithoutInputResponse, this);
EXPECT_TRUE(this->responseExecuted);
this->Reset();
// Service requests without input with wrong types.
EXPECT_FALSE(this->node.Request(g_topic, timeout, wrongRep, result));
EXPECT_TRUE(this->node.Request(g_topic, wrongResponse));
std::this_thread::sleep_for(std::chrono::milliseconds(300));
EXPECT_FALSE(this->callbackSrvExecuted);
}
/// \brief Advertise and request a service without waiting for response.
public: void TestServiceCallWithoutOutput()
{
msgs::Int32 req;
msgs::Vector3d wrongReq;
req.set_data(data);
this->Reset();
// Advertise an illegal service name without waiting for response.
EXPECT_FALSE(this->node.Advertise("Bad Srv", &MyTestClass::WithoutOutput,
this));
// Advertise and request a valid service without waiting for response.
EXPECT_TRUE(this->node.Advertise(g_topic, &MyTestClass::WithoutOutput,
this));
EXPECT_TRUE(this->node.Request(g_topic, req));
EXPECT_TRUE(this->callbackSrvExecuted);
this->Reset();
// Service requests without waiting for response with wrong types.
EXPECT_TRUE(this->node.Request(g_topic, wrongReq));
std::this_thread::sleep_for(std::chrono::milliseconds(300));
EXPECT_FALSE(this->callbackSrvExecuted);
}
/// \brief Advertise and request a service without waiting for response.
public: void TestServiceCallRequestingBeforeAdvertising()
{
msgs::Int32 req;
req.set_data(data);
this->Reset();
// Request a valid service using a member function callback.
this->node.Request(g_topic, req, &MyTestClass::EchoResponse, this);
// Advertise and request a valid service.
EXPECT_TRUE(this->node.Advertise(g_topic, &MyTestClass::Echo, this));
std::this_thread::sleep_for(std::chrono::milliseconds(300));
EXPECT_TRUE(this->responseExecuted);
}
public: void Reset()
{
this->callbackExecuted = false;
this->callbackSrvExecuted = false;
this->responseExecuted = false;
}
/// \brief Member variables that flag when the actions are executed.
public: bool callbackExecuted;
public: bool callbackSrvExecuted;
public: bool responseExecuted;
/// \brief Transport node;
private: transport::Node node;
};
//////////////////////////////////////////////////
/// \brief Create a subscriber and wait for a callback to be executed.
void CreateSubscriber(const transport::NodeOptions &_nodeOptions)
{
transport::Node node(_nodeOptions);
EXPECT_TRUE(node.Subscribe(g_topic, cb));
transport::waitUntil([&]{ return cbExecuted.load(); });
}
//////////////////////////////////////////////////
/// \brief Use two threads using their own transport nodes. One thread
/// will publish a message, whereas the other thread is subscribed to the topic.
/// \param[in] _scope Scope used to advertise the topic.
void CreatePubSubTwoThreads(
const transport::NodeOptions &_nodeOptions,
const transport::Scope_t &_sc = transport::Scope_t::ALL)
{
reset();
transport::AdvertiseMessageOptions opts;
opts.SetScope(_sc);
msgs::Int32 msg;
msg.set_data(data);
transport::Node node(_nodeOptions);
auto pub = node.Advertise<msgs::Int32>(g_topic, opts);
EXPECT_TRUE(pub);
// Subscribe to a topic in a different thread and wait until the callback is
// received.
std::thread subscribeThread(CreateSubscriber, _nodeOptions);
// Wait until the subscriber is alive.
transport::waitUntil([&]{ return pub.HasConnections(); });
// Publish a msg on topic.
EXPECT_TRUE(pub.Publish(msg));
// Wait until the subscribe thread finishes.
subscribeThread.join();
// Check that the message was received.
EXPECT_TRUE(cbExecuted);
reset();
}
//////////////////////////////////////////////////
/// \brief Test the bool operator of the Node::Publisher class
TEST(NodePubTest, BoolOperatorTest)
{
transport::Node node;
transport::Node node2;
transport::Node::Publisher pub;
const transport::Node::Publisher pub_const;
EXPECT_FALSE(pub);
EXPECT_FALSE(pub_const);
pub = node.Advertise<msgs::Vector3d>(g_topic);
const transport::Node::Publisher pub2_const =
node2.Advertise<msgs::Vector3d>(g_topic);
EXPECT_TRUE(pub);
EXPECT_TRUE(pub2_const);
}
//////////////////////////////////////////////////
/// \brief A message should not be published if it is not advertised before.
TEST(NodeTest, PubWithoutAdvertise)
{
reset();
msgs::Int32 msg;
msg.set_data(data);
// Check that an invalid namespace is ignored. The callbacks are expecting an
// empty namespace.
transport::NodeOptions optionsNode1;
transport::NodeOptions optionsNode2;
optionsNode1.SetPartition(partition);
optionsNode1.SetNameSpace("invalid namespace");
optionsNode2.SetPartition(partition);
transport::Node node1(optionsNode1);
transport::Node node2(optionsNode2);
// Check the advertised/subscribed topics and advertised services.
EXPECT_TRUE(node1.AdvertisedTopics().empty());
EXPECT_TRUE(node1.SubscribedTopics().empty());
EXPECT_TRUE(node1.AdvertisedServices().empty());
auto pub1 = node1.Advertise(g_topic, std::string(msg.GetTypeName()));
EXPECT_TRUE(pub1);
auto advertisedTopics = node1.AdvertisedTopics();
ASSERT_EQ(advertisedTopics.size(), 1u);
EXPECT_EQ(advertisedTopics.at(0), g_topic);
auto pub2 = node2.Advertise<msgs::Int32>(g_topic);
EXPECT_TRUE(pub2);
advertisedTopics = node2.AdvertisedTopics();
ASSERT_EQ(advertisedTopics.size(), 1u);
EXPECT_EQ(advertisedTopics.at(0), g_topic);
EXPECT_TRUE(node2.Subscribe(g_topic, cb));
auto subscribedTopics = node2.SubscribedTopics();
ASSERT_EQ(subscribedTopics.size(), 1u);
EXPECT_EQ(subscribedTopics.at(0), g_topic);
// Wait some time before publishing.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::unique_lock<std::mutex> lk(cbMutex);
// Publish a message by each node.
EXPECT_TRUE(pub1.Publish(msg));
// Wait for the messages to arrive.
cbCondition.wait(lk, []{return counter >= 1;});
EXPECT_TRUE(pub2.Publish(msg));
cbCondition.wait(lk, []{return counter >= 2;});
// Check that the msg was received twice.
EXPECT_TRUE(cbExecuted);
EXPECT_EQ(counter, 2);
reset();
}
//////////////////////////////////////////////////
/// \brief A thread can create a node, and send and receive messages.
TEST(NodeTest, PubSubSameThread)
{
reset();
msgs::Int32 msg;
msg.set_data(data);
transport::Node node;
// Advertise an illegal topic.
EXPECT_FALSE(node.Advertise<msgs::Int32>("invalid topic"));
auto pub = node.Advertise<msgs::Int32>(g_topic);
EXPECT_TRUE(pub);
// Subscribe to an illegal topic.
EXPECT_FALSE(node.Subscribe("invalid topic", cb));
EXPECT_TRUE(node.Subscribe(g_topic, cb));
// Wait some time before publishing.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Publish a first message.
EXPECT_TRUE(pub.Publish(msg));
// Give some time to the subscribers.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Check that the message was received.
EXPECT_TRUE(cbExecuted);
reset();
// Publish a second message on topic.
EXPECT_TRUE(pub.Publish(msg));
// Give some time to the subscribers.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Check that the data was received.
EXPECT_TRUE(cbExecuted);
reset();
}
//////////////////////////////////////////////////
/// \brief A thread can create a node, and send and receive messages.
TEST(NodeTest, PubSubSameThreadGenericCb)
{
reset();
msgs::Int32 msg;
msg.set_data(data);
transport::Node node;
auto pub = node.Advertise<msgs::Int32>(g_topic);
EXPECT_TRUE(pub);
EXPECT_TRUE(node.Subscribe(g_topic, genericCb));
// Wait some time before publishing.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Publish a first message.
EXPECT_TRUE(pub.Publish(msg));
// Give some time to the subscribers.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Check that the message was received.
EXPECT_TRUE(genericCbExecuted);
reset();
// Publish a second message on topic.
EXPECT_TRUE(pub.Publish(msg));
// Give some time to the subscribers.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Check that the data was received.
EXPECT_TRUE(genericCbExecuted);
reset();
}
//////////////////////////////////////////////////
/// \brief A thread can create a node, and send and receive messages.
/// This test uses a callback that accepts a parameter with the message
/// information.
TEST(NodeTest, PubSubSameThreadMessageInfo)
{
reset();
msgs::Int32 msg;
msg.set_data(data);
transport::Node node;
auto pub = node.Advertise<msgs::Int32>(g_topic);
EXPECT_TRUE(pub);
EXPECT_TRUE(node.Subscribe(g_topic, cbInfo));
// Wait some time before publishing.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Publish a first message.
EXPECT_TRUE(pub.Publish(msg));
// Give some time to the subscribers.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Check that the message was received.
EXPECT_TRUE(cbExecuted);
reset();
// Publish a second message on topic.
EXPECT_TRUE(pub.Publish(msg));
// Give some time to the subscribers.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Check that the data was received.
EXPECT_TRUE(cbExecuted);
reset();
}
//////////////////////////////////////////////////
TEST(NodeTest, RawPubSubSameThreadMessageInfo)
{
reset();
msgs::Int32 msg;
msg.set_data(data);
transport::Node node;
auto pub = node.Advertise<msgs::Int32>(g_topic);
EXPECT_TRUE(pub);
EXPECT_TRUE(node.Subscribe(g_topic, cbInfo));
// Wait some time before publishing.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Publish a first message.
EXPECT_TRUE(pub.PublishRaw(msg.SerializeAsString(),
std::string(msg.GetTypeName())));
// Give some time to the subscribers.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Check that the message was received.
EXPECT_TRUE(cbExecuted);
reset();
// Publish a second message on topic.
EXPECT_TRUE(pub.PublishRaw(msg.SerializeAsString(),
std::string(msg.GetTypeName())));
// Give some time to the subscribers.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Check that the data was received.
EXPECT_TRUE(cbExecuted);
reset();
}
//////////////////////////////////////////////////
TEST(NodeTest, RawPubRawSubSameThreadMessageInfo)
{
reset();
msgs::Int32 msg;
msg.set_data(data);
transport::Node node;
auto pub = node.Advertise<msgs::Int32>(g_topic);
EXPECT_TRUE(pub);
EXPECT_TRUE(node.SubscribeRaw(g_topic, rawCbInfo));
// Wait some time before publishing.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Publish a first message.
EXPECT_TRUE(pub.PublishRaw(msg.SerializeAsString(),
std::string(msg.GetTypeName())));
// Give some time to the subscribers.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Check that the message was received.
EXPECT_TRUE(cbExecuted);
reset();
// Publish a second message on topic.
EXPECT_TRUE(pub.PublishRaw(msg.SerializeAsString(),
std::string(msg.GetTypeName())));
// Give some time to the subscribers.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Check that the data was received.
EXPECT_TRUE(cbExecuted);
reset();
}
//////////////////////////////////////////////////
TEST(NodeTest, PubRawSubSameThreadMessageInfo)
{
reset();
msgs::Int32 msg;
msg.set_data(data);
transport::Node node;
auto pub = node.Advertise<msgs::Int32>(g_topic);
EXPECT_TRUE(pub);
EXPECT_TRUE(node.SubscribeRaw(g_topic, rawCbInfo));
// Wait some time before publishing.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Publish a first message.
EXPECT_TRUE(pub.Publish(msg));
// Give some time to the subscribers.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Check that the message was received.
EXPECT_TRUE(cbExecuted);
reset();
// Publish a second message on topic.
EXPECT_TRUE(pub.Publish(msg));
// Give some time to the subscribers.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Check that the data was received.
EXPECT_TRUE(cbExecuted);
reset();
}
//////////////////////////////////////////////////
/// \brief Subscribe to a topic using a lambda function.
TEST(NodeTest, PubSubSameThreadLambda)
{
reset();
msgs::Int32 msg;
msg.set_data(data);
transport::Node node;
auto pub = node.Advertise<msgs::Int32>(g_topic);
EXPECT_TRUE(pub);
std::mutex mutex;
std::condition_variable condition;
bool executed = false;
std::function<void(const msgs::Int32&)> subCb =
[&executed, &mutex, &condition](const msgs::Int32 &_msg)
{
EXPECT_EQ(_msg.data(), data);
std::lock_guard<std::mutex> lk(mutex);
executed = true;
condition.notify_all();
};
EXPECT_TRUE(node.Subscribe(g_topic, subCb));
// Give some time to the subscribers.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Publish a first message.
EXPECT_TRUE(pub.Publish(msg));
// The local publish is asynchronous, which means we need to wait
// for the callback.
std::unique_lock<std::mutex> lk(mutex);
condition.wait(lk, [&executed]{return executed;});
EXPECT_TRUE(executed);
reset();
}
//////////////////////////////////////////////////
/// \brief Subscribe to a topic using a lambda function.
/// This test uses a callback that accepts a parameter with the message
/// information.
TEST(NodeTest, PubSubSameThreadLambdaMessageInfo)
{
reset();
msgs::Int32 msg;
msg.set_data(data);
transport::Node node;
auto pub = node.Advertise<msgs::Int32>(g_topic);
EXPECT_TRUE(pub);
bool executed = false;
std::function<void(const msgs::Int32&,
const transport::MessageInfo &)> subCb =
[&executed](const msgs::Int32 &_msg,
const transport::MessageInfo &_info)
{
EXPECT_EQ(_info.Topic(), g_topic);
EXPECT_EQ(_msg.data(), data);
EXPECT_EQ(g_FQNPartition, _info.Partition());
EXPECT_EQ(_msg.GetTypeName(), _info.Type());
EXPECT_TRUE(_info.IntraProcess());
std::lock_guard<std::mutex> lk(cbMutex);
executed = true;
cbCondition.notify_all();
};
EXPECT_TRUE(node.Subscribe(g_topic, subCb));
// Give some time to the subscribers.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Publish a first message.
std::unique_lock<std::mutex> lk(cbMutex);
EXPECT_TRUE(pub.Publish(msg));
cbCondition.wait(lk, [&executed]{return executed;});
EXPECT_TRUE(executed);
reset();
}
//////////////////////////////////////////////////
/// \brief Test the bool operator of the Node::Subscriber class
TEST(NodeSubTest, BoolOperatorTest)
{
transport::Node node;
transport::Node::Subscriber sub;
const transport::Node::Subscriber sub_const;
EXPECT_FALSE(sub);
EXPECT_FALSE(sub_const);
std::function<void(const msgs::Int32 &)> cb =
[](const msgs::Int32 &) {};
sub = node.CreateSubscriber(g_topic, cb);
EXPECT_TRUE(sub);
EXPECT_TRUE(sub.Unsubscribe());
EXPECT_FALSE(sub);
const transport::Node::Subscriber sub2_const =
node.CreateSubscriber(g_topic, cb);
EXPECT_TRUE(sub2_const);
}
//////////////////////////////////////////////////
/// \brief Exercise the Subscriber move constructor.
TEST(NodeTest, MoveSubscriber)
{
transport::Node node;
std::vector<transport::Node::Subscriber> subscribers;
subscribers.emplace_back(node.CreateSubscriber(g_topic, cb));
}
//////////////////////////////////////////////////
/// \brief Subscribe to a topic using CreateSubscriber API
TEST(NodeTest, PubSubWithCreateSubscriber)
{
reset();
msgs::Int32 msg;
msg.set_data(data);
transport::Node node;
auto pub = node.Advertise<msgs::Int32>(g_topic);
EXPECT_TRUE(pub);
std::mutex mutex;
std::condition_variable condition;