Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(428)

Side by Side Diff: runtime/vm/thread_pool_test.cc

Issue 2478243004: Fix thread pool tests (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 UNIT_TEST_CASE(ThreadPool_Create) { 15 UNIT_TEST_CASE(ThreadPool_Create) {
16 ThreadPool thread_pool; 16 ThreadPool thread_pool;
17 } 17 }
18 18
19 19
20 class TestTask : public ThreadPool::Task { 20 class TestTask : public ThreadPool::Task {
21 public: 21 public:
22 TestTask(Monitor* sync, bool* done) 22 TestTask(Monitor* sync, bool* done)
23 : sync_(sync), done_(done) { 23 : sync_(sync), done_(done) {
24 } 24 }
25 25
26 // Before running the task, *done_ should be true. This lets the caller
27 // ASSERT things knowing that the thread is still around. To unblock the
28 // thread, the caller should take the lock, set *done_ to false, and Notify()
29 // the monitor.
26 virtual void Run() { 30 virtual void Run() {
31 {
32 MonitorLocker ml(sync_);
33 while (*done_) {
34 ml.Wait();
35 }
36 }
27 MonitorLocker ml(sync_); 37 MonitorLocker ml(sync_);
28 *done_ = true; 38 *done_ = true;
29 ml.Notify(); 39 ml.Notify();
30 } 40 }
31 41
32 private: 42 private:
33 Monitor* sync_; 43 Monitor* sync_;
34 bool* done_; 44 bool* done_;
35 }; 45 };
36 46
37 47
38 UNIT_TEST_CASE(ThreadPool_RunOne) { 48 UNIT_TEST_CASE(ThreadPool_RunOne) {
39 ThreadPool thread_pool; 49 ThreadPool thread_pool;
40 Monitor sync; 50 Monitor sync;
41 bool done = false; 51 bool done = true;
42 thread_pool.Run(new TestTask(&sync, &done)); 52 thread_pool.Run(new TestTask(&sync, &done));
43 { 53 {
44 MonitorLocker ml(&sync); 54 MonitorLocker ml(&sync);
55 done = false;
56 ml.Notify();
45 while (!done) { 57 while (!done) {
46 ml.Wait(); 58 ml.Wait();
47 } 59 }
48 } 60 }
49 EXPECT(done); 61 EXPECT(done);
50 62
51 // Do a sanity test on the worker stats. 63 // Do a sanity test on the worker stats.
52 EXPECT_EQ(1U, thread_pool.workers_started()); 64 EXPECT_EQ(1U, thread_pool.workers_started());
53 EXPECT_EQ(0U, thread_pool.workers_stopped()); 65 EXPECT_EQ(0U, thread_pool.workers_stopped());
54 } 66 }
55 67
56 68
57 UNIT_TEST_CASE(ThreadPool_RunMany) { 69 UNIT_TEST_CASE(ThreadPool_RunMany) {
58 const int kTaskCount = 100; 70 const int kTaskCount = 100;
59 ThreadPool thread_pool; 71 ThreadPool thread_pool;
60 Monitor sync[kTaskCount]; 72 Monitor sync[kTaskCount];
61 bool done[kTaskCount]; 73 bool done[kTaskCount];
62 74
63 for (int i = 0; i < kTaskCount; i++) { 75 for (int i = 0; i < kTaskCount; i++) {
64 done[i] = false; 76 done[i] = true;
65 thread_pool.Run(new TestTask(&sync[i], &done[i])); 77 thread_pool.Run(new TestTask(&sync[i], &done[i]));
66 } 78 }
67 for (int i = 0; i < kTaskCount; i++) { 79 for (int i = 0; i < kTaskCount; i++) {
68 MonitorLocker ml(&sync[i]); 80 MonitorLocker ml(&sync[i]);
81 done[i] = false;
82 ml.Notify();
69 while (!done[i]) { 83 while (!done[i]) {
70 ml.Wait(); 84 ml.Wait();
71 } 85 }
72 EXPECT(done[i]); 86 EXPECT(done[i]);
73 } 87 }
74 } 88 }
75 89
76 90
77 class SleepTask : public ThreadPool::Task { 91 class SleepTask : public ThreadPool::Task {
78 public: 92 public:
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 // Adjust the worker timeout so that we timeout quickly. 164 // Adjust the worker timeout so that we timeout quickly.
151 int saved_timeout = FLAG_worker_timeout_millis; 165 int saved_timeout = FLAG_worker_timeout_millis;
152 FLAG_worker_timeout_millis = 1; 166 FLAG_worker_timeout_millis = 1;
153 167
154 ThreadPool thread_pool; 168 ThreadPool thread_pool;
155 EXPECT_EQ(0U, thread_pool.workers_started()); 169 EXPECT_EQ(0U, thread_pool.workers_started());
156 EXPECT_EQ(0U, thread_pool.workers_stopped()); 170 EXPECT_EQ(0U, thread_pool.workers_stopped());
157 171
158 // Run a worker. 172 // Run a worker.
159 Monitor sync; 173 Monitor sync;
160 bool done = false; 174 bool done = true;
161 thread_pool.Run(new TestTask(&sync, &done)); 175 thread_pool.Run(new TestTask(&sync, &done));
162 EXPECT_EQ(1U, thread_pool.workers_started()); 176 EXPECT_EQ(1U, thread_pool.workers_started());
163 EXPECT_EQ(0U, thread_pool.workers_stopped()); 177 EXPECT_EQ(0U, thread_pool.workers_stopped());
164 { 178 {
165 MonitorLocker ml(&sync); 179 MonitorLocker ml(&sync);
180 done = false;
181 ml.Notify();
166 while (!done) { 182 while (!done) {
167 ml.Wait(); 183 ml.Wait();
168 } 184 }
169 } 185 }
170 EXPECT(done); 186 EXPECT(done);
171 187
172 // Wait up to 5 seconds to see if a worker times out. 188 // Wait up to 5 seconds to see if a worker times out.
173 const int kMaxWait = 5000; 189 const int kMaxWait = 5000;
174 int waited = 0; 190 int waited = 0;
175 while (thread_pool.workers_stopped() == 0 && waited < kMaxWait) { 191 while (thread_pool.workers_stopped() == 0 && waited < kMaxWait) {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 { 244 {
229 MonitorLocker ml(&sync); 245 MonitorLocker ml(&sync);
230 while (done < kTotalTasks) { 246 while (done < kTotalTasks) {
231 ml.Wait(); 247 ml.Wait();
232 } 248 }
233 } 249 }
234 EXPECT_EQ(kTotalTasks, done); 250 EXPECT_EQ(kTotalTasks, done);
235 } 251 }
236 252
237 } // namespace dart 253 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698