Index: runtime/bin/thread_pool_test.cc |
diff --git a/runtime/bin/thread_pool_test.cc b/runtime/bin/thread_pool_test.cc |
index a5712006008300443e00dcb0c413a9e45f4b48d9..df1bedb9f7aeb6aa674c9782d433a47049830038 100644 |
--- a/runtime/bin/thread_pool_test.cc |
+++ b/runtime/bin/thread_pool_test.cc |
@@ -3,6 +3,7 @@ |
// BSD-style license that can be found in the LICENSE file. |
#include "bin/thread_pool.h" |
+#include "bin/thread.h" |
#include "platform/assert.h" |
#include "platform/globals.h" |
#include "vm/unit_test.h" |
@@ -13,3 +14,74 @@ UNIT_TEST_CASE(ThreadPoolStartStop) { |
thread_pool.Start(); |
thread_pool.Shutdown(); |
} |
+ |
+ |
+static dart::Monitor* monitor = NULL; |
+static uint32_t task_count = 0; |
+static uint32_t task_sum = 0; |
+void TestTaskHandler(ThreadPool::Task args) { |
Mads Ager (google)
2012/01/24 12:29:39
Blank line before the function?
Søren Gjesse
2012/01/24 13:04:35
Done.
|
+ MonitorLocker ml(monitor); |
+ task_count++; |
+ task_sum += reinterpret_cast<int>(args); |
+} |
+ |
+ |
+UNIT_TEST_CASE(ThreadPoolTest1) { |
+ static const uint32_t kNumTestThreads = 10; |
+ static const uint32_t kNumTestTasks = 1000; |
+ static const uint32_t kNumLoops = 100; |
+ monitor = new dart::Monitor(); |
+ ThreadPool thread_pool(&TestTaskHandler, kNumTestThreads); |
+ uint32_t pool_start_count = 0; |
+ while (pool_start_count++ < kNumLoops) { |
+ task_count = 0; |
+ task_sum = 0; |
+ thread_pool.Start(); |
+ for (uint32_t i = 1; i <= kNumTestTasks; i++) { |
+ bool result = |
+ thread_pool.InsertTask(reinterpret_cast<ThreadPool::Task>(i)); |
+ EXPECT(result); |
+ } |
+ thread_pool.Shutdown(true); |
+ |
+ EXPECT_EQ(kNumTestTasks, task_count); |
+ EXPECT_EQ((1 + kNumTestTasks) * (kNumTestTasks / 2), task_sum); |
+ } |
+ |
+ delete monitor; |
+ monitor = NULL; |
+} |
+ |
+ |
+UNIT_TEST_CASE(ThreadPoolTest2) { |
+ static const uint32_t kNumTestThreads = 10; |
+ static const uint32_t kNumTestTasks = 50000; |
+ static const uint32_t kNumLoops = 100; |
+ monitor = new dart::Monitor(); |
+ ThreadPool thread_pool(&TestTaskHandler, kNumTestThreads); |
+ for (uint32_t i = 1; i <= kNumTestTasks; i++) { |
+ bool result = |
+ thread_pool.InsertTask(reinterpret_cast<ThreadPool::Task>(i)); |
+ EXPECT(result); |
+ } |
+ |
+ // Start and stop the thread pool without draining the queue a |
+ // number of times. |
+ uint32_t pool_start_count = 0; |
+ while (pool_start_count++ < kNumLoops) { |
+ printf("%d %d\n", task_count, task_sum); |
+ thread_pool.Start(); |
+ dart::OS::Sleep(1); |
+ thread_pool.Shutdown(false); |
+ } |
+ // Finally start and drain the queue to get all messages processed. |
+ thread_pool.Start(); |
+ thread_pool.Shutdown(true); |
+ |
+ // Check that all tasks where processed. |
+ EXPECT_EQ(kNumTestTasks, task_count); |
+ EXPECT_EQ((1 + kNumTestTasks) * (kNumTestTasks / 2), task_sum); |
+ |
+ delete monitor; |
+ monitor = NULL; |
+} |