OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "bin/thread_pool.h" | 5 #include "bin/thread_pool.h" |
6 #include "bin/thread.h" | |
6 #include "platform/assert.h" | 7 #include "platform/assert.h" |
7 #include "platform/globals.h" | 8 #include "platform/globals.h" |
8 #include "vm/unit_test.h" | 9 #include "vm/unit_test.h" |
9 | 10 |
10 | 11 |
11 UNIT_TEST_CASE(ThreadPoolStartStop) { | 12 UNIT_TEST_CASE(ThreadPoolStartStop) { |
12 ThreadPool thread_pool(NULL, 10); | 13 ThreadPool thread_pool(NULL, 10); |
13 thread_pool.Start(); | 14 thread_pool.Start(); |
14 thread_pool.Shutdown(); | 15 thread_pool.Shutdown(); |
15 } | 16 } |
17 | |
18 | |
19 static dart::Monitor* monitor = NULL; | |
20 static uint32_t task_count = 0; | |
21 static uint32_t task_sum = 0; | |
22 | |
23 | |
24 void TestTaskHandler(ThreadPool::Task args) { | |
25 MonitorLocker ml(monitor); | |
26 task_count++; | |
27 task_sum += reinterpret_cast<int>(args); | |
28 } | |
29 | |
30 | |
31 UNIT_TEST_CASE(ThreadPoolTest1) { | |
32 static const uint32_t kNumTestThreads = 10; | |
33 static const uint32_t kNumTestTasks = 1000; | |
34 static const uint32_t kNumLoops = 100; | |
35 monitor = new dart::Monitor(); | |
36 ThreadPool thread_pool(&TestTaskHandler, kNumTestThreads); | |
37 uint32_t pool_start_count = 0; | |
38 while (pool_start_count++ < kNumLoops) { | |
39 task_count = 0; | |
40 task_sum = 0; | |
41 thread_pool.Start(); | |
42 for (uint32_t i = 1; i <= kNumTestTasks; i++) { | |
43 bool result = | |
44 thread_pool.InsertTask(reinterpret_cast<ThreadPool::Task>(i)); | |
45 EXPECT(result); | |
46 } | |
47 thread_pool.Shutdown(ThreadPool::kDrain); | |
48 | |
49 EXPECT_EQ(kNumTestTasks, task_count); | |
50 EXPECT_EQ((1 + kNumTestTasks) * (kNumTestTasks / 2), task_sum); | |
51 } | |
52 | |
53 delete monitor; | |
54 monitor = NULL; | |
55 } | |
56 | |
57 | |
58 UNIT_TEST_CASE(ThreadPoolTest2) { | |
59 static const uint32_t kNumTestThreads = 10; | |
60 static const uint32_t kNumTestTasks = 50000; | |
61 static const uint32_t kNumLoops = 100; | |
62 monitor = new dart::Monitor(); | |
63 ThreadPool thread_pool(&TestTaskHandler, kNumTestThreads); | |
64 for (uint32_t i = 1; i <= kNumTestTasks; i++) { | |
65 bool result = | |
66 thread_pool.InsertTask(reinterpret_cast<ThreadPool::Task>(i)); | |
67 EXPECT(result); | |
68 } | |
69 | |
70 // Start and stop the thread pool without draining the queue a | |
71 // number of times. | |
72 uint32_t pool_start_count = 0; | |
73 while (pool_start_count++ < kNumLoops) { | |
74 printf("%d %d\n", task_count, task_sum); | |
Mads Ager (google)
2012/01/24 13:44:27
Remove debug printing?
Søren Gjesse
2012/01/24 14:38:50
Done.
| |
75 thread_pool.Start(); | |
76 dart::OS::Sleep(1); | |
77 thread_pool.Shutdown(ThreadPool::kDoNotDrain); | |
78 } | |
79 // Finally start and drain the queue to get all messages processed. | |
80 thread_pool.Start(); | |
81 thread_pool.Shutdown(ThreadPool::kDrain); | |
82 | |
83 // Check that all tasks where processed. | |
84 EXPECT_EQ(kNumTestTasks, task_count); | |
85 EXPECT_EQ((1 + kNumTestTasks) * (kNumTestTasks / 2), task_sum); | |
86 | |
87 delete monitor; | |
88 monitor = NULL; | |
89 } | |
OLD | NEW |