A Discrete-Event Network Simulator
API
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 
42 namespace ns3
43 {
44 
45 namespace tests
46 {
47 
53 {
54  public:
57  void DoRun() override;
61  void ExpireTimer();
62  std::vector<Time> m_expiredTimes;
63 
67  void TransientOver();
68 
73  void TestSteadyState(Time unit);
74 
79  void TestRedundancy(Time unit);
80 
86  void ConsistentEvent(Time interval, TrickleTimer* tricklePtr);
87 
89 };
90 
92  : TestCase("Check the Trickle Timer algorithm")
93 {
94 }
95 
96 void
98 {
100  {
101  return;
102  }
103 
104  m_expiredTimes.push_back(Simulator::Now());
105 }
106 
107 void
109 {
110  m_enableDataCollection = true;
111 }
112 
113 void
115 {
116  m_expiredTimes.clear();
117  m_enableDataCollection = false;
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 
134  Simulator::Run();
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 
154 void
156 {
157  m_expiredTimes.clear();
158  m_enableDataCollection = false;
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 
180  Simulator::Run();
182 
183  NS_TEST_EXPECT_MSG_EQ(m_expiredTimes.size(), 0, "Timer did fire while being suppressed ??");
184 }
185 
186 void
188 {
189  tricklePtr->ConsistentEvent();
190  Simulator::Schedule(interval,
192  this,
193  interval,
194  tricklePtr);
195 }
196 
197 void
199 {
200  TestSteadyState(Time(1));
203 }
204 
210 {
211  public:
214  : TestSuite("trickle-timer")
215  {
217  }
218 };
219 
225 
226 } // namespace tests
227 
228 } // namespace ns3
#define min(a, b)
Definition: 80211b.c:41
#define max(a, b)
Definition: 80211b.c:42
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:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
A Trickle Timer following RFC 6206.
Definition: trickle-timer.h:74
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.
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:996
#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:830
#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:251
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
static TrickleTimerTestSuite g_trickleTimerTestSuite
TrickleTimerTestSuite instance variable.
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:839
Every class exported by the ns3 library is enclosed in the ns3 namespace.