Skip to content

Commit

Permalink
Use task queue factory factory as parameter for TaskQueueTest
Browse files Browse the repository at this point in the history
The new parameter simplify TaskQueueFactory lifetime and
allows insertion of per TestCase state

Bug: webrtc:10191
Change-Id: If4948df2756580957052b2b333b5c12cf4914d55
Reviewed-on: https://webrtc-review.googlesource.com/c/121648
Reviewed-by: Karl Wiberg <[email protected]>
Commit-Queue: Danil Chapovalov <[email protected]>
Cr-Commit-Position: refs/heads/master@{#26583}
  • Loading branch information
DanilChapovalov authored and Commit Bot committed Feb 7, 2019
1 parent 0041fe5 commit 710f3d3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
39 changes: 25 additions & 14 deletions api/task_queue/task_queue_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@ namespace webrtc {
namespace {

std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue(
TaskQueueFactory* factory,
const std::unique_ptr<webrtc::TaskQueueFactory>& factory,
absl::string_view task_queue_name,
TaskQueueFactory::Priority priority = TaskQueueFactory::Priority::NORMAL) {
return factory->CreateTaskQueue(task_queue_name, priority);
}

TEST_P(TaskQueueTest, Construct) {
auto queue = CreateTaskQueue(GetParam(), "Construct");
std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
auto queue = CreateTaskQueue(factory, "Construct");
EXPECT_FALSE(queue->IsCurrent());
}

TEST_P(TaskQueueTest, PostAndCheckCurrent) {
std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
rtc::Event event;
auto queue = CreateTaskQueue(GetParam(), "PostAndCheckCurrent");
auto queue = CreateTaskQueue(factory, "PostAndCheckCurrent");

// We're not running a task, so there shouldn't be a current queue.
EXPECT_FALSE(queue->IsCurrent());
Expand All @@ -46,8 +48,9 @@ TEST_P(TaskQueueTest, PostAndCheckCurrent) {
}

TEST_P(TaskQueueTest, PostCustomTask) {
std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
rtc::Event ran;
auto queue = CreateTaskQueue(GetParam(), "PostCustomImplementation");
auto queue = CreateTaskQueue(factory, "PostCustomImplementation");

class CustomTask : public QueuedTask {
public:
Expand All @@ -67,16 +70,18 @@ TEST_P(TaskQueueTest, PostCustomTask) {
}

TEST_P(TaskQueueTest, PostDelayedZero) {
std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
rtc::Event event;
auto queue = CreateTaskQueue(GetParam(), "PostDelayedZero");
auto queue = CreateTaskQueue(factory, "PostDelayedZero");

queue->PostDelayedTask(rtc::NewClosure([&event] { event.Set(); }), 0);
EXPECT_TRUE(event.Wait(1000));
}

TEST_P(TaskQueueTest, PostFromQueue) {
std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
rtc::Event event;
auto queue = CreateTaskQueue(GetParam(), "PostFromQueue");
auto queue = CreateTaskQueue(factory, "PostFromQueue");

queue->PostTask(rtc::NewClosure([&event, &queue] {
queue->PostTask(rtc::NewClosure([&event] { event.Set(); }));
Expand All @@ -85,9 +90,10 @@ TEST_P(TaskQueueTest, PostFromQueue) {
}

TEST_P(TaskQueueTest, PostDelayed) {
std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
rtc::Event event;
auto queue = CreateTaskQueue(GetParam(), "PostDelayed",
TaskQueueFactory::Priority::HIGH);
auto queue =
CreateTaskQueue(factory, "PostDelayed", TaskQueueFactory::Priority::HIGH);

int64_t start = rtc::TimeMillis();
queue->PostDelayedTask(rtc::NewClosure([&event, &queue] {
Expand All @@ -105,7 +111,8 @@ TEST_P(TaskQueueTest, PostDelayed) {
}

TEST_P(TaskQueueTest, PostMultipleDelayed) {
auto queue = CreateTaskQueue(GetParam(), "PostMultipleDelayed");
std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
auto queue = CreateTaskQueue(factory, "PostMultipleDelayed");

std::vector<rtc::Event> events(100);
for (int i = 0; i < 100; ++i) {
Expand All @@ -122,9 +129,10 @@ TEST_P(TaskQueueTest, PostMultipleDelayed) {
}

TEST_P(TaskQueueTest, PostDelayedAfterDestruct) {
std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
rtc::Event run;
rtc::Event deleted;
auto queue = CreateTaskQueue(GetParam(), "PostDelayedAfterDestruct");
auto queue = CreateTaskQueue(factory, "PostDelayedAfterDestruct");
queue->PostDelayedTask(
rtc::NewClosure([&run] { run.Set(); }, [&deleted] { deleted.Set(); }),
100);
Expand All @@ -136,9 +144,10 @@ TEST_P(TaskQueueTest, PostDelayedAfterDestruct) {
}

TEST_P(TaskQueueTest, PostAndReuse) {
std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
rtc::Event event;
auto post_queue = CreateTaskQueue(GetParam(), "PostQueue");
auto reply_queue = CreateTaskQueue(GetParam(), "ReplyQueue");
auto post_queue = CreateTaskQueue(factory, "PostQueue");
auto reply_queue = CreateTaskQueue(factory, "ReplyQueue");

int call_count = 0;

Expand Down Expand Up @@ -181,6 +190,7 @@ TEST_P(TaskQueueTest, PostAndReuse) {
// Tests posting more messages than a queue can queue up.
// In situations like that, tasks will get dropped.
TEST_P(TaskQueueTest, PostALot) {
std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
// To destruct the event after the queue has gone out of scope.
rtc::Event event;

Expand All @@ -189,7 +199,7 @@ TEST_P(TaskQueueTest, PostALot) {
static const int kTaskCount = 0xffff;

{
auto queue = CreateTaskQueue(GetParam(), "PostALot");
auto queue = CreateTaskQueue(factory, "PostALot");

// On linux, the limit of pending bytes in the pipe buffer is 0xffff.
// So here we post a total of 0xffff+1 messages, which triggers a failure
Expand Down Expand Up @@ -219,12 +229,13 @@ TEST_P(TaskQueueTest, PostALot) {
// unit test, run it under TSan or some other tool that is able to
// directly detect data races.
TEST_P(TaskQueueTest, PostTwoWithSharedUnprotectedState) {
std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
struct SharedState {
// First task will set this value to 1 and second will assert it.
int state = 0;
} state;

auto queue = CreateTaskQueue(GetParam(), "PostTwoWithSharedUnprotectedState");
auto queue = CreateTaskQueue(factory, "PostTwoWithSharedUnprotectedState");
rtc::Event done;
queue->PostTask(rtc::NewClosure([&state, &queue, &done] {
// Post tasks from queue to guarantee, that 1st task won't be
Expand Down
14 changes: 11 additions & 3 deletions api/task_queue/task_queue_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#ifndef API_TASK_QUEUE_TASK_QUEUE_TEST_H_
#define API_TASK_QUEUE_TASK_QUEUE_TEST_H_

#include <functional>
#include <memory>

#include "api/task_queue/task_queue_factory.h"
#include "test/gtest.h"

Expand All @@ -20,12 +23,17 @@ namespace webrtc {
//
// namespace {
//
// using ::testing::Values;
// using ::webrtc::TaskQueueTest;
// webrtc::TaskQueueFactory* MyFactory();
// INSTANTIATE_TEST_SUITE_P(My, TaskQueueTest, ::testing::Values(MyFactory()));
//
// std::unique_ptr<webrtc::TaskQueueFactory> CreateMyFactory();
//
// INSTANTIATE_TEST_SUITE_P(My, TaskQueueTest, Values(CreateMyFactory));
//
// } // namespace
class TaskQueueTest : public ::testing::TestWithParam<TaskQueueFactory*> {};
class TaskQueueTest : public ::testing::TestWithParam<
std::function<std::unique_ptr<TaskQueueFactory>()>> {
};

} // namespace webrtc

Expand Down

0 comments on commit 710f3d3

Please sign in to comment.