| 5 | using namespace Star; |
| 6 | |
| 7 | TEST(WorkerPoolTest, All) { |
| 8 | int counter = 0; |
| 9 | Mutex counterMutex; |
| 10 | |
| 11 | auto incCounter = [&counter, &counterMutex]() { |
| 12 | Thread::sleep(100); |
| 13 | MutexLocker locker(counterMutex); |
| 14 | counter += 1; |
| 15 | }; |
| 16 | |
| 17 | Deque<WorkerPoolHandle> handles; |
| 18 | |
| 19 | WorkerPool workerPool("WorkerPoolTest"); |
| 20 | for (size_t i = 0; i < 10; ++i) |
| 21 | handles.append(workerPool.addWork(incCounter)); |
| 22 | |
| 23 | workerPool.start(10); |
| 24 | |
| 25 | for (size_t i = 0; i < 90; ++i) |
| 26 | handles.append(workerPool.addWork(incCounter)); |
| 27 | |
| 28 | while (handles.size() > 20) |
| 29 | handles.takeFirst().finish(); |
| 30 | |
| 31 | workerPool.finish(); |
| 32 | |
| 33 | EXPECT_EQ(counter, 100); |
| 34 | } |