outgoing_transfer_registry.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <algorithm>
  5. #include <gtest/gtest.h>
  6. #include <uavcan/transport/outgoing_transfer_registry.hpp>
  7. #include "../clock.hpp"
  8. #include "transfer_test_helpers.hpp"
  9. TEST(OutgoingTransferRegistry, Basic)
  10. {
  11. using uavcan::OutgoingTransferRegistryKey;
  12. uavcan::PoolAllocator<uavcan::MemPoolBlockSize * 2, uavcan::MemPoolBlockSize> poolmgr;
  13. uavcan::OutgoingTransferRegistry otr(poolmgr);
  14. otr.cleanup(tsMono(1000));
  15. static const int NUM_KEYS = 5;
  16. const OutgoingTransferRegistryKey keys[NUM_KEYS] =
  17. {
  18. OutgoingTransferRegistryKey(123, uavcan::TransferTypeServiceRequest, 42),
  19. OutgoingTransferRegistryKey(321, uavcan::TransferTypeMessageBroadcast, 0),
  20. OutgoingTransferRegistryKey(213, uavcan::TransferTypeServiceRequest, 2),
  21. OutgoingTransferRegistryKey(312, uavcan::TransferTypeServiceRequest, 4),
  22. OutgoingTransferRegistryKey(456, uavcan::TransferTypeServiceRequest, 2)
  23. };
  24. ASSERT_EQ(0, otr.accessOrCreate(keys[0], tsMono(1000000))->get());
  25. ASSERT_EQ(0, otr.accessOrCreate(keys[1], tsMono(1000000))->get());
  26. ASSERT_EQ(0, otr.accessOrCreate(keys[2], tsMono(1000000))->get());
  27. ASSERT_EQ(0, otr.accessOrCreate(keys[3], tsMono(1000000))->get());
  28. ASSERT_FALSE(otr.accessOrCreate(keys[4], tsMono(1000000))); // OOM
  29. /*
  30. * Incrementing a little
  31. */
  32. otr.accessOrCreate(keys[0], tsMono(2000000))->increment();
  33. otr.accessOrCreate(keys[0], tsMono(4000000))->increment();
  34. otr.accessOrCreate(keys[0], tsMono(3000000))->increment();
  35. ASSERT_EQ(3, otr.accessOrCreate(keys[0], tsMono(5000000))->get());
  36. otr.accessOrCreate(keys[2], tsMono(2000000))->increment();
  37. otr.accessOrCreate(keys[2], tsMono(3000000))->increment();
  38. ASSERT_EQ(2, otr.accessOrCreate(keys[2], tsMono(6000000))->get());
  39. otr.accessOrCreate(keys[3], tsMono(9000000))->increment();
  40. ASSERT_EQ(1, otr.accessOrCreate(keys[3], tsMono(4000000))->get());
  41. ASSERT_EQ(0, otr.accessOrCreate(keys[1], tsMono(4000000))->get());
  42. ASSERT_FALSE(otr.accessOrCreate(keys[4], tsMono(1000000))); // Still OOM
  43. /*
  44. * Checking existence
  45. * Exist: 0, 1, 2, 3
  46. * Does not exist: 4
  47. */
  48. ASSERT_TRUE(otr.exists(keys[1].getDataTypeID(), keys[1].getTransferType()));
  49. ASSERT_TRUE(otr.exists(keys[0].getDataTypeID(), keys[0].getTransferType()));
  50. ASSERT_TRUE(otr.exists(keys[3].getDataTypeID(), keys[3].getTransferType()));
  51. ASSERT_TRUE(otr.exists(keys[2].getDataTypeID(), keys[2].getTransferType()));
  52. ASSERT_FALSE(otr.exists(keys[1].getDataTypeID(), keys[2].getTransferType())); // Invalid combination
  53. ASSERT_FALSE(otr.exists(keys[0].getDataTypeID(), keys[1].getTransferType())); // Invalid combination
  54. ASSERT_FALSE(otr.exists(keys[4].getDataTypeID(), keys[4].getTransferType())); // Plain missing
  55. /*
  56. * Cleaning up
  57. */
  58. otr.cleanup(tsMono(4000001)); // Kills 1, 3
  59. ASSERT_EQ(0, otr.accessOrCreate(keys[1], tsMono(1000000))->get());
  60. ASSERT_EQ(0, otr.accessOrCreate(keys[3], tsMono(1000000))->get());
  61. otr.accessOrCreate(keys[1], tsMono(5000000))->increment();
  62. otr.accessOrCreate(keys[3], tsMono(5000000))->increment();
  63. ASSERT_EQ(3, otr.accessOrCreate(keys[0], tsMono(5000000))->get());
  64. ASSERT_EQ(2, otr.accessOrCreate(keys[2], tsMono(6000000))->get());
  65. otr.cleanup(tsMono(5000001)); // Kills 1, 3 (He needs a bath, Jud. He stinks of the ground you buried him in.), 0
  66. ASSERT_EQ(0, otr.accessOrCreate(keys[0], tsMono(1000000))->get());
  67. ASSERT_EQ(0, otr.accessOrCreate(keys[1], tsMono(1000000))->get());
  68. ASSERT_EQ(0, otr.accessOrCreate(keys[3], tsMono(1000000))->get());
  69. ASSERT_EQ(2, otr.accessOrCreate(keys[2], tsMono(1000000))->get());
  70. otr.cleanup(tsMono(5000001)); // Frees some memory for 4
  71. ASSERT_EQ(0, otr.accessOrCreate(keys[0], tsMono(1000000))->get());
  72. }