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 { ...@@ -39,14 +39,13 @@ class Semaphore final {
// Increments the semaphore counter. // Increments the semaphore counter.
void Signal(); void Signal();
// Suspends the calling thread until the semaphore counter is non zero // Decrements the semaphore counter if it is positive, or blocks until it
// and then decrements the semaphore counter. // becomes positive and then decrements the counter.
void Wait(); void Wait();
// Suspends the calling thread until the counter is non zero or the timeout // Like Wait() but returns after rel_time time has passed. If the timeout
// time has passed. If timeout happens the return value is false and the // happens the return value is false and the counter is unchanged. Otherwise
// counter is unchanged. Otherwise the semaphore counter is decremented and // the semaphore counter is decremented and true is returned.
// true is returned.
bool WaitFor(const TimeDelta& rel_time) WARN_UNUSED_RESULT; bool WaitFor(const TimeDelta& rel_time) WARN_UNUSED_RESULT;
#if V8_OS_MACOSX #if V8_OS_MACOSX
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include "src/libplatform/task-queue.h" #include "src/libplatform/task-queue.h"
#include "src/base/logging.h" #include "src/base/logging.h"
#include "src/base/platform/platform.h"
#include "src/base/platform/time.h"
namespace v8 { namespace v8 {
namespace platform { namespace platform {
...@@ -53,5 +55,15 @@ void TaskQueue::Terminate() { ...@@ -53,5 +55,15 @@ void TaskQueue::Terminate() {
process_queue_semaphore_.Signal(); 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 platform
} // namespace v8 } // namespace v8
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "src/base/macros.h" #include "src/base/macros.h"
#include "src/base/platform/mutex.h" #include "src/base/platform/mutex.h"
#include "src/base/platform/semaphore.h" #include "src/base/platform/semaphore.h"
#include "testing/gtest/include/gtest/gtest_prod.h"
namespace v8 { namespace v8 {
...@@ -33,6 +34,10 @@ class TaskQueue { ...@@ -33,6 +34,10 @@ class TaskQueue {
void Terminate(); void Terminate();
private: private:
FRIEND_TEST(WorkerThreadTest, PostSingleTask);
void BlockUntilQueueEmptyForTesting();
base::Semaphore process_queue_semaphore_; base::Semaphore process_queue_semaphore_;
base::Mutex lock_; base::Mutex lock_;
std::queue<Task*> task_queue_; std::queue<Task*> task_queue_;
......
...@@ -44,5 +44,21 @@ TEST(WorkerThreadTest, Basic) { ...@@ -44,5 +44,21 @@ TEST(WorkerThreadTest, Basic) {
queue.Terminate(); 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 platform
} // namespace v8 } // 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