A Discrete-Event Network Simulator
API
fifo-queue-disc-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) 2017 Universita' degli Studi di Napoli Federico II
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  * Authors: Stefano Avallone <stavallo@unina.it>
19  *
20  */
21 
22 #include "ns3/test.h"
23 #include "ns3/fifo-queue-disc.h"
24 #include "ns3/queue.h"
25 #include "ns3/packet.h"
26 #include "ns3/uinteger.h"
27 #include "ns3/string.h"
28 #include "ns3/double.h"
29 #include "ns3/log.h"
30 #include "ns3/simulator.h"
31 #include "ns3/object-factory.h"
32 #include <vector>
33 
34 using namespace ns3;
35 
43 {
44 public:
51  FifoQueueDiscTestItem (Ptr<Packet> p, const Address & addr);
52  virtual ~FifoQueueDiscTestItem ();
53  virtual void AddHeader (void);
54  virtual bool Mark (void);
55 
56 private:
69 };
70 
72  : QueueDiscItem (p, addr, 0)
73 {
74 }
75 
77 {
78 }
79 
80 void
82 {
83 }
84 
85 bool
87 {
88  return false;
89 }
90 
98 {
99 public:
101  virtual void DoRun (void);
102 private:
107  void RunFifoTest (QueueSizeUnit mode);
114  void DoRunFifoTest (Ptr<FifoQueueDisc> q, uint32_t qSize, uint32_t pktSize);
115 };
116 
118  : TestCase ("Sanity check on the fifo queue disc implementation")
119 {
120 }
121 
122 void
124 {
125  std::vector<uint64_t> uids;
126  Ptr<Packet> p;
127  Ptr<QueueDiscItem> item;
128  Address dest;
129  uint32_t modeSize = (q->GetMaxSize ().GetUnit () == QueueSizeUnit::PACKETS ? 1 : pktSize);
130  uint32_t numPackets = qSize / modeSize;
131 
132  NS_TEST_EXPECT_MSG_EQ (q->GetCurrentSize ().GetValue (), 0, "The queue disc should be empty");
133 
134  // create and enqueue numPackets packets and store their UIDs; check they are all enqueued
135  for (uint32_t i = 1; i <= numPackets; i++)
136  {
137  p = Create<Packet> (pktSize);
138  uids.push_back (p->GetUid ());
139  q->Enqueue (Create<FifoQueueDiscTestItem> (p, dest));
140  NS_TEST_EXPECT_MSG_EQ (q->GetCurrentSize ().GetValue (), i * modeSize, "There should be " << i << " packet(s) in there");
141  }
142 
143  // no room for another packet
144  NS_TEST_EXPECT_MSG_EQ (q->Enqueue (Create<FifoQueueDiscTestItem> (p, dest)),
145  false, "There should be no room for another packet");
146 
147  // dequeue and check packet order
148  for (uint32_t i = 1; i <= numPackets; i++)
149  {
150  item = q->Dequeue ();
151  NS_TEST_EXPECT_MSG_EQ ((item != 0), true, "A packet should have been dequeued");
152  NS_TEST_EXPECT_MSG_EQ (q->GetCurrentSize ().GetValue (), (numPackets-i) * modeSize, "There should be " << numPackets-i << " packet(s) in there");
153  NS_TEST_EXPECT_MSG_EQ (item->GetPacket ()->GetUid (), uids[i-1], "was this the right packet?");
154  }
155 
156  item = q->Dequeue ();
157  NS_TEST_EXPECT_MSG_EQ ((item == 0), true, "There are really no packets in there");
158 }
159 
160 void
162 {
163  Ptr<FifoQueueDisc> queue;
164  uint32_t numPackets = 10;
165  uint32_t pktSize = 1000;
166  uint32_t modeSize = (mode == QueueSizeUnit::PACKETS ? 1 : pktSize);
167 
168  // test 1: set the limit on the queue disc before initialization
169  queue = CreateObject<FifoQueueDisc> ();
170 
171  NS_TEST_EXPECT_MSG_EQ (queue->GetNInternalQueues (), 0, "Verify that the queue disc has no internal queue");
172 
173  NS_TEST_EXPECT_MSG_EQ (queue->SetAttributeFailSafe ("MaxSize",
174  QueueSizeValue (QueueSize (mode, numPackets*modeSize))),
175  true, "Verify that we can actually set the attribute MaxSize");
176 
177  queue->Initialize ();
178 
179  DoRunFifoTest (queue, numPackets*modeSize, pktSize);
180 
181 
182  // test 2: set the limit on the queue disc after initialization
183  queue = CreateObject<FifoQueueDisc> ();
184 
185  NS_TEST_EXPECT_MSG_EQ (queue->GetNInternalQueues (), 0, "Verify that the queue disc has no internal queue");
186 
187  queue->Initialize ();
188 
189  NS_TEST_EXPECT_MSG_EQ (queue->SetAttributeFailSafe ("MaxSize",
190  QueueSizeValue (QueueSize (mode, numPackets*modeSize))),
191  true, "Verify that we can actually set the attribute MaxSize");
192 
193  DoRunFifoTest (queue, numPackets*modeSize, pktSize);
194 
195 
196  // test 3: set the limit on the internal queue before initialization
197  queue = CreateObject<FifoQueueDisc> ();
198 
199  NS_TEST_EXPECT_MSG_EQ (queue->GetNInternalQueues (), 0, "Verify that the queue disc has no internal queue");
200 
201  ObjectFactory factory;
202  factory.SetTypeId ("ns3::DropTailQueue<QueueDiscItem>");
203  if (mode == QueueSizeUnit::PACKETS)
204  {
205  factory.Set ("MaxSize", QueueSizeValue (QueueSize (QueueSizeUnit::PACKETS, numPackets)));
206  }
207  else
208  {
209  factory.Set ("MaxSize", QueueSizeValue (QueueSize (QueueSizeUnit::BYTES, numPackets*pktSize)));
210  }
211  queue->AddInternalQueue (factory.Create<QueueDisc::InternalQueue> ());
212 
213  queue->Initialize ();
214 
215  DoRunFifoTest (queue, numPackets*modeSize, pktSize);
216 
217 
218  // test 4: set the limit on the internal queue after initialization
219  queue = CreateObject<FifoQueueDisc> ();
220 
221  NS_TEST_EXPECT_MSG_EQ (queue->GetNInternalQueues (), 0, "Verify that the queue disc has no internal queue");
222 
223  queue->Initialize ();
224 
225  NS_TEST_EXPECT_MSG_EQ (queue->GetNInternalQueues (), 1, "Verify that the queue disc got an internal queue");
226 
227  Ptr<QueueDisc::InternalQueue> iq = queue->GetInternalQueue (0);
228 
229  if (mode == QueueSizeUnit::PACKETS)
230  {
231  NS_TEST_EXPECT_MSG_EQ (iq->SetAttributeFailSafe ("MaxSize",
233  true, "Verify that we can actually set the attribute MaxSize on the internal queue");
234  }
235  else
236  {
237  NS_TEST_EXPECT_MSG_EQ (iq->SetAttributeFailSafe ("MaxSize",
239  true, "Verify that we can actually set the attribute MaxSize on the internal queue");
240  }
241 
242  DoRunFifoTest (queue, numPackets*modeSize, pktSize);
243 }
244 
245 void
247 {
250  Simulator::Destroy ();
251 }
252 
259 static class FifoQueueDiscTestSuite : public TestSuite
260 {
261 public:
263  : TestSuite ("fifo-queue-disc", UNIT)
264  {
265  AddTestCase (new FifoQueueDiscTestCase (), TestCase::QUICK);
266  }
Fifo Queue Disc Test Case.
void RunFifoTest(QueueSizeUnit mode)
Run test function.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void DoRunFifoTest(Ptr< FifoQueueDisc > q, uint32_t qSize, uint32_t pktSize)
Run test function.
Fifo Queue Disc Test Item.
virtual void AddHeader(void)
Add the header to the packet.
virtual bool Mark(void)
Marks the packet as a substitute for dropping it, such as for Explicit Congestion Notification.
FifoQueueDiscTestItem(const FifoQueueDiscTestItem &)
Copy constructor Disable default implementation to avoid misuse.
Fifo Queue Disc Test Suite.
a polymophic address class
Definition: address.h:91
Instantiate subclasses of ns3::Object.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:390
Introspection did not find any typical Config paths.
QueueDiscItem is the abstract base class for items that are stored in a queue disc.
Definition: queue-item.h:148
Class for representing queue sizes.
Definition: queue-size.h:95
encapsulates test code
Definition: test.h:1154
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:1344
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1353
QueueSizeUnit
Enumeration of the operating modes of queues.
Definition: queue-size.h:43
@ BYTES
Use number of bytes for queue size.
Definition: queue-size.h:45
@ PACKETS
Use number of packets for queue size.
Definition: queue-size.h:44
#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:283
FifoQueueDiscTestSuite g_fifoQueueTestSuite
the test suite
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint32_t pktSize
packet size used for the simulation (in bytes)
Definition: wifi-bianchi.cc:86