Index: base/worker_pool_job_unittest.cc |
diff --git a/base/worker_pool_job_unittest.cc b/base/worker_pool_job_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..760a322dca109c942bb894725d78acdaadeff0b2 |
--- /dev/null |
+++ b/base/worker_pool_job_unittest.cc |
@@ -0,0 +1,100 @@ |
+// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
eroman
2011/01/05 01:24:52
nit: might consider changing these to 2011
|
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/worker_pool_job.h" |
+ |
+#include "base/callback.h" |
+#include "base/message_loop.h" |
+#include "base/waitable_event.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace base { |
+ |
+namespace { |
+ |
+class TestJob : public WorkerPoolJob { |
+ public: |
+ explicit TestJob() |
eroman
2011/01/05 01:24:52
is explicit needed?
|
+ : run_on_worker_pool_event_(true /* manual reset */, |
+ false /* not initially signalled */), |
+ have_run_(false), |
+ waiting_for_run_(false) {} |
+ |
+ void SignalOnWorkerPool() { |
+ run_on_worker_pool_event_.Signal(); |
+ } |
+ |
+ void WaitUntilComplete() { |
+ DCHECK(!waiting_for_run_); |
+ while (!have_run_) { |
+ waiting_for_run_ = true; |
+ MessageLoop::current()->Run(); |
+ waiting_for_run_ = false; |
+ } |
+ have_run_ = false; // auto-reset for next callback |
+ } |
+ |
+ using WorkerPoolJob::StartJob; |
+ using WorkerPoolJob::CancelJob; |
+ using WorkerPoolJob::canceled; |
+ using WorkerPoolJob::is_running; |
+ |
+ protected: |
+ ~TestJob() {} |
+ |
+ private: |
+ virtual void RunJob() { |
+ run_on_worker_pool_event_.Wait(); |
+ } |
+ |
+ virtual void OnJobCompleted() { |
+ if (waiting_for_run_) |
+ MessageLoop::current()->Quit(); |
+ have_run_ = true; |
+ } |
+ |
+ WaitableEvent run_on_worker_pool_event_; |
+ bool have_run_; |
+ bool waiting_for_run_; |
+}; |
+ |
+TEST(WorkerPoolJobTest, Simple) { |
+ MessageLoop loop; |
+ scoped_refptr<TestJob> job(new TestJob); |
+ job->StartJob(); |
+ EXPECT_TRUE(job->is_running()); |
+ job->SignalOnWorkerPool(); |
+ job->WaitUntilComplete(); |
+ EXPECT_FALSE(job->canceled()); |
+ EXPECT_FALSE(job->is_running()); |
+} |
+ |
+TEST(WorkerPoolJobTest, CancelOnWorker) { |
+ MessageLoop loop; |
+ scoped_refptr<TestJob> job(new TestJob); |
+ job->StartJob(); |
+ job->CancelJob(); |
+ job->SignalOnWorkerPool(); |
+ EXPECT_TRUE(job->canceled()); |
+ |
+ // Try to catch any broken behavior. |
+ MessageLoop::current()->RunAllPending(); |
+} |
+ |
+TEST(WorkerPoolJobTest, CancelOnOrigin) { |
+ MessageLoop loop; |
+ scoped_refptr<TestJob> job(new TestJob); |
+ job->StartJob(); |
+ EXPECT_TRUE(job->is_running()); |
+ job->SignalOnWorkerPool(); |
+ job->CancelJob(); |
+ EXPECT_TRUE(job->canceled()); |
+ |
+ // Try to catch any broken behavior. |
+ MessageLoop::current()->RunAllPending(); |
+} |
+ |
+} // namespace |
+ |
+} // namespace base |