]>
Commit | Line | Data |
---|---|---|
11fdf7f2 | 1 | /* |
7c673cae FG |
2 | * Copyright (c) 2016-present, Facebook, Inc. |
3 | * All rights reserved. | |
4 | * | |
11fdf7f2 TL |
5 | * This source code is licensed under both the BSD-style license (found in the |
6 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found | |
7 | * in the COPYING file in the root directory of this source tree). | |
7c673cae FG |
8 | */ |
9 | #include "utils/ThreadPool.h" | |
10 | ||
11 | #include <gtest/gtest.h> | |
12 | #include <atomic> | |
11fdf7f2 | 13 | #include <iostream> |
7c673cae FG |
14 | #include <thread> |
15 | #include <vector> | |
16 | ||
17 | using namespace pzstd; | |
18 | ||
19 | TEST(ThreadPool, Ordering) { | |
20 | std::vector<int> results; | |
21 | ||
22 | { | |
23 | ThreadPool executor(1); | |
24 | for (int i = 0; i < 10; ++i) { | |
25 | executor.add([ &results, i ] { results.push_back(i); }); | |
26 | } | |
27 | } | |
28 | ||
29 | for (int i = 0; i < 10; ++i) { | |
30 | EXPECT_EQ(i, results[i]); | |
31 | } | |
32 | } | |
33 | ||
34 | TEST(ThreadPool, AllJobsFinished) { | |
35 | std::atomic<unsigned> numFinished{0}; | |
36 | std::atomic<bool> start{false}; | |
37 | { | |
11fdf7f2 | 38 | std::cerr << "Creating executor" << std::endl; |
7c673cae FG |
39 | ThreadPool executor(5); |
40 | for (int i = 0; i < 10; ++i) { | |
41 | executor.add([ &numFinished, &start ] { | |
42 | while (!start.load()) { | |
11fdf7f2 | 43 | std::this_thread::yield(); |
7c673cae FG |
44 | } |
45 | ++numFinished; | |
46 | }); | |
47 | } | |
11fdf7f2 | 48 | std::cerr << "Starting" << std::endl; |
7c673cae | 49 | start.store(true); |
11fdf7f2 | 50 | std::cerr << "Finishing" << std::endl; |
7c673cae FG |
51 | } |
52 | EXPECT_EQ(10, numFinished.load()); | |
53 | } | |
54 | ||
55 | TEST(ThreadPool, AddJobWhileJoining) { | |
56 | std::atomic<bool> done{false}; | |
57 | { | |
58 | ThreadPool executor(1); | |
59 | executor.add([&executor, &done] { | |
60 | while (!done.load()) { | |
61 | std::this_thread::yield(); | |
62 | } | |
63 | // Sleep for a second to be sure that we are joining | |
64 | std::this_thread::sleep_for(std::chrono::seconds(1)); | |
65 | executor.add([] { | |
66 | EXPECT_TRUE(false); | |
67 | }); | |
68 | }); | |
69 | done.store(true); | |
70 | } | |
71 | } |