grpc
third_party
abseil-cpp
absl
synchronization
abseil-cpp/absl/synchronization/blocking_counter_test.cc
Go to the documentation of this file.
1
// Copyright 2017 The Abseil Authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include "absl/synchronization/blocking_counter.h"
16
17
#include <thread>
// NOLINT(build/c++11)
18
#include <vector>
19
20
#include "gtest/gtest.h"
21
#include "absl/time/clock.h"
22
#include "absl/time/time.h"
23
24
namespace
absl
{
25
ABSL_NAMESPACE_BEGIN
26
namespace
{
27
28
void
PauseAndDecreaseCounter
(
BlockingCounter
*
counter
,
int
*
done
) {
29
absl::SleepFor
(
absl::Seconds
(1));
30
*
done
= 1;
31
counter
->DecrementCount();
32
}
33
34
TEST
(BlockingCounterTest, BasicFunctionality) {
35
// This test verifies that BlockingCounter functions correctly. Starts a
36
// number of threads that just sleep for a second and decrement a counter.
37
38
// Initialize the counter.
39
const
int
num_workers = 10;
40
BlockingCounter
counter
(num_workers);
41
42
std::vector<std::thread>
workers
;
43
std::vector<int>
done
(num_workers, 0);
44
45
// Start a number of parallel tasks that will just wait for a seconds and
46
// then decrement the count.
47
workers
.reserve(num_workers);
48
for
(
int
k
= 0;
k
< num_workers;
k
++) {
49
workers
.emplace_back(
50
[&
counter
, &
done
,
k
] {
PauseAndDecreaseCounter
(&
counter
, &
done
[
k
]); });
51
}
52
53
// Wait for the threads to have all finished.
54
counter
.Wait();
55
56
// Check that all the workers have completed.
57
for
(
int
k
= 0;
k
< num_workers;
k
++) {
58
EXPECT_EQ
(1,
done
[
k
]);
59
}
60
61
for
(
std::thread
& w :
workers
) {
62
w.join();
63
}
64
}
65
66
TEST
(BlockingCounterTest, WaitZeroInitialCount) {
67
BlockingCounter
counter
(0);
68
counter
.Wait();
69
}
70
71
#if GTEST_HAS_DEATH_TEST
72
TEST
(BlockingCounterTest, WaitNegativeInitialCount) {
73
EXPECT_DEATH(
BlockingCounter
counter
(-1),
74
"BlockingCounter initial_count negative"
);
75
}
76
#endif
77
78
}
// namespace
79
ABSL_NAMESPACE_END
80
}
// namespace absl
absl::TEST
TEST(NotificationTest, SanityTest)
Definition:
abseil-cpp/absl/synchronization/notification_test.cc:126
absl::SleepFor
void SleepFor(absl::Duration duration)
Definition:
abseil-cpp/absl/time/clock.h:70
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition:
third_party/abseil-cpp/absl/base/config.h:171
setup.k
k
Definition:
third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition:
iomgr/time_averaged_stats_test.cc:27
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition:
third_party/abseil-cpp/absl/base/config.h:170
done
struct tab * done
Definition:
bloaty/third_party/zlib/examples/enough.c:176
counter
static int counter
Definition:
abseil-cpp/absl/flags/reflection_test.cc:131
absl::BlockingCounter
Definition:
abseil-cpp/absl/synchronization/blocking_counter.h:63
absl::ABSL_NAMESPACE_BEGIN::PauseAndDecreaseCounter
void PauseAndDecreaseCounter(BlockingCounter *counter, int *done)
Definition:
abseil-cpp/absl/synchronization/blocking_counter_test.cc:28
absl::Seconds
constexpr Duration Seconds(T n)
Definition:
third_party/abseil-cpp/absl/time/time.h:419
workers
struct child_worker * workers
absl
Definition:
abseil-cpp/absl/algorithm/algorithm.h:31
thread
static uv_thread_t thread
Definition:
test-async-null-cb.c:29
grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:48