transport_stats_provider.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <gtest/gtest.h>
  5. #include <uavcan/protocol/transport_stats_provider.hpp>
  6. #include "helpers.hpp"
  7. TEST(TransportStatsProvider, Basic)
  8. {
  9. InterlinkedTestNodesWithSysClock nodes;
  10. uavcan::TransportStatsProvider tsp(nodes.a);
  11. uavcan::GlobalDataTypeRegistry::instance().reset();
  12. uavcan::DefaultDataTypeRegistrator<uavcan::protocol::GetTransportStats> _reg1;
  13. ASSERT_LE(0, tsp.start());
  14. ServiceClientWithCollector<uavcan::protocol::GetTransportStats> tsp_cln(nodes.b);
  15. /*
  16. * First request
  17. */
  18. ASSERT_LE(0, tsp_cln.call(1, uavcan::protocol::GetTransportStats::Request()));
  19. ASSERT_LE(0, nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(10)));
  20. ASSERT_TRUE(tsp_cln.collector.result.get());
  21. ASSERT_TRUE(tsp_cln.collector.result->isSuccessful());
  22. ASSERT_EQ(0, tsp_cln.collector.result->getResponse().transfer_errors);
  23. ASSERT_EQ(1, tsp_cln.collector.result->getResponse().transfers_rx);
  24. ASSERT_EQ(0, tsp_cln.collector.result->getResponse().transfers_tx);
  25. ASSERT_EQ(1, tsp_cln.collector.result->getResponse().can_iface_stats.size());
  26. ASSERT_EQ(0, tsp_cln.collector.result->getResponse().can_iface_stats[0].errors);
  27. ASSERT_EQ(1, tsp_cln.collector.result->getResponse().can_iface_stats[0].frames_rx);
  28. ASSERT_EQ(0, tsp_cln.collector.result->getResponse().can_iface_stats[0].frames_tx);
  29. /*
  30. * Second request
  31. */
  32. ASSERT_LE(0, tsp_cln.call(1, uavcan::protocol::GetTransportStats::Request()));
  33. ASSERT_LE(0, nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(10)));
  34. ASSERT_TRUE(tsp_cln.collector.result.get());
  35. ASSERT_EQ(0, tsp_cln.collector.result->getResponse().transfer_errors);
  36. ASSERT_EQ(2, tsp_cln.collector.result->getResponse().transfers_rx);
  37. ASSERT_EQ(1, tsp_cln.collector.result->getResponse().transfers_tx);
  38. ASSERT_EQ(1, tsp_cln.collector.result->getResponse().can_iface_stats.size());
  39. ASSERT_EQ(0, tsp_cln.collector.result->getResponse().can_iface_stats[0].errors);
  40. ASSERT_EQ(2, tsp_cln.collector.result->getResponse().can_iface_stats[0].frames_rx);
  41. ASSERT_EQ(6, tsp_cln.collector.result->getResponse().can_iface_stats[0].frames_tx);
  42. /*
  43. * Sending a malformed frame, it must be registered as tranfer error
  44. */
  45. uavcan::Frame frame(uavcan::protocol::GetTransportStats::DefaultDataTypeID, uavcan::TransferTypeServiceRequest,
  46. 2, 1, 1);
  47. frame.setStartOfTransfer(true);
  48. frame.setEndOfTransfer(true);
  49. uavcan::CanFrame can_frame;
  50. ASSERT_TRUE(frame.compile(can_frame));
  51. nodes.can_a.read_queue.push(can_frame);
  52. ASSERT_LE(0, nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(10)));
  53. /*
  54. * Introducing a CAN driver error
  55. */
  56. nodes.can_a.error_count = 72;
  57. /*
  58. * Third request
  59. */
  60. ASSERT_LE(0, tsp_cln.call(1, uavcan::protocol::GetTransportStats::Request()));
  61. ASSERT_LE(0, nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(10)));
  62. ASSERT_TRUE(tsp_cln.collector.result.get());
  63. EXPECT_EQ(1, tsp_cln.collector.result->getResponse().transfer_errors); // That broken frame
  64. EXPECT_EQ(3, tsp_cln.collector.result->getResponse().transfers_rx);
  65. EXPECT_EQ(2, tsp_cln.collector.result->getResponse().transfers_tx);
  66. EXPECT_EQ(1, tsp_cln.collector.result->getResponse().can_iface_stats.size());
  67. EXPECT_EQ(72, tsp_cln.collector.result->getResponse().can_iface_stats[0].errors);
  68. EXPECT_EQ(4, tsp_cln.collector.result->getResponse().can_iface_stats[0].frames_rx); // Same here
  69. EXPECT_EQ(12, tsp_cln.collector.result->getResponse().can_iface_stats[0].frames_tx);
  70. }