Skip to content

Commit

Permalink
[test] Added tests for CUnitQueue
Browse files Browse the repository at this point in the history
Covers some issues related to Local storage depleted issue Haivision#486
  • Loading branch information
maxsharabayko committed Jun 9, 2020
1 parent 1a8ec9f commit 8d95e5f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/filelist.maf
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ test_seqno.cpp
test_socket_options.cpp
test_sync.cpp
test_timer.cpp
test_unitqueue.cpp
test_utilities.cpp
88 changes: 88 additions & 0 deletions test/test_unitqueue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <array>
#include <vector>
#include "gtest/gtest.h"
#include "queue.h"


using namespace std;


/// Create CUnitQueue with queue size of 4 units.
/// The size of 4 is chosen on purpose, because
/// CUnitQueue::getNextAvailUnit(..) has the following
/// condition `if (m_iCount * 10 > m_iSize * 9)`. With m_iSize = 4
/// it will be false up until m_iCount becomes 4.
/// And there was an issue in getNextAvailUnit(..) in taking
/// the very last element of the queue (it was skipped).
TEST(CUnitQueue, Increase)
{
const int buffer_size_pkts = 4;
CUnitQueue unit_queue;
unit_queue.init(buffer_size_pkts, 1500, AF_INET);

vector<CUnit*> taken_units;
for (int i = 0; i < 5 * buffer_size_pkts; ++i)
{
CUnit* unit = unit_queue.getNextAvailUnit();
ASSERT_NE(unit, nullptr);
unit_queue.makeUnitGood(unit);
taken_units.push_back(unit);
}
}

/// Create CUnitQueue with queue size of 4 units.
/// Then after requesting the 5th unit, free the previous
/// four units. This makes the previous queue completely free.
/// Requesting the 5th unit, there would be 3 units available in the
/// beginning of the same queue.
TEST(CUnitQueue, IncreaseAndFree)
{
const int buffer_size_pkts = 4;
CUnitQueue unit_queue;
unit_queue.init(buffer_size_pkts, 1500, AF_INET);

CUnit* taken_unit = nullptr;
for (int i = 0; i < 5 * buffer_size_pkts; ++i)
{
CUnit* unit = unit_queue.getNextAvailUnit();
ASSERT_NE(unit, nullptr);
unit_queue.makeUnitGood(unit);

if (taken_unit)
unit_queue.makeUnitFree(taken_unit);

taken_unit = unit;
}
}

/// Create CUnitQueue with queue size of 4 units.
/// Then after requesting the 5th unit, free the previous
/// four units. This makes the previous queue completely free.
/// Requesting the 9th unit, there would be 4 units available in the
/// Thus the test checks if
TEST(CUnitQueue, IncreaseAndFreeGrouped)
{
const int buffer_size_pkts = 4;
CUnitQueue unit_queue;
unit_queue.init(buffer_size_pkts, 1500, AF_INET);

vector<CUnit*> taken_units;
for (int i = 0; i < 5 * buffer_size_pkts; ++i)
{
CUnit* unit = unit_queue.getNextAvailUnit();
ASSERT_NE(unit, nullptr);
unit_queue.makeUnitGood(unit);

if (taken_units.size() >= buffer_size_pkts)
{
for_each(taken_units.begin(), taken_units.end(),
[&unit_queue](CUnit* u) { unit_queue.makeUnitFree(u); });

taken_units.clear();
}

taken_units.push_back(unit);
EXPECT_LE(unit_queue.capacity(), 2 * buffer_size_pkts)
<< "Buffer capacity should not exceed two queues of 4 units";
}
}

0 comments on commit 8d95e5f

Please sign in to comment.