forked from facebookincubator/dispenso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread_id_test.cpp
62 lines (51 loc) · 1.46 KB
/
thread_id_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <dispenso/thread_id.h>
#include <unordered_set>
#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
TEST(ThreadId, Repeatable) {
constexpr int kRounds = 100;
constexpr int kThreadsPerRound = 8;
for (int round = 0; round < kRounds; ++round) {
std::vector<std::thread> threads;
for (int i = 0; i < kThreadsPerRound; ++i) {
threads.emplace_back([]() {
constexpr int kTrials = 1000;
auto id = dispenso::threadId();
for (int i = 0; i < kTrials; ++i) {
EXPECT_EQ(id, dispenso::threadId());
}
});
}
for (auto& t : threads) {
t.join();
}
}
}
TEST(ThreadId, Unique) {
constexpr int kRounds = 1000;
constexpr int kThreadsPerRound = 8;
std::vector<uint64_t> ids(kRounds * kThreadsPerRound);
std::atomic<size_t> slot(0);
for (int round = 0; round < kRounds; ++round) {
std::vector<std::thread> threads;
for (int i = 0; i < kThreadsPerRound; ++i) {
threads.emplace_back([&ids, &slot]() {
ids[slot.fetch_add(1, std::memory_order_relaxed)] = dispenso::threadId();
});
}
for (auto& t : threads) {
t.join();
}
}
std::unordered_set<uint64_t> uniquenessSet;
for (uint64_t id : ids) {
EXPECT_TRUE(uniquenessSet.insert(id).second);
}
}