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
34using namespace ns3;
35
43{
44 public:
52 ~FifoQueueDiscTestItem() override;
53
54 // Delete default constructor, copy constructor and assignment operator to avoid misuse
58
59 void AddHeader() override;
60 bool Mark() override;
61};
62
64 : QueueDiscItem(p, addr, 0)
65{
66}
67
69{
70}
71
72void
74{
75}
76
77bool
79{
80 return false;
81}
82
90{
91 public:
93 void DoRun() override;
94
95 private:
100 void RunFifoTest(QueueSizeUnit mode);
108};
109
111 : TestCase("Sanity check on the fifo queue disc implementation")
112{
113}
114
115void
117{
118 std::vector<uint64_t> uids;
119 Ptr<Packet> p;
121 Address dest;
122 uint32_t modeSize = (q->GetMaxSize().GetUnit() == QueueSizeUnit::PACKETS ? 1 : pktSize);
123 uint32_t numPackets = qSize / modeSize;
124
125 NS_TEST_ASSERT_MSG_EQ(q->GetCurrentSize().GetValue(), 0, "The queue disc should be empty");
126
127 // create and enqueue numPackets packets and store their UIDs; check they are all enqueued
128 for (uint32_t i = 1; i <= numPackets; i++)
129 {
130 p = Create<Packet>(pktSize);
131 uids.push_back(p->GetUid());
132 q->Enqueue(Create<FifoQueueDiscTestItem>(p, dest));
133 NS_TEST_ASSERT_MSG_EQ(q->GetCurrentSize().GetValue(),
134 i * modeSize,
135 "There should be " << i << " packet(s) in there");
136 }
137
138 // no room for another packet
139 NS_TEST_ASSERT_MSG_EQ(q->Enqueue(Create<FifoQueueDiscTestItem>(p, dest)),
140 false,
141 "There should be no room for another packet");
142
143 // dequeue and check packet order
144 for (uint32_t i = 1; i <= numPackets; i++)
145 {
146 item = q->Dequeue();
147 NS_TEST_ASSERT_MSG_NE(item, nullptr, "A packet should have been dequeued");
148 NS_TEST_ASSERT_MSG_EQ(q->GetCurrentSize().GetValue(),
149 (numPackets - i) * modeSize,
150 "There should be " << numPackets - i << " packet(s) in there");
151 NS_TEST_ASSERT_MSG_EQ(item->GetPacket()->GetUid(),
152 uids[i - 1],
153 "was this the right packet?");
154 }
155
156 item = q->Dequeue();
157 NS_TEST_ASSERT_MSG_EQ(item, nullptr, "There are really no packets in there");
158}
159
160void
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_ASSERT_MSG_EQ(queue->GetNInternalQueues(),
172 0,
173 "Verify that the queue disc has no internal queue");
174
176 queue->SetAttributeFailSafe("MaxSize",
177 QueueSizeValue(QueueSize(mode, numPackets * modeSize))),
178 true,
179 "Verify that we can actually set the attribute MaxSize");
180
181 queue->Initialize();
182
183 DoRunFifoTest(queue, numPackets * modeSize, pktSize);
184
185 // test 2: set the limit on the queue disc after initialization
186 queue = CreateObject<FifoQueueDisc>();
187
188 NS_TEST_ASSERT_MSG_EQ(queue->GetNInternalQueues(),
189 0,
190 "Verify that the queue disc has no internal queue");
191
192 queue->Initialize();
193
195 queue->SetAttributeFailSafe("MaxSize",
196 QueueSizeValue(QueueSize(mode, numPackets * modeSize))),
197 true,
198 "Verify that we can actually set the attribute MaxSize");
199
200 DoRunFifoTest(queue, numPackets * modeSize, pktSize);
201
202 // test 3: set the limit on the internal queue before initialization
203 queue = CreateObject<FifoQueueDisc>();
204
205 NS_TEST_ASSERT_MSG_EQ(queue->GetNInternalQueues(),
206 0,
207 "Verify that the queue disc has no internal queue");
208
209 ObjectFactory factory;
210 factory.SetTypeId("ns3::DropTailQueue<QueueDiscItem>");
211 if (mode == QueueSizeUnit::PACKETS)
212 {
213 factory.Set("MaxSize", QueueSizeValue(QueueSize(QueueSizeUnit::PACKETS, numPackets)));
214 }
215 else
216 {
217 factory.Set("MaxSize",
219 }
220 queue->AddInternalQueue(factory.Create<QueueDisc::InternalQueue>());
221
222 queue->Initialize();
223
224 DoRunFifoTest(queue, numPackets * modeSize, pktSize);
225
226 // test 4: set the limit on the internal queue after initialization
227 queue = CreateObject<FifoQueueDisc>();
228
229 NS_TEST_ASSERT_MSG_EQ(queue->GetNInternalQueues(),
230 0,
231 "Verify that the queue disc has no internal queue");
232
233 queue->Initialize();
234
235 NS_TEST_ASSERT_MSG_EQ(queue->GetNInternalQueues(),
236 1,
237 "Verify that the queue disc got an internal queue");
238
239 Ptr<QueueDisc::InternalQueue> iq = queue->GetInternalQueue(0);
240
241 if (mode == QueueSizeUnit::PACKETS)
242 {
244 iq->SetAttributeFailSafe("MaxSize",
246 true,
247 "Verify that we can actually set the attribute MaxSize on the internal queue");
248 }
249 else
250 {
252 iq->SetAttributeFailSafe(
253 "MaxSize",
255 true,
256 "Verify that we can actually set the attribute MaxSize on the internal queue");
257 }
258
259 DoRunFifoTest(queue, numPackets * modeSize, pktSize);
260}
261
262void
264{
267 Simulator::Destroy();
268}
269
277{
278 public:
280 : TestSuite("fifo-queue-disc", UNIT)
281 {
282 AddTestCase(new FifoQueueDiscTestCase(), TestCase::QUICK);
283 }
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(const FifoQueueDiscTestItem &)=delete
FifoQueueDiscTestItem & operator=(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:92
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
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:133
Class for representing queue sizes.
Definition: queue-size.h:96
AttributeValue implementation for QueueSize.
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:305
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)