A Discrete-Event Network Simulator
API
trickle-timer-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2020 Universita' di Firenze, Italy
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19  */
20 
21 #include "ns3/trickle-timer.h"
22 #include "ns3/test.h"
23 #include <vector>
24 #include <numeric>
25 #include <algorithm>
26 
42 namespace ns3 {
43 
44 namespace tests {
45 
46 
52 {
53 public:
56  virtual void DoRun (void);
60  void ExpireTimer (void);
61  std::vector<Time> m_expiredTimes;
62 
66  void TransientOver (void);
67 
72  void TestSteadyState (Time unit);
73 
78  void TestRedundancy (Time unit);
79 
85  void ConsistentEvent (Time interval, TrickleTimer* tricklePtr);
86 
88 };
89 
91  : TestCase ("Check the Trickle Timer algorithm")
92 {}
93 
94 void
96 {
97  if (m_enableDataCollection==false)
98  {
99  return;
100  }
101 
102  m_expiredTimes.push_back (Simulator::Now ());
103 }
104 
105 void
107 {
108  m_enableDataCollection = true;
109 }
110 
111 void
113 {
114  m_expiredTimes.clear ();
115  m_enableDataCollection = false;
116 
117  TrickleTimer trickle (unit, 4, 1);
119  trickle.Enable ();
120  // We reset the timer to force the interval to the minimum
121  trickle.Reset ();
122 
123  NS_TEST_EXPECT_MSG_EQ (trickle.GetDoublings (), 4, "The doublings re-compute mechanism is not working.");
124 
125  // The transient is over at (exp2(doublings +1) -1) * MinInterval (worst case).
127 
128  Simulator::Stop (unit * 50000);
129 
130  Simulator::Run ();
132 
133  std::vector<Time> expirationFrequency;
134 
135  expirationFrequency.resize (m_expiredTimes.size ());
136  std::adjacent_difference (m_expiredTimes.begin (), m_expiredTimes.end (), expirationFrequency.begin ());
137  expirationFrequency.erase (expirationFrequency.begin ());
138 
139  int64x64_t min = (*std::min_element (expirationFrequency.begin (), expirationFrequency.end ()))/unit;
140  int64x64_t max = (*std::max_element (expirationFrequency.begin (), expirationFrequency.end ()))/unit;
141 
142  NS_TEST_EXPECT_MSG_GT_OR_EQ (min.GetDouble (), 8, "Timer did fire too fast ??");
143  NS_TEST_EXPECT_MSG_LT_OR_EQ (max.GetDouble (), 24, "Timer did fire too slow ??");
144 }
145 
146 void
148 {
149  m_expiredTimes.clear ();
150  m_enableDataCollection = false;
151 
152  TrickleTimer trickle (unit, 4, 1);
154  trickle.Enable ();
155  // We reset the timer to force the interval to the minimum
156  trickle.Reset ();
157 
158  NS_TEST_EXPECT_MSG_EQ (trickle.GetDoublings (), 4, "The doublings re-compute mechanism is not working.");
159 
160  // The transient is over at (exp2(doublings +1) -1) * MinInterval (worst case).
162  Simulator::Schedule (unit*31, &TrickleTimerTestCase::ConsistentEvent, this, unit*8, &trickle);
163 
164  Simulator::Stop (unit * 50000);
165 
166  Simulator::Run ();
168 
169  NS_TEST_EXPECT_MSG_EQ (m_expiredTimes.size (), 0, "Timer did fire while being suppressed ??");
170 }
171 
172 void
174 {
175  tricklePtr->ConsistentEvent ();
176  Simulator::Schedule (interval, &TrickleTimerTestCase::ConsistentEvent, this, interval, tricklePtr);
177 }
178 
179 
180 void
182 {
183  TestSteadyState (Time (1));
184  TestSteadyState (Seconds (1));
185  TestRedundancy (Seconds (1));
186 }
187 
188 
194 {
195 public:
198  : TestSuite ("trickle-timer")
199  {
201  }
202 };
203 
209 
210 
211 } // namespace tests
212 
213 } // namespace ns3
#define min(a, b)
Definition: 80211b.c:42
#define max(a, b)
Definition: 80211b.c:43
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
A Trickle Timer following RFC 6206.
Definition: trickle-timer.h:74
void Reset()
Reset the timer.
void SetFunction(FN fn)
Set the function to execute when the timer expires.
void Enable()
Enable the timer.
uint8_t GetDoublings(void) const
Get the doublings of the timer.
void ConsistentEvent()
Records a consistent event.
High precision numerical type, implementing Q64.64 fixed precision.
Definition: int64x64-128.h:56
void TransientOver(void)
Function to signal that the transient is over.
void TestRedundancy(Time unit)
Test the redundancy suppression.
bool m_enableDataCollection
Collect data if true.
void TestSteadyState(Time unit)
Test the steady-state.
std::vector< Time > m_expiredTimes
Time when TrickleTimer expired.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void ExpireTimer(void)
Function to invoke when TrickleTimer expires.
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:935
#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:785
#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:240
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
static TrickleTimerTestSuite g_trickleTimerTestSuite
TrickleTimerTestSuite instance variable.
Every class exported by the ns3 library is enclosed in the ns3 namespace.