A Discrete-Event Network Simulator
API
fifo-queue-disc-test-suite.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Universita' degli Studi di Napoli Federico II
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  * Authors: Stefano Avallone <stavallo@unina.it>
18  *
19  */
20 
21 #include "ns3/double.h"
22 #include "ns3/fifo-queue-disc.h"
23 #include "ns3/log.h"
24 #include "ns3/object-factory.h"
25 #include "ns3/packet.h"
26 #include "ns3/queue.h"
27 #include "ns3/simulator.h"
28 #include "ns3/string.h"
29 #include "ns3/test.h"
30 #include "ns3/uinteger.h"
31 
32 #include <vector>
33 
34 using namespace ns3;
35 
42 {
43  public:
51  ~FifoQueueDiscTestItem() override;
52 
53  // Delete default constructor, copy constructor and assignment operator to avoid misuse
57 
58  void AddHeader() override;
59  bool Mark() override;
60 };
61 
63  : QueueDiscItem(p, addr, 0)
64 {
65 }
66 
68 {
69 }
70 
71 void
73 {
74 }
75 
76 bool
78 {
79  return false;
80 }
81 
88 {
89  public:
91  void DoRun() override;
92 
93  private:
98  void RunFifoTest(QueueSizeUnit mode);
105  void DoRunFifoTest(Ptr<FifoQueueDisc> q, uint32_t qSize, uint32_t pktSize);
106 };
107 
109  : TestCase("Sanity check on the fifo queue disc implementation")
110 {
111 }
112 
113 void
115 {
116  std::vector<uint64_t> uids;
117  Ptr<Packet> p;
118  Ptr<QueueDiscItem> item;
119  Address dest;
120  uint32_t modeSize = (q->GetMaxSize().GetUnit() == QueueSizeUnit::PACKETS ? 1 : pktSize);
121  uint32_t numPackets = qSize / modeSize;
122 
123  NS_TEST_ASSERT_MSG_EQ(q->GetCurrentSize().GetValue(), 0, "The queue disc should be empty");
124 
125  // create and enqueue numPackets packets and store their UIDs; check they are all enqueued
126  for (uint32_t i = 1; i <= numPackets; i++)
127  {
128  p = Create<Packet>(pktSize);
129  uids.push_back(p->GetUid());
130  q->Enqueue(Create<FifoQueueDiscTestItem>(p, dest));
131  NS_TEST_ASSERT_MSG_EQ(q->GetCurrentSize().GetValue(),
132  i * modeSize,
133  "There should be " << i << " packet(s) in there");
134  }
135 
136  // no room for another packet
137  NS_TEST_ASSERT_MSG_EQ(q->Enqueue(Create<FifoQueueDiscTestItem>(p, dest)),
138  false,
139  "There should be no room for another packet");
140 
141  // dequeue and check packet order
142  for (uint32_t i = 1; i <= numPackets; i++)
143  {
144  item = q->Dequeue();
145  NS_TEST_ASSERT_MSG_NE(item, nullptr, "A packet should have been dequeued");
146  NS_TEST_ASSERT_MSG_EQ(q->GetCurrentSize().GetValue(),
147  (numPackets - i) * modeSize,
148  "There should be " << numPackets - i << " packet(s) in there");
149  NS_TEST_ASSERT_MSG_EQ(item->GetPacket()->GetUid(),
150  uids[i - 1],
151  "was this the right packet?");
152  }
153 
154  item = q->Dequeue();
155  NS_TEST_ASSERT_MSG_EQ(item, nullptr, "There are really no packets in there");
156 }
157 
158 void
160 {
161  Ptr<FifoQueueDisc> queue;
162  uint32_t numPackets = 10;
163  uint32_t pktSize = 1000;
164  uint32_t modeSize = (mode == QueueSizeUnit::PACKETS ? 1 : pktSize);
165 
166  // test 1: set the limit on the queue disc before initialization
167  queue = CreateObject<FifoQueueDisc>();
168 
169  NS_TEST_ASSERT_MSG_EQ(queue->GetNInternalQueues(),
170  0,
171  "Verify that the queue disc has no internal queue");
172 
174  queue->SetAttributeFailSafe("MaxSize",
175  QueueSizeValue(QueueSize(mode, numPackets * modeSize))),
176  true,
177  "Verify that we can actually set the attribute MaxSize");
178 
179  queue->Initialize();
180 
181  DoRunFifoTest(queue, numPackets * modeSize, pktSize);
182 
183  // test 2: set the limit on the queue disc after initialization
184  queue = CreateObject<FifoQueueDisc>();
185 
186  NS_TEST_ASSERT_MSG_EQ(queue->GetNInternalQueues(),
187  0,
188  "Verify that the queue disc has no internal queue");
189 
190  queue->Initialize();
191 
193  queue->SetAttributeFailSafe("MaxSize",
194  QueueSizeValue(QueueSize(mode, numPackets * modeSize))),
195  true,
196  "Verify that we can actually set the attribute MaxSize");
197 
198  DoRunFifoTest(queue, numPackets * modeSize, pktSize);
199 
200  // test 3: set the limit on the internal queue before initialization
201  queue = CreateObject<FifoQueueDisc>();
202 
203  NS_TEST_ASSERT_MSG_EQ(queue->GetNInternalQueues(),
204  0,
205  "Verify that the queue disc has no internal queue");
206 
207  ObjectFactory factory;
208  factory.SetTypeId("ns3::DropTailQueue<QueueDiscItem>");
209  if (mode == QueueSizeUnit::PACKETS)
210  {
211  factory.Set("MaxSize", QueueSizeValue(QueueSize(QueueSizeUnit::PACKETS, numPackets)));
212  }
213  else
214  {
215  factory.Set("MaxSize",
216  QueueSizeValue(QueueSize(QueueSizeUnit::BYTES, numPackets * pktSize)));
217  }
218  queue->AddInternalQueue(factory.Create<QueueDisc::InternalQueue>());
219 
220  queue->Initialize();
221 
222  DoRunFifoTest(queue, numPackets * modeSize, pktSize);
223 
224  // test 4: set the limit on the internal queue after initialization
225  queue = CreateObject<FifoQueueDisc>();
226 
227  NS_TEST_ASSERT_MSG_EQ(queue->GetNInternalQueues(),
228  0,
229  "Verify that the queue disc has no internal queue");
230 
231  queue->Initialize();
232 
233  NS_TEST_ASSERT_MSG_EQ(queue->GetNInternalQueues(),
234  1,
235  "Verify that the queue disc got an internal queue");
236 
237  Ptr<QueueDisc::InternalQueue> iq = queue->GetInternalQueue(0);
238 
239  if (mode == QueueSizeUnit::PACKETS)
240  {
242  iq->SetAttributeFailSafe("MaxSize",
243  QueueSizeValue(QueueSize(QueueSizeUnit::PACKETS, numPackets))),
244  true,
245  "Verify that we can actually set the attribute MaxSize on the internal queue");
246  }
247  else
248  {
250  iq->SetAttributeFailSafe(
251  "MaxSize",
252  QueueSizeValue(QueueSize(QueueSizeUnit::BYTES, numPackets * pktSize))),
253  true,
254  "Verify that we can actually set the attribute MaxSize on the internal queue");
255  }
256 
257  DoRunFifoTest(queue, numPackets * modeSize, pktSize);
258 }
259 
260 void
262 {
265  Simulator::Destroy();
266 }
267 
273 static class FifoQueueDiscTestSuite : public TestSuite
274 {
275  public:
277  : TestSuite("fifo-queue-disc", UNIT)
278  {
279  AddTestCase(new FifoQueueDiscTestCase(), TestCase::QUICK);
280  }
Fifo Queue Disc Test Case.
void RunFifoTest(QueueSizeUnit mode)
Run test function.
void DoRun() override
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.
void AddHeader() override
Add the header to the packet.
FifoQueueDiscTestItem()=delete
FifoQueueDiscTestItem & operator=(const FifoQueueDiscTestItem &)=delete
FifoQueueDiscTestItem(const FifoQueueDiscTestItem &)=delete
bool Mark() override
Marks the packet as a substitute for dropping it, such as for Explicit Congestion Notification.
Fifo Queue Disc Test Suite.
a polymophic address class
Definition: address.h:101
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
uint64_t GetUid() const
Returns the packet's Uid.
Definition: packet.cc:412
QueueDiscItem is the abstract base class for items that are stored in a queue disc.
Definition: queue-item.h:133
Template class for packet Queues.
Definition: queue.h:268
Class for representing queue sizes.
Definition: queue-size.h:96
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
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
QueueSizeUnit
Enumeration of the operating modes of queues.
Definition: queue-size.h:44
@ BYTES
Use number of bytes for queue size.
Definition: queue-size.h:46
@ PACKETS
Use number of packets for queue size.
Definition: queue-size.h:45
#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:144
#define NS_TEST_ASSERT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report and abort if not.
Definition: test.h:564
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)