Main function for running the tests.
344{
345
347 []()
348 {
349
351
352
353 bool bInitSuccess = false;
354
355
356 for (int nI = 0; nI < 3; ++nI)
357 {
358 if (RoveCommUDPNode.
Init(11004))
359 {
360 bInitSuccess = true;
361 break;
362 }
363 else
364 {
365 std::this_thread::sleep_for(std::chrono::seconds(1));
366 }
367 }
368
369 ASSERT_TRUE(bInitSuccess) << "Failed to initialize the UDP socket";
370
371
372 struct sockaddr_in saUDPClientAddr;
373 memset(&saUDPClientAddr, 0, sizeof(saUDPClientAddr));
374 saUDPClientAddr.sin_family = AF_INET;
375 saUDPClientAddr.sin_port = htons(11004);
376 inet_pton(AF_INET, "127.0.0.1", &saUDPClientAddr.sin_addr);
377
378
379 const uint16_t unTestDataId = 1300;
380
381
382 bool bUint8CallbackInvoked = false;
383 bool bUint16CallbackInvoked = false;
384 bool bUint32CallbackInvoked = false;
385 bool bInt8CallbackInvoked = false;
386 bool bInt16CallbackInvoked = false;
387 bool bInt32CallbackInvoked = false;
388 bool bFloatCallbackInvoked = false;
389 bool bDoubleCallbackInvoked = false;
390 bool bCharCallbackInvoked = false;
391
392
393 {
394
395 std::vector<uint8_t> vExpectedData = {1, 2, 3, 255};
396
397
398 RoveCommUDPNode.
On<uint8_t>(unTestDataId,
400 {
401 bUint8CallbackInvoked = true;
402 EXPECT_EQ(stPacket.unDataId, unTestDataId);
403 EXPECT_EQ(stPacket.GetDataCount(), vExpectedData.size());
405
406 for (size_t siI = 0; siI < stPacket.vData.size(); siI++)
407 {
408 EXPECT_EQ(stPacket.vData[siI], vExpectedData[siI]);
409 }
410 });
411
412
414
415
417
418
419 RoveCommUDPNode.
ProcessPacket<uint8_t>(vData, saUDPClientAddr);
420 }
421
422
423 {
424
425 std::vector<uint16_t> vExpectedData = {1000, 2000, 3000, 65535};
426
427
428 RoveCommUDPNode.
On<uint16_t>(unTestDataId,
430 {
431 bUint16CallbackInvoked = true;
432 EXPECT_EQ(stPacket.unDataId, unTestDataId);
433 EXPECT_EQ(stPacket.GetDataCount(), vExpectedData.size());
435
436 for (size_t siI = 0; siI < stPacket.vData.size(); siI++)
437 {
438 EXPECT_EQ(stPacket.vData[siI], vExpectedData[siI]);
439 }
440 });
441
442
444
445
447
448
449 RoveCommUDPNode.
ProcessPacket<uint16_t>(vData, saUDPClientAddr);
450 }
451
452
453 {
454
455 std::vector<int32_t> vExpectedData = {-100000, -1, 0, 1, 100000};
456
457
458 RoveCommUDPNode.
On<int32_t>(unTestDataId,
460 {
461 bInt32CallbackInvoked = true;
462 EXPECT_EQ(stPacket.unDataId, unTestDataId);
463 EXPECT_EQ(stPacket.GetDataCount(), vExpectedData.size());
465
466 for (size_t siI = 0; siI < stPacket.vData.size(); siI++)
467 {
468 EXPECT_EQ(stPacket.vData[siI], vExpectedData[siI]);
469 }
470 });
471
472
474
475
477
478
479 RoveCommUDPNode.
ProcessPacket<int32_t>(vData, saUDPClientAddr);
480 }
481
482
483 {
484
485 std::vector<float> vExpectedData = {-3.14f, 0.0f, 2.718f, 1000.5f};
486
487
488 RoveCommUDPNode.
On<
float>(unTestDataId,
490 {
491 bFloatCallbackInvoked = true;
492 EXPECT_EQ(stPacket.unDataId, unTestDataId);
493 EXPECT_EQ(stPacket.GetDataCount(), vExpectedData.size());
495
496 for (size_t siI = 0; siI < stPacket.vData.size(); siI++)
497 {
498 EXPECT_FLOAT_EQ(stPacket.vData[siI], vExpectedData[siI]);
499 }
500 });
501
502
504
505
507
508
509 RoveCommUDPNode.
ProcessPacket<
float>(vData, saUDPClientAddr);
510 }
511
512
513 {
514
515 std::vector<double> vExpectedData = {-3.14, 0.0, 2.718, 1000.5};
516
517
518 RoveCommUDPNode.
On<
double>(unTestDataId,
520 {
521 bDoubleCallbackInvoked = true;
522 EXPECT_EQ(stPacket.unDataId, unTestDataId);
523 EXPECT_EQ(stPacket.GetDataCount(), vExpectedData.size());
525
526 for (size_t siI = 0; siI < stPacket.vData.size(); siI++)
527 {
528 EXPECT_DOUBLE_EQ(stPacket.vData[siI], vExpectedData[siI]);
529 }
530 });
531
532
534
535
537
538
539 RoveCommUDPNode.
ProcessPacket<
double>(vData, saUDPClientAddr);
540 }
541
542
543 {
544
545 std::string szMessage = "Hello";
546 std::vector<char> vExpectedData(szMessage.begin(), szMessage.end());
547
548
549 RoveCommUDPNode.
On<
char>(unTestDataId,
551 {
552 bCharCallbackInvoked = true;
553 EXPECT_EQ(stPacket.unDataId, unTestDataId);
554 EXPECT_EQ(stPacket.GetDataCount(), vExpectedData.size());
556
557 for (size_t siI = 0; siI < stPacket.vData.size(); siI++)
558 {
559 EXPECT_EQ(stPacket.vData[siI], vExpectedData[siI]);
560 }
561 });
562
563
565
566
568
569
571 }
572
573
574 EXPECT_TRUE(bUint8CallbackInvoked) << "uint8_t callback was not invoked";
575 EXPECT_TRUE(bUint16CallbackInvoked) << "uint16_t callback was not invoked";
576 EXPECT_TRUE(bInt32CallbackInvoked) << "int32_t callback was not invoked";
577 EXPECT_TRUE(bFloatCallbackInvoked) << "float callback was not invoked";
578 EXPECT_TRUE(bCharCallbackInvoked) << "char callback was not invoked";
579
580
581 RoveCommUDPNode.
Close();
582 },
583 3,
584 30000);
585}
The RoveCommUDP class is used to send and receive data over a UDP connection.
Definition RoveCommUDP.h:52
void ProcessPacket(std::span< const uint8_t > stData, const sockaddr_in &saClientAddr)
Process a UDP packet and invoke the appropriate callback function. This function is called from the R...
Definition RoveCommUDP.cpp:433
void Close()
Close the UDP socket. This method is called when the RoveCommUDP object is destroyed....
Definition RoveCommUDP.cpp:696
bool Init(int nPort=manifest::General::ETHERNET_UDP_PORT)
Initialize the UDP socket and bind it to the specified port. This method also starts the thread that ...
Definition RoveCommUDP.cpp:110
CallbackHandle On(const uint16_t unDataId, std::function< void(const RoveCommPacket< T > &)> fnCallback)
Register a callback for UDP packets with the given data ID. The callback belongs to this node: packet...
Definition RoveCommUDP.cpp:333
@ INT32_T
Definition RoveCommManifest.h:39
@ UINT8_T
Definition RoveCommManifest.h:36
@ FLOAT_T
Definition RoveCommManifest.h:41
@ UINT16_T
Definition RoveCommManifest.h:38
@ DOUBLE_T
Definition RoveCommManifest.h:42
@ CHAR
Definition RoveCommManifest.h:43
std::vector< uint8_t > PackPacket(const RoveCommPacket< T > &stPacket)
Create a RoveCommData structure from a RoveCommPacket structure.
Definition RoveCommPacket.cpp:44
void RunTimedTest(const std::function< void()> &fnTestCode, int nMaxRetries, int nTimeoutMS)
Run a test with a timeout and retries.
Definition TestUtils.cc:31
The RoveCommPacket struct is used to store data in a packet format for transmission over a network....
Definition RoveCommPacket.h:75