A Discrete-Event Network Simulator
API
watchdog-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) 2007 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "ns3/watchdog.h"
21 #include "ns3/test.h"
22 
31 namespace ns3 {
32 
33 namespace tests {
34 
35 
40 class WatchdogTestCase : public TestCase
41 {
42 public:
45  virtual void DoRun (void);
50  void Expire (int arg);
51  bool m_expired;
54 };
55 
57  : TestCase ("Check that we can keepalive a watchdog")
58 {}
59 
60 void
62 {
63  m_expired = true;
65  m_expiredArgument = arg;
66 }
67 
68 void
70 {
71  m_expired = false;
73  m_expiredTime = Seconds (0);
74 
75  Watchdog watchdog;
76  watchdog.SetFunction (&WatchdogTestCase::Expire, this);
77  watchdog.SetArguments (1);
78  watchdog.Ping (MicroSeconds (10));
82  Simulator::Run ();
84  NS_TEST_ASSERT_MSG_EQ (m_expired, true, "The timer did not expire ??");
85  NS_TEST_ASSERT_MSG_EQ (m_expiredTime, MicroSeconds (40), "The timer did not expire at the expected time ?");
86  NS_TEST_ASSERT_MSG_EQ (m_expiredArgument, 1, "We did not get the right argument");
87 }
88 
89 
95 {
96 public:
99  : TestSuite ("watchdog")
100  {
101  AddTestCase (new WatchdogTestCase ());
102  }
103 };
104 
110 
111 
112 } // namespace tests
113 
114 } // namespace ns3
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
void Ping(Time delay)
Delay the timer.
Definition: watchdog.cc:50
bool m_expired
Flag for expired Watchdog.
A suite of tests to run.
Definition: test.h:1343
static void Run(void)
Run the simulation.
Definition: simulator.cc:170
encapsulates test code
Definition: test.h:1153
void SetFunction(FN fn)
Set the function to execute when the timer expires.
Definition: watchdog.h:199
A very simple watchdog operating in virtual time.
Definition: watchdog.h:56
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1390
static WatchdogTestSuite g_watchdogTestSuite
WatchdogTestSuite instance variable.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
#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:166
Time m_expiredTime
Time when Watchdog expired.
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:134
virtual void DoRun(void)
Implementation to actually run this TestCase.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void Expire(int arg)
Function to invoke when Watchdog expires.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:193
int m_expiredArgument
Argument supplied to expired Watchdog.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1070
void SetArguments(T1 a1)
Set the arguments to be used when invoking the expire function.
Definition: watchdog.h:214
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1086