21 #include "ns3/simulator.h"
22 #include "ns3/list-scheduler.h"
23 #include "ns3/heap-scheduler.h"
24 #include "ns3/map-scheduler.h"
25 #include "ns3/calendar-scheduler.h"
26 #include "ns3/config.h"
27 #include "ns3/string.h"
28 #include "ns3/system-thread.h"
46 void DoNothing (
unsigned int threadno);
47 static void SchedulingThread (std::pair<ThreadedSimulatorEventsTestCase *, unsigned int> context);
53 unsigned int m_threads;
54 bool m_threadWaiting[MAXTHREADS];
57 std::string m_simulatorType;
59 std::list<Ptr<SystemThread> > m_threadlist;
62 virtual void DoSetup (
void);
63 virtual void DoRun (
void);
64 virtual void DoTeardown (
void);
67 ThreadedSimulatorEventsTestCase::ThreadedSimulatorEventsTestCase (
ObjectFactory schedulerFactory,
const std::string &simulatorType,
unsigned int threads)
68 :
TestCase (
"Check that threaded event handling is working with " +
69 schedulerFactory.GetTypeId ().GetName () +
" in " + simulatorType),
71 m_schedulerFactory (schedulerFactory),
72 m_simulatorType (simulatorType)
77 ThreadedSimulatorEventsTestCase::End (
void)
80 for (std::list<
Ptr<SystemThread> >::iterator it2 = m_threadlist.begin(); it2 != m_threadlist.end(); ++it2)
86 ThreadedSimulatorEventsTestCase::SchedulingThread (std::pair<ThreadedSimulatorEventsTestCase *, unsigned int> context)
89 unsigned int threadno = context.second;
93 me->m_threadWaiting[threadno] =
true;
94 Simulator::ScheduleWithContext (uint32_t (-1),
96 &ThreadedSimulatorEventsTestCase::DoNothing, me, threadno);
97 while (!me->m_stop && me->m_threadWaiting[threadno])
102 nanosleep (&ts, NULL);
107 ThreadedSimulatorEventsTestCase::DoNothing (
unsigned int threadno)
109 if (!m_error.empty())
111 m_error =
"Bad threaded scheduling";
113 m_threadWaiting[threadno] =
false;
116 ThreadedSimulatorEventsTestCase::A (
int a)
118 if (m_a != m_b || m_a != m_c || m_a != m_d)
120 m_error =
"Bad scheduling";
124 Simulator::Schedule (MicroSeconds (10),
125 &ThreadedSimulatorEventsTestCase::B,
this, a+1);
129 ThreadedSimulatorEventsTestCase::B (
int b)
131 if (m_a != (m_b+1) || m_a != (m_c+1) || m_a != (m_d+1))
133 m_error =
"Bad scheduling";
137 Simulator::Schedule (MicroSeconds (10),
138 &ThreadedSimulatorEventsTestCase::C,
this, b+1);
142 ThreadedSimulatorEventsTestCase::C (
int c)
144 if (m_a != m_b || m_a != (m_c+1) || m_a != (m_d+1))
146 m_error =
"Bad scheduling";
150 Simulator::Schedule (MicroSeconds (10),
151 &ThreadedSimulatorEventsTestCase::D,
this, c+1);
155 ThreadedSimulatorEventsTestCase::D (
int d)
157 if (m_a != m_b || m_a != m_c || m_a != (m_d+1))
159 m_error =
"Bad scheduling";
169 Simulator::Schedule (MicroSeconds (10),
170 &ThreadedSimulatorEventsTestCase::A,
this, d+1);
177 if (!m_simulatorType.empty())
179 Config::SetGlobal (
"SimulatorImplementationType",
StringValue (m_simulatorType));
189 for (
unsigned int i=0; i < m_threads; ++i)
191 m_threadlist.push_back(
192 Create<SystemThread> (MakeBoundCallback (
193 &ThreadedSimulatorEventsTestCase::SchedulingThread,
194 std::pair<ThreadedSimulatorEventsTestCase *, unsigned int>(
this,i) )) );
200 m_threadlist.clear();
202 Config::SetGlobal (
"SimulatorImplementationType",
StringValue (
"ns3::DefaultSimulatorImpl"));
208 Simulator::SetScheduler (m_schedulerFactory);
210 Simulator::Schedule (
MicroSeconds (10), &ThreadedSimulatorEventsTestCase::A,
this, 1);
211 Simulator::Schedule (
Seconds (1), &ThreadedSimulatorEventsTestCase::End,
this);
214 for (std::list<
Ptr<SystemThread> >::iterator it = m_threadlist.begin(); it != m_threadlist.end(); ++it)
220 Simulator::Destroy ();
222 NS_TEST_EXPECT_MSG_EQ (m_error.empty(),
true, m_error.c_str());
223 NS_TEST_EXPECT_MSG_EQ (m_a, m_b,
"Bad scheduling");
224 NS_TEST_EXPECT_MSG_EQ (m_a, m_c,
"Bad scheduling");
225 NS_TEST_EXPECT_MSG_EQ (m_a, m_d,
"Bad scheduling");
234 std::string simulatorTypes[] = {
236 "ns3::RealtimeSimulatorImpl",
238 "ns3::DefaultSimulatorImpl"
240 std::string schedulerTypes[] = {
241 "ns3::ListScheduler",
242 "ns3::HeapScheduler",
244 "ns3::CalendarScheduler"
246 unsigned int threadcounts[] = {
254 for (
unsigned int i=0; i < (
sizeof(simulatorTypes) /
sizeof(simulatorTypes[0])); ++i)
256 for (
unsigned int j=0; j < (
sizeof(threadcounts) /
sizeof(threadcounts[0])); ++j)
258 for (
unsigned int k=0; k < (
sizeof(schedulerTypes) /
sizeof(schedulerTypes[0])); ++k)
266 } g_threadedSimulatorTestSuite;
smart pointer class similar to boost::intrusive_ptr
hold variables of type string
void SetTypeId(TypeId tid)
TestSuite(std::string name, Type type=UNIT)
Constuct a new test suite.
virtual void DoRun(void)
Implementation to actually run this test case.
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
instantiate subclasses of ns3::Object.
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
virtual void DoTeardown(void)
Implementation to do any local setup required for this test case.
virtual void DoSetup(void)
Implementation to do any local setup required for this test case.
Time MicroSeconds(uint64_t us)
create ns3::Time instances in units of microseconds.