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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
18 */
19
20#include "ns3/test.h"
21#include "ns3/trickle-timer.h"
22
23#include <algorithm>
24#include <numeric>
25#include <vector>
26
27/**
28 * \file
29 * \ingroup core-tests
30 * \ingroup timer
31 * \ingroup timer-tests
32 *
33 * Trickle Timer test suite.
34 *
35 * This test checks that the timer, in steady-state mode (i.e., after
36 * the transient period) have a frequency between [I/2, I + I/2].
37 *
38 * The test also checks that the redundancy works, i.e., if the timer
39 * receives enough "consistent" events it will suppress its output.
40 */
41
42namespace ns3
43{
44
45namespace tests
46{
47
48/**
49 * \ingroup timer-tests
50 * TrickleTimer test
51 */
53{
54 public:
55 /** Constructor. */
57 void DoRun() override;
58 /**
59 * Function to invoke when TrickleTimer expires.
60 */
61 void ExpireTimer();
62 std::vector<Time> m_expiredTimes; //!< Time when TrickleTimer expired
63
64 /**
65 * Function to signal that the transient is over
66 */
67 void TransientOver();
68
69 /**
70 * Test the steady-state
71 * \param unit Minimum interval
72 */
73 void TestSteadyState(Time unit);
74
75 /**
76 * Test the redundancy suppression
77 * \param unit Minimum interval
78 */
79 void TestRedundancy(Time unit);
80
81 /**
82 * Inject in the timer a consistent event
83 * \param interval Interval
84 * \param tricklePtr Pointer to the TrickleTimer
85 */
86 void ConsistentEvent(Time interval, TrickleTimer* tricklePtr);
87
88 bool m_enableDataCollection; //!< Collect data if true
89};
90
92 : TestCase("Check the Trickle Timer algorithm")
93{
94}
95
96void
98{
100 {
101 return;
102 }
103
104 m_expiredTimes.push_back(Simulator::Now());
105}
106
107void
109{
111}
112
113void
115{
116 m_expiredTimes.clear();
118
119 TrickleTimer trickle(unit, 4, 1);
121 trickle.Enable();
122 // We reset the timer to force the interval to the minimum
123 trickle.Reset();
124
126 4,
127 "The doublings re-compute mechanism is not working.");
128
129 // The transient is over at (exp2(doublings +1) -1) * MinInterval (worst case).
131
132 Simulator::Stop(unit * 50000);
133
136
137 std::vector<Time> expirationFrequency;
138
139 expirationFrequency.resize(m_expiredTimes.size());
140 std::adjacent_difference(m_expiredTimes.begin(),
141 m_expiredTimes.end(),
142 expirationFrequency.begin());
143 expirationFrequency.erase(expirationFrequency.begin());
144
145 int64x64_t min =
146 (*std::min_element(expirationFrequency.begin(), expirationFrequency.end())) / unit;
147 int64x64_t max =
148 (*std::max_element(expirationFrequency.begin(), expirationFrequency.end())) / unit;
149
150 NS_TEST_EXPECT_MSG_GT_OR_EQ(min.GetDouble(), 8, "Timer did fire too fast ??");
151 NS_TEST_EXPECT_MSG_LT_OR_EQ(max.GetDouble(), 24, "Timer did fire too slow ??");
152}
153
154void
156{
157 m_expiredTimes.clear();
159
160 TrickleTimer trickle(unit, 4, 1);
162 trickle.Enable();
163 // We reset the timer to force the interval to the minimum
164 trickle.Reset();
165
167 4,
168 "The doublings re-compute mechanism is not working.");
169
170 // The transient is over at (exp2(doublings +1) -1) * MinInterval (worst case).
172 Simulator::Schedule(unit * 31,
174 this,
175 unit * 8,
176 &trickle);
177
178 Simulator::Stop(unit * 50000);
179
182
183 NS_TEST_EXPECT_MSG_EQ(m_expiredTimes.size(), 0, "Timer did fire while being suppressed ??");
184}
185
186void
188{
189 tricklePtr->ConsistentEvent();
190 Simulator::Schedule(interval,
192 this,
193 interval,
194 tricklePtr);
195}
196
197void
199{
203}
204
205/**
206 * \ingroup timer-tests
207 * Trickle Timer test suite
208 */
210{
211 public:
212 /** Constructor. */
214 : TestSuite("trickle-timer")
215 {
217 }
218};
219
220/**
221 * \ingroup timer-tests
222 * TrickleTimerTestSuite instance variable.
223 */
225
226} // namespace tests
227
228} // namespace ns3
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
A Trickle Timer following RFC 6206.
Definition: trickle-timer.h:79
uint8_t GetDoublings() const
Get the doublings of the timer.
void Reset()
Reset the timer.
void SetFunction(FN fn)
Set the function to execute when the timer expires.
void Enable()
Enable the timer.
void ConsistentEvent()
Records a consistent event.
High precision numerical type, implementing Q64.64 fixed precision.
Definition: int64x64-128.h:56
double GetDouble() const
Get this value as a double.
Definition: int64x64-128.h:240
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.
#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:997
#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:831
#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:252
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
static TrickleTimerTestSuite g_trickleTimerTestSuite
TrickleTimerTestSuite instance variable.
Every class exported by the ns3 library is enclosed in the ns3 namespace.