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 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.
| |
23 MonitorLocker ml(monitor); | |
24 task_count++; | |
25 task_sum += reinterpret_cast<int>(args); | |
26 } | |
27 | |
28 | |
29 UNIT_TEST_CASE(ThreadPoolTest1) { | |
30 static const uint32_t kNumTestThreads = 10; | |
31 static const uint32_t kNumTestTasks = 1000; | |
32 static const uint32_t kNumLoops = 100; | |
33 monitor = new dart::Monitor(); | |
34 ThreadPool thread_pool(&TestTaskHandler, kNumTestThreads); | |
35 uint32_t pool_start_count = 0; | |
36 while (pool_start_count++ < kNumLoops) { | |
37 task_count = 0; | |
38 task_sum = 0; | |
39 thread_pool.Start(); | |
40 for (uint32_t i = 1; i <= kNumTestTasks; i++) { | |
41 bool result = | |
42 thread_pool.InsertTask(reinterpret_cast<ThreadPool::Task>(i)); | |
43 EXPECT(result); | |
44 } | |
45 thread_pool.Shutdown(true); | |
46 | |
47 EXPECT_EQ(kNumTestTasks, task_count); | |
48 EXPECT_EQ((1 + kNumTestTasks) * (kNumTestTasks / 2), task_sum); | |
49 } | |
50 | |
51 delete monitor; | |
52 monitor = NULL; | |
53 } | |
54 | |
55 | |
56 UNIT_TEST_CASE(ThreadPoolTest2) { | |
57 static const uint32_t kNumTestThreads = 10; | |
58 static const uint32_t kNumTestTasks = 50000; | |
59 static const uint32_t kNumLoops = 100; | |
60 monitor = new dart::Monitor(); | |
61 ThreadPool thread_pool(&TestTaskHandler, kNumTestThreads); | |
62 for (uint32_t i = 1; i <= kNumTestTasks; i++) { | |
63 bool result = | |
64 thread_pool.InsertTask(reinterpret_cast<ThreadPool::Task>(i)); | |
65 EXPECT(result); | |
66 } | |
67 | |
68 // Start and stop the thread pool without draining the queue a | |
69 // number of times. | |
70 uint32_t pool_start_count = 0; | |
71 while (pool_start_count++ < kNumLoops) { | |
72 printf("%d %d\n", task_count, task_sum); | |
73 thread_pool.Start(); | |
74 dart::OS::Sleep(1); | |
75 thread_pool.Shutdown(false); | |
76 } | |
77 // Finally start and drain the queue to get all messages processed. | |
78 thread_pool.Start(); | |
79 thread_pool.Shutdown(true); | |
80 | |
81 // Check that all tasks where processed. | |
82 EXPECT_EQ(kNumTestTasks, task_count); | |
83 EXPECT_EQ((1 + kNumTestTasks) * (kNumTestTasks / 2), task_sum); | |
84 | |
85 delete monitor; | |
86 monitor = NULL; | |
87 } | |
OLD | NEW |