2014-04-11 22:01:13 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2014-08-21 22:40:44 +00:00
|
|
|
#include <gtest/gtest.h>
|
2014-04-11 22:01:13 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/bind.h"
|
|
|
|
#include "packager/base/synchronization/waitable_event.h"
|
|
|
|
#include "packager/media/base/closure_thread.h"
|
|
|
|
#include "packager/media/base/producer_consumer_queue.h"
|
2014-10-09 23:33:56 +00:00
|
|
|
#include "packager/media/base/test/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.
|
2014-04-11 22:01:13 +00:00
|
|
|
|
2015-10-01 16:47:56 +00:00
|
|
|
// Verify that the |delta| is approximately |expected_time_in_milliseconds|.
|
|
|
|
void ExpectTimeApproxEqual(int64_t expected_time_in_milliseconds,
|
|
|
|
const base::TimeDelta& delta) {
|
|
|
|
const int64_t kOverhead = 50; // 0.05s.
|
|
|
|
EXPECT_NEAR(delta.InMilliseconds(), expected_time_in_milliseconds, kOverhead);
|
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-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<base::ElapsedTimer> timer;
|
2014-04-11 22:01:13 +00:00
|
|
|
ProducerConsumerQueue<int> queue(kCapacity);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < kCapacity; ++i) {
|
|
|
|
timer.reset(new base::ElapsedTimer());
|
|
|
|
ASSERT_OK(queue.Push(i, kTimeout));
|
|
|
|
// Expect Push to return instantly without waiting.
|
2015-10-01 16:47:56 +00:00
|
|
|
ExpectTimeApproxEqual(0, timer->Elapsed());
|
2014-04-11 22:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
timer.reset(new base::ElapsedTimer());
|
|
|
|
ASSERT_EQ(error::TIME_OUT, queue.Push(0, kTimeout).error_code());
|
|
|
|
// Expect elapsed time exceeds defined timeout.
|
2015-10-01 16:47:56 +00:00
|
|
|
ExpectTimeApproxEqual(kTimeout, timer->Elapsed());
|
2014-04-11 22:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ProducerConsumerQueueTest, PopWithTimeout) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<base::ElapsedTimer> timer;
|
2014-04-11 22:01:13 +00:00
|
|
|
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) {
|
|
|
|
timer.reset(new base::ElapsedTimer());
|
|
|
|
ASSERT_OK(queue.Pop(&val, kTimeout));
|
|
|
|
// Expect Pop to return instantly without waiting.
|
2015-10-01 16:47:56 +00:00
|
|
|
ExpectTimeApproxEqual(0, timer->Elapsed());
|
2014-04-11 22:01:13 +00:00
|
|
|
EXPECT_EQ(i, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
timer.reset(new base::ElapsedTimer());
|
|
|
|
ASSERT_EQ(error::TIME_OUT, queue.Pop(&val, kTimeout).error_code());
|
|
|
|
// Expect elapsed time exceeds defined timeout.
|
2015-10-01 16:47:56 +00:00
|
|
|
ExpectTimeApproxEqual(kTimeout, timer->Elapsed());
|
2014-04-11 22:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ProducerConsumerQueueTest, PeekWithTimeout) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<base::ElapsedTimer> timer;
|
2014-04-11 22:01:13 +00:00
|
|
|
ProducerConsumerQueue<size_t> queue(kCapacity);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < kCapacity; ++i)
|
|
|
|
ASSERT_OK(queue.Push(i, kInfiniteTimeout));
|
|
|
|
|
|
|
|
size_t val;
|
|
|
|
timer.reset(new base::ElapsedTimer());
|
|
|
|
ASSERT_EQ(error::TIME_OUT,
|
|
|
|
queue.Peek(kCapacity, &val, kTimeout).error_code());
|
|
|
|
// Expect elapsed time exceeds defined timeout.
|
2015-10-01 16:47:56 +00:00
|
|
|
ExpectTimeApproxEqual(kTimeout, timer->Elapsed());
|
2014-04-11 22:01:13 +00:00
|
|
|
|
|
|
|
for (size_t i = kCapacity / 2; i < kCapacity; ++i) {
|
|
|
|
timer.reset(new base::ElapsedTimer());
|
|
|
|
ASSERT_OK(queue.Peek(i, &val, kTimeout));
|
|
|
|
// Expect Peek to return instantly without waiting.
|
2015-10-01 16:47:56 +00:00
|
|
|
ExpectTimeApproxEqual(0, timer->Elapsed());
|
2014-04-11 22:01:13 +00:00
|
|
|
EXPECT_EQ(i, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ProducerConsumerQueueTest, CheckStop) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<base::ElapsedTimer> timer;
|
2014-04-11 22:01:13 +00:00
|
|
|
ProducerConsumerQueue<int> queue(kUnlimitedCapacity);
|
|
|
|
|
|
|
|
ASSERT_FALSE(queue.Stopped());
|
|
|
|
queue.Stop();
|
|
|
|
ASSERT_TRUE(queue.Stopped());
|
|
|
|
|
|
|
|
EXPECT_EQ(error::STOPPED, queue.Push(0, kInfiniteTimeout).error_code());
|
|
|
|
|
|
|
|
timer.reset(new base::ElapsedTimer());
|
|
|
|
EXPECT_EQ(error::STOPPED, queue.Push(0, kTimeout).error_code());
|
|
|
|
// Expect Push to return instantly without waiting.
|
2015-10-01 16:47:56 +00:00
|
|
|
ExpectTimeApproxEqual(0, timer->Elapsed());
|
2014-04-11 22:01:13 +00:00
|
|
|
|
|
|
|
int val;
|
|
|
|
EXPECT_EQ(error::STOPPED, queue.Pop(&val, kInfiniteTimeout).error_code());
|
|
|
|
timer.reset(new base::ElapsedTimer());
|
|
|
|
EXPECT_EQ(error::STOPPED, queue.Pop(&val, kTimeout).error_code());
|
|
|
|
// Expect Pop to return instantly without waiting.
|
2015-10-01 16:47:56 +00:00
|
|
|
ExpectTimeApproxEqual(0, timer->Elapsed());
|
2014-04-11 22:01:13 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(error::STOPPED, queue.Peek(0, &val, kInfiniteTimeout).error_code());
|
|
|
|
timer.reset(new base::ElapsedTimer());
|
|
|
|
EXPECT_EQ(error::STOPPED, queue.Peek(0, &val, kTimeout).error_code());
|
|
|
|
// Expect Pop to return instantly without waiting.
|
2015-10-01 16:47:56 +00:00
|
|
|
ExpectTimeApproxEqual(0, timer->Elapsed());
|
2014-04-11 22:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class MultiThreadProducerConsumerQueueTest : public ::testing::Test {
|
|
|
|
public:
|
|
|
|
MultiThreadProducerConsumerQueueTest()
|
|
|
|
: thread_("My Push Thread",
|
|
|
|
base::Bind(&MultiThreadProducerConsumerQueueTest::PushTask,
|
|
|
|
base::Unretained(this))),
|
|
|
|
queue_(kCapacity) {}
|
2015-07-22 23:40:45 +00:00
|
|
|
~MultiThreadProducerConsumerQueueTest() override {}
|
2014-04-11 22:01:13 +00:00
|
|
|
|
|
|
|
protected:
|
2015-07-22 23:40:45 +00:00
|
|
|
void SetUp() override { thread_.Start(); }
|
|
|
|
void TearDown() override { thread_.Join(); }
|
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;
|
|
|
|
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
|
|
|
|
kSleepDurationInMillisecondsPerLoop));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ClosureThread thread_;
|
|
|
|
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) {
|
|
|
|
base::ElapsedTimer timer;
|
|
|
|
const size_t kVeryLargePosition = 88888888u;
|
|
|
|
|
|
|
|
size_t val;
|
|
|
|
ASSERT_EQ(error::TIME_OUT,
|
|
|
|
queue_.Peek(kVeryLargePosition, &val, 0).error_code());
|
2015-10-01 16:47:56 +00:00
|
|
|
ExpectTimeApproxEqual(0, timer.Elapsed());
|
2014-04-11 22:01:13 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(error::TIME_OUT,
|
|
|
|
queue_.Peek(kVeryLargePosition, &val, kTimeout).error_code());
|
2015-10-01 16:47:56 +00:00
|
|
|
ExpectTimeApproxEqual(kTimeout, timer.Elapsed());
|
2014-04-11 22:01:13 +00:00
|
|
|
|
|
|
|
queue_.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
enum Operation {
|
|
|
|
kPush,
|
|
|
|
kPop,
|
|
|
|
kPeek,
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
class MultiThreadProducerConsumerQueueStopTest
|
|
|
|
: public ::testing::TestWithParam<Operation> {
|
|
|
|
public:
|
2016-08-17 21:42:23 +00:00
|
|
|
MultiThreadProducerConsumerQueueStopTest()
|
|
|
|
: queue_(1),
|
|
|
|
event_(base::WaitableEvent::ResetPolicy::MANUAL,
|
|
|
|
base::WaitableEvent::InitialState::NOT_SIGNALED) {}
|
2015-07-22 23:40:45 +00:00
|
|
|
~MultiThreadProducerConsumerQueueStopTest() override {}
|
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:
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
event_.Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
ProducerConsumerQueue<int> queue_;
|
|
|
|
base::WaitableEvent event_;
|
|
|
|
|
|
|
|
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();
|
|
|
|
ClosureThread thread(
|
|
|
|
"My Thread",
|
|
|
|
base::Bind(&MultiThreadProducerConsumerQueueStopTest::ClosureTask,
|
|
|
|
base::Unretained(this),
|
|
|
|
op));
|
|
|
|
thread.Start();
|
|
|
|
|
|
|
|
base::ElapsedTimer timer;
|
|
|
|
ASSERT_TRUE(!event_.IsSignaled());
|
|
|
|
queue_.Stop();
|
|
|
|
event_.Wait();
|
|
|
|
// Expect Stop to stop the operations immediately.
|
2015-10-01 16:47:56 +00:00
|
|
|
ExpectTimeApproxEqual(0, timer.Elapsed());
|
2014-04-11 22:01:13 +00:00
|
|
|
|
|
|
|
thread.Join();
|
|
|
|
}
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(Operations,
|
|
|
|
MultiThreadProducerConsumerQueueStopTest,
|
|
|
|
::testing::Values(kPush, kPop, kPeek));
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|