]>
git.proxmox.com Git - ceph.git/blob - ceph/src/zstd/contrib/pzstd/utils/test/ThreadPoolTest.cpp
2 * Copyright (c) 2016-present, Facebook, Inc.
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
9 #include "utils/ThreadPool.h"
11 #include <gtest/gtest.h>
16 using namespace pzstd
;
18 TEST(ThreadPool
, Ordering
) {
19 std::vector
<int> results
;
22 ThreadPool
executor(1);
23 for (int i
= 0; i
< 10; ++i
) {
24 executor
.add([ &results
, i
] { results
.push_back(i
); });
28 for (int i
= 0; i
< 10; ++i
) {
29 EXPECT_EQ(i
, results
[i
]);
33 TEST(ThreadPool
, AllJobsFinished
) {
34 std::atomic
<unsigned> numFinished
{0};
35 std::atomic
<bool> start
{false};
37 ThreadPool
executor(5);
38 for (int i
= 0; i
< 10; ++i
) {
39 executor
.add([ &numFinished
, &start
] {
40 while (!start
.load()) {
48 EXPECT_EQ(10, numFinished
.load());
51 TEST(ThreadPool
, AddJobWhileJoining
) {
52 std::atomic
<bool> done
{false};
54 ThreadPool
executor(1);
55 executor
.add([&executor
, &done
] {
56 while (!done
.load()) {
57 std::this_thread::yield();
59 // Sleep for a second to be sure that we are joining
60 std::this_thread::sleep_for(std::chrono::seconds(1));