A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
trickle-timer-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Universita' di Firenze, Italy
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
7 */
8
9#include "ns3/test.h"
10#include "ns3/trickle-timer.h"
11
12#include <algorithm>
13#include <numeric>
14#include <vector>
15
16/**
17 * @file
18 * @ingroup core-tests
19 * @ingroup timer
20 * @ingroup timer-tests
21 *
22 * Trickle Timer test suite.
23 *
24 * This test checks that the timer, in steady-state mode (i.e., after
25 * the transient period) have a frequency between [I/2, I + I/2].
26 *
27 * The test also checks that the redundancy works, i.e., if the timer
28 * receives enough "consistent" events it will suppress its output.
29 */
30
31namespace ns3
32{
33
34namespace tests
35{
36
37/**
38 * @ingroup timer-tests
39 * TrickleTimer test
40 */
42{
43 public:
44 /** Constructor. */
46 void DoRun() override;
47 /**
48 * Function to invoke when TrickleTimer expires.
49 */
50 void ExpireTimer();
51 std::vector<Time> m_expiredTimes; //!< Time when TrickleTimer expired
52
53 /**
54 * Function to signal that the transient is over
55 */
56 void TransientOver();
57
58 /**
59 * Test the steady-state
60 * @param unit Minimum interval
61 */
62 void TestSteadyState(Time unit);
63
64 /**
65 * Test the redundancy suppression
66 * @param unit Minimum interval
67 */
68 void TestRedundancy(Time unit);
69
70 /**
71 * Inject in the timer a consistent event
72 * @param interval Interval
73 * @param tricklePtr Pointer to the TrickleTimer
74 */
76
77 bool m_enableDataCollection; //!< Collect data if true
78};
79
81 : TestCase("Check the Trickle Timer algorithm")
82{
83}
84
85void
87{
89 {
90 return;
91 }
92
93 m_expiredTimes.push_back(Simulator::Now());
94}
95
96void
101
102void
104{
105 m_expiredTimes.clear();
107
108 TrickleTimer trickle(unit, 4, 1);
109 trickle.SetFunction(&TrickleTimerTestCase::ExpireTimer, this);
110 trickle.Enable();
111 // We reset the timer to force the interval to the minimum
112 trickle.Reset();
113
114 NS_TEST_EXPECT_MSG_EQ(trickle.GetDoublings(),
115 4,
116 "The doublings re-compute mechanism is not working.");
117
118 // The transient is over at (exp2(doublings +1) -1) * MinInterval (worst case).
120
121 Simulator::Stop(unit * 50000);
122
125
126 std::vector<Time> expirationFrequency;
127
128 expirationFrequency.resize(m_expiredTimes.size());
129 std::adjacent_difference(m_expiredTimes.begin(),
130 m_expiredTimes.end(),
131 expirationFrequency.begin());
133
134 NS_TEST_ASSERT_MSG_EQ(expirationFrequency.empty(), false, "No expiration frequency");
135
136 int64x64_t min =
137 (*std::min_element(expirationFrequency.begin(), expirationFrequency.end())) / unit;
138 int64x64_t max =
139 (*std::max_element(expirationFrequency.begin(), expirationFrequency.end())) / unit;
140
141 NS_TEST_EXPECT_MSG_GT_OR_EQ(min.GetDouble(), 8, "Timer did fire too fast ??");
142 NS_TEST_EXPECT_MSG_LT_OR_EQ(max.GetDouble(), 24, "Timer did fire too slow ??");
143}
144
145void
147{
148 m_expiredTimes.clear();
150
151 TrickleTimer trickle(unit, 4, 1);
152 trickle.SetFunction(&TrickleTimerTestCase::ExpireTimer, this);
153 trickle.Enable();
154 // We reset the timer to force the interval to the minimum
155 trickle.Reset();
156
157 NS_TEST_EXPECT_MSG_EQ(trickle.GetDoublings(),
158 4,
159 "The doublings re-compute mechanism is not working.");
160
161 // The transient is over at (exp2(doublings +1) -1) * MinInterval (worst case).
163 Simulator::Schedule(unit * 31,
165 this,
166 unit * 8,
167 &trickle);
168
169 Simulator::Stop(unit * 50000);
170
173
174 NS_TEST_EXPECT_MSG_EQ(m_expiredTimes.size(), 0, "Timer did fire while being suppressed ??");
175}
176
177void
179{
180 tricklePtr->ConsistentEvent();
181 Simulator::Schedule(interval,
183 this,
184 interval,
185 tricklePtr);
186}
187
188void
195
196/**
197 * @ingroup timer-tests
198 * Trickle Timer test suite
199 */
201{
202 public:
203 /** Constructor. */
205 : TestSuite("trickle-timer")
206 {
208 }
209};
210
211/**
212 * @ingroup timer-tests
213 * TrickleTimerTestSuite instance variable.
214 */
216
217} // namespace tests
218
219} // namespace ns3
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
A Trickle Timer following RFC 6206.
High precision numerical type, implementing Q64.64 fixed precision.
double GetDouble() const
Get this value as a double.
void TestRedundancy(Time unit)
Test the redundancy suppression.
bool m_enableDataCollection
Collect data if true.
void TestSteadyState(Time unit)
Test the steady-state.
void TransientOver()
Function to signal that the transient is over.
void ExpireTimer()
Function to invoke when TrickleTimer expires.
std::vector< Time > m_expiredTimes
Time when TrickleTimer expired.
void DoRun() override
Implementation to actually run this TestCase.
void ConsistentEvent(Time interval, TrickleTimer *tricklePtr)
Inject in the timer a consistent event.
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#define NS_TEST_EXPECT_MSG_GT_OR_EQ(actual, limit, msg)
Test that an actual value is greater than or equal to limit and report if not.
Definition test.h:986
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition test.h:134
#define NS_TEST_EXPECT_MSG_LT_OR_EQ(actual, limit, msg)
Test that an actual value is less than or equal to a limit and report if not.
Definition test.h:820
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition test.h:241
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
static TrickleTimerTestSuite g_trickleTimerTestSuite
TrickleTimerTestSuite instance variable.
Every class exported by the ns3 library is enclosed in the ns3 namespace.