Commit f5b86867 authored by jochen's avatar jochen Committed by Commit bot

Add test for posting a single task to the worker pool

Also, clarify comments about how semaphores work

BUG=none
R=mlippautz@chromium.org

Review-Url: https://codereview.chromium.org/2270703002
Cr-Commit-Position: refs/heads/master@{#38816}
parent 09a7ac5f
......@@ -39,14 +39,13 @@ class Semaphore final {
// Increments the semaphore counter.
void Signal();
// Suspends the calling thread until the semaphore counter is non zero
// and then decrements the semaphore counter.
// Decrements the semaphore counter if it is positive, or blocks until it
// becomes positive and then decrements the counter.
void Wait();
// Suspends the calling thread until the counter is non zero or the timeout
// time has passed. If timeout happens the return value is false and the
// counter is unchanged. Otherwise the semaphore counter is decremented and
// true is returned.
// Like Wait() but returns after rel_time time has passed. If the timeout
// happens the return value is false and the counter is unchanged. Otherwise
// the semaphore counter is decremented and true is returned.
bool WaitFor(const TimeDelta& rel_time) WARN_UNUSED_RESULT;
#if V8_OS_MACOSX
......
......@@ -5,6 +5,8 @@
#include "src/libplatform/task-queue.h"
#include "src/base/logging.h"
#include "src/base/platform/platform.h"
#include "src/base/platform/time.h"
namespace v8 {
namespace platform {
......@@ -53,5 +55,15 @@ void TaskQueue::Terminate() {
process_queue_semaphore_.Signal();
}
void TaskQueue::BlockUntilQueueEmptyForTesting() {
for (;;) {
{
base::LockGuard<base::Mutex> guard(&lock_);
if (task_queue_.empty()) return;
}
base::OS::Sleep(base::TimeDelta::FromMilliseconds(5));
}
}
} // namespace platform
} // namespace v8
......@@ -10,6 +10,7 @@
#include "src/base/macros.h"
#include "src/base/platform/mutex.h"
#include "src/base/platform/semaphore.h"
#include "testing/gtest/include/gtest/gtest_prod.h"
namespace v8 {
......@@ -33,6 +34,10 @@ class TaskQueue {
void Terminate();
private:
FRIEND_TEST(WorkerThreadTest, PostSingleTask);
void BlockUntilQueueEmptyForTesting();
base::Semaphore process_queue_semaphore_;
base::Mutex lock_;
std::queue<Task*> task_queue_;
......
......@@ -44,5 +44,21 @@ TEST(WorkerThreadTest, Basic) {
queue.Terminate();
}
TEST(WorkerThreadTest, PostSingleTask) {
TaskQueue queue;
WorkerThread thread1(&queue);
WorkerThread thread2(&queue);
InSequence s;
StrictMock<MockTask>* task = new StrictMock<MockTask>;
EXPECT_CALL(*task, Run());
EXPECT_CALL(*task, Die());
queue.Append(task);
// The next call should not time out.
queue.BlockUntilQueueEmptyForTesting();
queue.Terminate();
}
} // namespace platform
} // namespace v8
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment