Руслан Урядинский / libuavcan

Dependents:   UAVCAN UAVCAN_Subscriber

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers transport_stats_provider.cpp Source File

transport_stats_provider.cpp

00001 /*
00002  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
00003  */
00004 
00005 #include <gtest/gtest.h>
00006 #include <uavcan/protocol/transport_stats_provider.hpp>
00007 #include "helpers.hpp"
00008 
00009 
00010 TEST(TransportStatsProvider, Basic)
00011 {
00012     InterlinkedTestNodesWithSysClock nodes;
00013 
00014     uavcan::TransportStatsProvider tsp(nodes.a);
00015 
00016     uavcan::GlobalDataTypeRegistry::instance().reset();
00017     uavcan::DefaultDataTypeRegistrator<uavcan::protocol::GetTransportStats> _reg1;
00018 
00019     ASSERT_LE(0, tsp.start());
00020 
00021     ServiceClientWithCollector<uavcan::protocol::GetTransportStats> tsp_cln(nodes.b);
00022 
00023     /*
00024      * First request
00025      */
00026     ASSERT_LE(0, tsp_cln.call(1, uavcan::protocol::GetTransportStats::Request()));
00027     ASSERT_LE(0, nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(10)));
00028 
00029     ASSERT_TRUE(tsp_cln.collector.result.get());
00030     ASSERT_TRUE(tsp_cln.collector.result->isSuccessful());
00031     ASSERT_EQ(0, tsp_cln.collector.result->getResponse().transfer_errors);
00032     ASSERT_EQ(1, tsp_cln.collector.result->getResponse().transfers_rx);
00033     ASSERT_EQ(0, tsp_cln.collector.result->getResponse().transfers_tx);
00034     ASSERT_EQ(1, tsp_cln.collector.result->getResponse().can_iface_stats.size());
00035     ASSERT_EQ(0, tsp_cln.collector.result->getResponse().can_iface_stats[0].errors);
00036     ASSERT_EQ(1, tsp_cln.collector.result->getResponse().can_iface_stats[0].frames_rx);
00037     ASSERT_EQ(0, tsp_cln.collector.result->getResponse().can_iface_stats[0].frames_tx);
00038 
00039     /*
00040      * Second request
00041      */
00042     ASSERT_LE(0, tsp_cln.call(1, uavcan::protocol::GetTransportStats::Request()));
00043     ASSERT_LE(0, nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(10)));
00044 
00045     ASSERT_TRUE(tsp_cln.collector.result.get());
00046     ASSERT_EQ(0, tsp_cln.collector.result->getResponse().transfer_errors);
00047     ASSERT_EQ(2, tsp_cln.collector.result->getResponse().transfers_rx);
00048     ASSERT_EQ(1, tsp_cln.collector.result->getResponse().transfers_tx);
00049     ASSERT_EQ(1, tsp_cln.collector.result->getResponse().can_iface_stats.size());
00050     ASSERT_EQ(0, tsp_cln.collector.result->getResponse().can_iface_stats[0].errors);
00051     ASSERT_EQ(2, tsp_cln.collector.result->getResponse().can_iface_stats[0].frames_rx);
00052     ASSERT_EQ(6, tsp_cln.collector.result->getResponse().can_iface_stats[0].frames_tx);
00053 
00054     /*
00055      * Sending a malformed frame, it must be registered as tranfer error
00056      */
00057     uavcan::Frame frame(uavcan::protocol::GetTransportStats::DefaultDataTypeID, uavcan::TransferTypeServiceRequest,
00058                         2, 1, 1);
00059     frame.setStartOfTransfer(true);
00060     frame.setEndOfTransfer(true);
00061     uavcan::CanFrame can_frame;
00062     ASSERT_TRUE(frame.compile(can_frame));
00063     nodes.can_a.read_queue.push(can_frame);
00064 
00065     ASSERT_LE(0, nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(10)));
00066 
00067     /*
00068      * Introducing a CAN driver error
00069      */
00070     nodes.can_a.error_count = 72;
00071 
00072     /*
00073      * Third request
00074      */
00075     ASSERT_LE(0, tsp_cln.call(1, uavcan::protocol::GetTransportStats::Request()));
00076     ASSERT_LE(0, nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(10)));
00077 
00078     ASSERT_TRUE(tsp_cln.collector.result.get());
00079     EXPECT_EQ(1, tsp_cln.collector.result->getResponse().transfer_errors);                  // That broken frame
00080     EXPECT_EQ(3, tsp_cln.collector.result->getResponse().transfers_rx);
00081     EXPECT_EQ(2, tsp_cln.collector.result->getResponse().transfers_tx);
00082     EXPECT_EQ(1, tsp_cln.collector.result->getResponse().can_iface_stats.size());
00083     EXPECT_EQ(72, tsp_cln.collector.result->getResponse().can_iface_stats[0].errors);
00084     EXPECT_EQ(4, tsp_cln.collector.result->getResponse().can_iface_stats[0].frames_rx);     // Same here
00085     EXPECT_EQ(12, tsp_cln.collector.result->getResponse().can_iface_stats[0].frames_tx);
00086 }