2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-04-11 22:01:13 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2023-10-11 08:49:50 +00:00
|
|
|
#include <packager/media/base/producer_consumer_queue.h>
|
2014-04-11 22:01:13 +00:00
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
#include <thread>
|
|
|
|
|
2023-10-13 19:42:47 +00:00
|
|
|
#include <absl/log/log.h>
|
2023-10-09 23:21:41 +00:00
|
|
|
#include <absl/synchronization/notification.h>
|
2023-10-11 08:49:50 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/status/status_test_util.h>
|
2014-04-11 22:01:13 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2014-04-11 22:01:13 +00:00
|
|
|
namespace {
|
|
|
|
const size_t kCapacity = 10u;
|
2014-09-30 21:52:21 +00:00
|
|
|
const int64_t kTimeout = 100; // 0.1s.
|
2022-11-04 22:46:41 +00:00
|
|
|
const std::chrono::milliseconds kTimeoutDuration(kTimeout);
|
2014-04-11 22:01:13 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace media {
|
|
|
|
|
|
|
|
TEST(ProducerConsumerQueueTest, CheckEmpty) {
|
|
|
|
ProducerConsumerQueue<int> queue(kUnlimitedCapacity);
|
|
|
|
EXPECT_EQ(0u, queue.Size());
|
|
|
|
EXPECT_TRUE(queue.Empty());
|
|
|
|
EXPECT_EQ(0u, queue.HeadPos());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ProducerConsumerQueueTest, PushPop) {
|
|
|
|
ProducerConsumerQueue<size_t> queue(kCapacity);
|
|
|
|
for (size_t i = 0; i < kCapacity; ++i)
|
|
|
|
ASSERT_OK(queue.Push(i, kInfiniteTimeout));
|
|
|
|
|
|
|
|
EXPECT_EQ(kCapacity, queue.Size());
|
|
|
|
EXPECT_FALSE(queue.Empty());
|
|
|
|
EXPECT_EQ(0u, queue.HeadPos());
|
|
|
|
EXPECT_EQ(kCapacity - 1, queue.TailPos());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < kCapacity; ++i) {
|
|
|
|
size_t val;
|
|
|
|
ASSERT_OK(queue.Pop(&val, kInfiniteTimeout));
|
|
|
|
EXPECT_EQ(i, val);
|
|
|
|
EXPECT_EQ(i + 1, queue.HeadPos());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ProducerConsumerQueueTest, Peek) {
|
|
|
|
ProducerConsumerQueue<size_t> queue(kCapacity);
|
|
|
|
for (size_t i = 0; i < kCapacity; ++i)
|
|
|
|
ASSERT_OK(queue.Push(i, kInfiniteTimeout));
|
|
|
|
for (size_t i = 0; i < kCapacity; ++i) {
|
|
|
|
size_t val;
|
|
|
|
ASSERT_OK(queue.Peek(i, &val, kInfiniteTimeout));
|
|
|
|
EXPECT_EQ(i, val);
|
|
|
|
// Expect head position to move along with peek position.
|
|
|
|
EXPECT_EQ(i >= kCapacity / 2 ? i - kCapacity / 2 : 0, queue.HeadPos());
|
|
|
|
}
|
|
|
|
EXPECT_EQ(kCapacity - 1, queue.TailPos());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ProducerConsumerQueueTest, PeekOnPoppedElement) {
|
|
|
|
ProducerConsumerQueue<size_t> queue(kCapacity);
|
|
|
|
for (size_t i = 0; i < kCapacity; ++i)
|
|
|
|
ASSERT_OK(queue.Push(i, kInfiniteTimeout));
|
|
|
|
size_t val;
|
|
|
|
ASSERT_OK(queue.Pop(&val, kInfiniteTimeout));
|
|
|
|
ASSERT_OK(queue.Push(kCapacity, kInfiniteTimeout));
|
|
|
|
|
|
|
|
ASSERT_OK(queue.Peek(kCapacity, &val, kInfiniteTimeout));
|
|
|
|
EXPECT_EQ(kCapacity, val);
|
|
|
|
|
|
|
|
// Expect head position to move along with peek position.
|
|
|
|
EXPECT_EQ(kCapacity / 2, queue.HeadPos());
|
|
|
|
ASSERT_OK(queue.Peek(kCapacity / 2, &val, kInfiniteTimeout));
|
|
|
|
EXPECT_EQ(kCapacity / 2, val);
|
|
|
|
|
|
|
|
ASSERT_EQ(error::INVALID_ARGUMENT,
|
|
|
|
queue.Peek(kCapacity / 2 - 2, &val, kInfiniteTimeout).error_code());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ProducerConsumerQueueTest, PushWithTimeout) {
|
2016-11-09 02:11:13 +00:00
|
|
|
ProducerConsumerQueue<size_t> queue(kCapacity);
|
2014-04-11 22:01:13 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < kCapacity; ++i) {
|
2022-11-04 22:46:41 +00:00
|
|
|
auto start = std::chrono::steady_clock::now();
|
2014-04-11 22:01:13 +00:00
|
|
|
ASSERT_OK(queue.Push(i, kTimeout));
|
2017-12-02 00:22:35 +00:00
|
|
|
// Expect Push to return without waiting for timeout.
|
2022-11-04 22:46:41 +00:00
|
|
|
auto elapsed = std::chrono::steady_clock::now() - start;
|
|
|
|
EXPECT_LT(elapsed, kTimeoutDuration);
|
2014-04-11 22:01:13 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
{
|
|
|
|
auto start = std::chrono::steady_clock::now();
|
|
|
|
ASSERT_EQ(error::TIME_OUT, queue.Push(0, kTimeout).error_code());
|
|
|
|
auto elapsed = std::chrono::steady_clock::now() - start;
|
|
|
|
EXPECT_GE(elapsed, kTimeoutDuration);
|
|
|
|
}
|
2014-04-11 22:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ProducerConsumerQueueTest, PopWithTimeout) {
|
|
|
|
ProducerConsumerQueue<size_t> queue(kCapacity);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < kCapacity; ++i)
|
|
|
|
ASSERT_OK(queue.Push(i, kInfiniteTimeout));
|
|
|
|
|
|
|
|
size_t val;
|
|
|
|
for (size_t i = 0; i < kCapacity; ++i) {
|
2022-11-04 22:46:41 +00:00
|
|
|
auto start = std::chrono::steady_clock::now();
|
2014-04-11 22:01:13 +00:00
|
|
|
ASSERT_OK(queue.Pop(&val, kTimeout));
|
2017-12-02 00:22:35 +00:00
|
|
|
// Expect Pop to return without waiting for timeout.
|
2022-11-04 22:46:41 +00:00
|
|
|
auto elapsed = std::chrono::steady_clock::now() - start;
|
|
|
|
EXPECT_LT(elapsed, kTimeoutDuration);
|
2014-04-11 22:01:13 +00:00
|
|
|
EXPECT_EQ(i, val);
|
|
|
|
}
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
{
|
|
|
|
auto start = std::chrono::steady_clock::now();
|
|
|
|
ASSERT_EQ(error::TIME_OUT, queue.Pop(&val, kTimeout).error_code());
|
|
|
|
auto elapsed = std::chrono::steady_clock::now() - start;
|
|
|
|
EXPECT_GE(elapsed, kTimeoutDuration);
|
|
|
|
}
|
2014-04-11 22:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ProducerConsumerQueueTest, PeekWithTimeout) {
|
|
|
|
ProducerConsumerQueue<size_t> queue(kCapacity);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < kCapacity; ++i)
|
|
|
|
ASSERT_OK(queue.Push(i, kInfiniteTimeout));
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
{
|
|
|
|
size_t val;
|
|
|
|
auto start = std::chrono::steady_clock::now();
|
|
|
|
ASSERT_EQ(error::TIME_OUT,
|
|
|
|
queue.Peek(kCapacity, &val, kTimeout).error_code());
|
|
|
|
auto elapsed = std::chrono::steady_clock::now() - start;
|
|
|
|
EXPECT_GE(elapsed, kTimeoutDuration);
|
|
|
|
}
|
2014-04-11 22:01:13 +00:00
|
|
|
|
|
|
|
for (size_t i = kCapacity / 2; i < kCapacity; ++i) {
|
2022-11-04 22:46:41 +00:00
|
|
|
size_t val;
|
|
|
|
auto start = std::chrono::steady_clock::now();
|
2014-04-11 22:01:13 +00:00
|
|
|
ASSERT_OK(queue.Peek(i, &val, kTimeout));
|
2017-12-02 00:22:35 +00:00
|
|
|
// Expect Peek to return without waiting for timeout.
|
2022-11-04 22:46:41 +00:00
|
|
|
auto elapsed = std::chrono::steady_clock::now() - start;
|
|
|
|
EXPECT_LT(elapsed, kTimeoutDuration);
|
2014-04-11 22:01:13 +00:00
|
|
|
EXPECT_EQ(i, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ProducerConsumerQueueTest, CheckStop) {
|
|
|
|
ProducerConsumerQueue<int> queue(kUnlimitedCapacity);
|
|
|
|
|
|
|
|
ASSERT_FALSE(queue.Stopped());
|
|
|
|
queue.Stop();
|
|
|
|
ASSERT_TRUE(queue.Stopped());
|
|
|
|
|
|
|
|
EXPECT_EQ(error::STOPPED, queue.Push(0, kInfiniteTimeout).error_code());
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
{
|
|
|
|
auto start = std::chrono::steady_clock::now();
|
|
|
|
EXPECT_EQ(error::STOPPED, queue.Push(0, kTimeout).error_code());
|
|
|
|
// Expect Push to return without waiting for timeout.
|
|
|
|
auto elapsed = std::chrono::steady_clock::now() - start;
|
|
|
|
EXPECT_LT(elapsed, kTimeoutDuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int val;
|
|
|
|
EXPECT_EQ(error::STOPPED, queue.Pop(&val, kInfiniteTimeout).error_code());
|
|
|
|
auto start = std::chrono::steady_clock::now();
|
|
|
|
EXPECT_EQ(error::STOPPED, queue.Pop(&val, kTimeout).error_code());
|
|
|
|
// Expect Pop to return without waiting for timeout.
|
|
|
|
auto elapsed = std::chrono::steady_clock::now() - start;
|
|
|
|
EXPECT_LT(elapsed, kTimeoutDuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int val;
|
|
|
|
EXPECT_EQ(error::STOPPED,
|
|
|
|
queue.Peek(0, &val, kInfiniteTimeout).error_code());
|
|
|
|
auto start = std::chrono::steady_clock::now();
|
|
|
|
EXPECT_EQ(error::STOPPED, queue.Peek(0, &val, kTimeout).error_code());
|
|
|
|
// Expect Peek to return without waiting for timeout.
|
|
|
|
auto elapsed = std::chrono::steady_clock::now() - start;
|
|
|
|
EXPECT_LT(elapsed, kTimeoutDuration);
|
|
|
|
}
|
2014-04-11 22:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class MultiThreadProducerConsumerQueueTest : public ::testing::Test {
|
|
|
|
public:
|
2022-11-04 22:46:41 +00:00
|
|
|
MultiThreadProducerConsumerQueueTest() : queue_(kCapacity) {}
|
2015-07-22 23:40:45 +00:00
|
|
|
~MultiThreadProducerConsumerQueueTest() override {}
|
2014-04-11 22:01:13 +00:00
|
|
|
|
|
|
|
protected:
|
2022-11-04 22:46:41 +00:00
|
|
|
void SetUp() override {
|
|
|
|
thread_.reset(new std::thread(
|
|
|
|
std::bind(&MultiThreadProducerConsumerQueueTest::PushTask, this)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TearDown() override {
|
|
|
|
thread_->join();
|
|
|
|
thread_.reset();
|
|
|
|
}
|
2014-04-11 22:01:13 +00:00
|
|
|
|
|
|
|
void PushTask() {
|
|
|
|
int val = 0;
|
|
|
|
// Push elements to the queue until stopped.
|
|
|
|
while (queue_.Push(val, kInfiniteTimeout).ok())
|
|
|
|
++val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SleepUntilQueueIsFull() {
|
|
|
|
const size_t kMaxNumLoopsWaiting = 1000;
|
|
|
|
const size_t kSleepDurationInMillisecondsPerLoop = 10;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < kMaxNumLoopsWaiting; i++) {
|
|
|
|
if (queue_.Size() >= kCapacity)
|
|
|
|
break;
|
2022-11-04 22:46:41 +00:00
|
|
|
std::this_thread::sleep_for(
|
|
|
|
std::chrono::milliseconds(kSleepDurationInMillisecondsPerLoop));
|
2014-04-11 22:01:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
std::unique_ptr<std::thread> thread_;
|
2014-04-11 22:01:13 +00:00
|
|
|
ProducerConsumerQueue<size_t> queue_;
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(MultiThreadProducerConsumerQueueTest);
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(MultiThreadProducerConsumerQueueTest, Pop) {
|
|
|
|
// Perform a number of pops.
|
|
|
|
size_t val;
|
|
|
|
size_t i = 0;
|
|
|
|
for (; i < kCapacity * 3; ++i) {
|
|
|
|
ASSERT_OK(queue_.Pop(&val, kInfiniteTimeout));
|
|
|
|
EXPECT_EQ(i, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until the queue is full. The size of the queue should be kCapacity
|
|
|
|
// exactly.
|
|
|
|
SleepUntilQueueIsFull();
|
|
|
|
EXPECT_EQ(kCapacity, queue_.Size());
|
|
|
|
|
|
|
|
queue_.Stop();
|
|
|
|
|
|
|
|
// Should still have kCapacity elements before STOPPED being returned.
|
|
|
|
for (size_t j = 0; j < kCapacity; ++j) {
|
|
|
|
ASSERT_OK(queue_.Pop(&val, kInfiniteTimeout));
|
|
|
|
EXPECT_EQ(i + j, val);
|
|
|
|
}
|
|
|
|
ASSERT_EQ(error::STOPPED, queue_.Pop(&val, kInfiniteTimeout).error_code());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MultiThreadProducerConsumerQueueTest, Peek) {
|
|
|
|
const size_t kPositionOne = 25u;
|
|
|
|
const size_t kPositionTwo = 88u;
|
|
|
|
|
|
|
|
EXPECT_EQ(0u, queue_.HeadPos());
|
|
|
|
|
|
|
|
size_t val;
|
|
|
|
ASSERT_OK(queue_.Peek(kPositionOne, &val, kInfiniteTimeout));
|
|
|
|
EXPECT_EQ(kPositionOne, val);
|
|
|
|
EXPECT_EQ(kPositionOne - kCapacity / 2, queue_.HeadPos());
|
|
|
|
|
|
|
|
ASSERT_OK(queue_.Peek(kPositionTwo, &val, kInfiniteTimeout));
|
|
|
|
EXPECT_EQ(kPositionTwo, val);
|
|
|
|
EXPECT_EQ(kPositionTwo - kCapacity / 2, queue_.HeadPos());
|
|
|
|
|
|
|
|
// Wait until the queue is full. The size of the queue should be kCapacity
|
|
|
|
// exactly.
|
|
|
|
SleepUntilQueueIsFull();
|
|
|
|
EXPECT_EQ(kCapacity, queue_.Size());
|
|
|
|
|
|
|
|
queue_.Stop();
|
|
|
|
EXPECT_EQ(kPositionTwo - kCapacity / 2, queue_.HeadPos());
|
|
|
|
EXPECT_EQ(kPositionTwo + kCapacity / 2 - 1, queue_.TailPos());
|
|
|
|
|
|
|
|
ASSERT_EQ(error::STOPPED,
|
|
|
|
queue_.Peek(kPositionTwo + kCapacity, &val, kInfiniteTimeout)
|
|
|
|
.error_code());
|
|
|
|
// Head will be moved pass Tail and the queue is expected to be empty.
|
|
|
|
EXPECT_EQ(kPositionTwo + kCapacity / 2, queue_.HeadPos());
|
|
|
|
EXPECT_EQ(kPositionTwo + kCapacity / 2 - 1, queue_.TailPos());
|
|
|
|
EXPECT_TRUE(queue_.Empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MultiThreadProducerConsumerQueueTest, PeekOnLargePosition) {
|
|
|
|
const size_t kVeryLargePosition = 88888888u;
|
|
|
|
|
|
|
|
size_t val;
|
|
|
|
ASSERT_EQ(error::TIME_OUT,
|
|
|
|
queue_.Peek(kVeryLargePosition, &val, 0).error_code());
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
auto start = std::chrono::steady_clock::now();
|
2014-04-11 22:01:13 +00:00
|
|
|
ASSERT_EQ(error::TIME_OUT,
|
|
|
|
queue_.Peek(kVeryLargePosition, &val, kTimeout).error_code());
|
2022-11-04 22:46:41 +00:00
|
|
|
auto elapsed = std::chrono::steady_clock::now() - start;
|
|
|
|
EXPECT_GE(elapsed, kTimeoutDuration);
|
2014-04-11 22:01:13 +00:00
|
|
|
|
|
|
|
queue_.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Operation {
|
|
|
|
kPush,
|
|
|
|
kPop,
|
|
|
|
kPeek,
|
|
|
|
};
|
|
|
|
|
|
|
|
class MultiThreadProducerConsumerQueueStopTest
|
|
|
|
: public ::testing::TestWithParam<Operation> {
|
|
|
|
public:
|
2022-11-04 22:46:41 +00:00
|
|
|
MultiThreadProducerConsumerQueueStopTest() : queue_(1) {}
|
2014-04-11 22:01:13 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
void ClosureTask(Operation op) {
|
|
|
|
int val = 0;
|
|
|
|
switch (op) {
|
|
|
|
case kPush:
|
2014-04-21 20:56:48 +00:00
|
|
|
// The queue was setup with size 1. The first push will return STOPPED
|
|
|
|
// if Stop() has been called; otherwise it should return OK and the
|
|
|
|
// second push will block until Stop() being called.
|
2014-04-11 22:01:13 +00:00
|
|
|
status_ = queue_.Push(0, kInfiniteTimeout);
|
2014-04-21 20:56:48 +00:00
|
|
|
if (status_.ok())
|
|
|
|
status_ = queue_.Push(0, kInfiniteTimeout);
|
2014-04-11 22:01:13 +00:00
|
|
|
break;
|
|
|
|
case kPop:
|
|
|
|
status_ = queue_.Pop(&val, kInfiniteTimeout);
|
|
|
|
break;
|
|
|
|
case kPeek:
|
|
|
|
|
|
|
|
status_ = queue_.Peek(0, &val, kInfiniteTimeout);
|
|
|
|
break;
|
|
|
|
default:
|
2022-11-04 22:46:41 +00:00
|
|
|
NOTIMPLEMENTED() << "Unknown test op: " << op;
|
|
|
|
break;
|
2014-04-11 22:01:13 +00:00
|
|
|
}
|
2022-11-04 22:46:41 +00:00
|
|
|
event_.Notify();
|
2014-04-11 22:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
ProducerConsumerQueue<int> queue_;
|
2022-11-04 22:46:41 +00:00
|
|
|
absl::Notification event_;
|
2014-04-11 22:01:13 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Status status_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(MultiThreadProducerConsumerQueueStopTest);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Verify that Stop stops Push/Pop/Peek operations and return immediately.
|
|
|
|
TEST_P(MultiThreadProducerConsumerQueueStopTest, StopTests) {
|
|
|
|
Operation op = GetParam();
|
2022-11-04 22:46:41 +00:00
|
|
|
std::thread thread(std::bind(
|
|
|
|
&MultiThreadProducerConsumerQueueStopTest::ClosureTask, this, op));
|
|
|
|
|
|
|
|
ASSERT_TRUE(!event_.HasBeenNotified());
|
2014-04-11 22:01:13 +00:00
|
|
|
queue_.Stop();
|
2022-11-04 22:46:41 +00:00
|
|
|
event_.WaitForNotification();
|
2014-04-11 22:01:13 +00:00
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
thread.join();
|
2014-04-11 22:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(Operations,
|
|
|
|
MultiThreadProducerConsumerQueueStopTest,
|
|
|
|
::testing::Values(kPush, kPop, kPeek));
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|