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 "vm/os.h" | 5 #include "vm/os.h" |
6 #include "vm/lockers.h" | 6 #include "vm/lockers.h" |
7 #include "vm/thread_pool.h" | 7 #include "vm/thread_pool.h" |
8 #include "vm/unit_test.h" | 8 #include "vm/unit_test.h" |
9 | 9 |
10 namespace dart { | 10 namespace dart { |
11 | 11 |
12 DECLARE_FLAG(int, worker_timeout_millis); | 12 DECLARE_FLAG(int, worker_timeout_millis); |
13 | 13 |
14 | 14 |
15 class ThreadPoolTestPeer { | |
16 public: | |
17 // When the pool has an exit monitor, workers notify a monitor just | |
18 // before they exit. This is only used in tests to make sure that | |
19 // Shutdown works. | |
20 static void SetExitMonitor(Monitor* exit_monitor, int* exit_count) { | |
21 ThreadPool::exit_monitor_ = exit_monitor; | |
22 ThreadPool::exit_count_ = exit_count; | |
23 } | |
24 }; | |
25 | |
26 | |
27 UNIT_TEST_CASE(ThreadPool_Create) { | 15 UNIT_TEST_CASE(ThreadPool_Create) { |
28 ThreadPool thread_pool; | 16 ThreadPool thread_pool; |
29 } | 17 } |
30 | 18 |
31 | 19 |
32 class TestTask : public ThreadPool::Task { | 20 class TestTask : public ThreadPool::Task { |
33 public: | 21 public: |
34 TestTask(Monitor* sync, bool* done) | 22 TestTask(Monitor* sync, bool* done) |
35 : sync_(sync), done_(done) { | 23 : sync_(sync), done_(done) { |
36 } | 24 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 | 82 |
95 virtual void Run() { | 83 virtual void Run() { |
96 OS::Sleep(millis_); | 84 OS::Sleep(millis_); |
97 } | 85 } |
98 | 86 |
99 private: | 87 private: |
100 int millis_; | 88 int millis_; |
101 }; | 89 }; |
102 | 90 |
103 | 91 |
104 UNIT_TEST_CASE(ThreadPool_WorkerShutdown) { | |
105 Monitor exit_sync; | |
106 int exit_count = 0; | |
107 MonitorLocker ml(&exit_sync); | |
108 | |
109 // Set up the ThreadPool so that workers notify before they exit. | |
110 ThreadPool* thread_pool = new ThreadPool(); | |
111 ThreadPoolTestPeer::SetExitMonitor(&exit_sync, &exit_count); | |
112 | |
113 // Run a single task. | |
114 thread_pool->Run(new SleepTask(2)); | |
115 | |
116 // Kill the thread pool. | |
117 delete thread_pool; | |
118 thread_pool = NULL; | |
119 | |
120 // Wait for the workers to terminate. | |
121 while (exit_count == 0) { | |
122 ml.Wait(); | |
123 } | |
124 EXPECT_EQ(1, exit_count); | |
125 } | |
126 | |
127 | |
128 UNIT_TEST_CASE(ThreadPool_WorkerTimeout) { | 92 UNIT_TEST_CASE(ThreadPool_WorkerTimeout) { |
129 // Adjust the worker timeout so that we timeout quickly. | 93 // Adjust the worker timeout so that we timeout quickly. |
130 int saved_timeout = FLAG_worker_timeout_millis; | 94 int saved_timeout = FLAG_worker_timeout_millis; |
131 FLAG_worker_timeout_millis = 1; | 95 FLAG_worker_timeout_millis = 1; |
132 | 96 |
133 ThreadPool thread_pool; | 97 ThreadPool thread_pool; |
134 EXPECT_EQ(0U, thread_pool.workers_started()); | 98 EXPECT_EQ(0U, thread_pool.workers_started()); |
135 EXPECT_EQ(0U, thread_pool.workers_stopped()); | 99 EXPECT_EQ(0U, thread_pool.workers_stopped()); |
136 | 100 |
137 // Run a worker. | 101 // Run a worker. |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 MonitorLocker ml(&sync); | 173 MonitorLocker ml(&sync); |
210 while (done < kTotalTasks) { | 174 while (done < kTotalTasks) { |
211 ml.Wait(); | 175 ml.Wait(); |
212 } | 176 } |
213 } | 177 } |
214 EXPECT_EQ(kTotalTasks, done); | 178 EXPECT_EQ(kTotalTasks, done); |
215 } | 179 } |
216 | 180 |
217 | 181 |
218 } // namespace dart | 182 } // namespace dart |
OLD | NEW |