Repository navigation
Expand file tree
/
Copy pathtask_group_storage_test.cpp
More file actions
101 lines (78 loc) · 2.56 KB
/
Copy pathtask_group_storage_test.cpp
File metadata and controls
101 lines (78 loc) · 2.56 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <gtest/gtest.h>
import std;
import mcpplibs.cmp;
namespace {
struct alignas(std::max_align_t) AllocationHeader final {
std::size_t size_ {};
};
std::atomic<std::size_t> gLiveBytes {};
} // namespace
void* operator new(std::size_t size) {
const auto actual = size == 0 ? 1 : size;
auto* memory = static_cast<AllocationHeader*>(
std::malloc(sizeof(AllocationHeader) + actual));
if (!memory) {
throw std::bad_alloc {};
}
memory->size_ = actual;
gLiveBytes.fetch_add(actual, std::memory_order_relaxed);
return memory + 1;
}
void operator delete(void* pointer) noexcept {
if (pointer) {
auto* memory = static_cast<AllocationHeader*>(pointer) - 1;
gLiveBytes.fetch_sub(memory->size_, std::memory_order_relaxed);
std::free(memory);
}
}
void operator delete(void* pointer, std::size_t) noexcept {
::operator delete(pointer);
}
namespace {
using namespace mcpplibs::cmp;
struct Retention final {
std::size_t before_ {};
std::size_t peak_ {};
std::size_t after_ {};
int completed_ {};
bool caught_ {};
};
Task<void> immediate_child(int& completed, bool fail) {
++completed;
if (fail) {
throw std::runtime_error { "first failure" };
}
co_return;
}
Task<Retention> measure_retention(int count, bool fail) {
TaskGroup group {};
Retention retention {};
auto joining = group.join();
retention.before_ = gLiveBytes.load();
for (int index {}; index < count; ++index) {
group.spawn(immediate_child(retention.completed_, fail && index == 0));
}
retention.peak_ = gLiveBytes.load();
try {
co_await std::move(joining);
} catch (const std::runtime_error& error) {
retention.caught_ = std::string_view { error.what() } == "first failure";
}
retention.after_ = gLiveBytes.load();
co_return retention;
}
TEST(CmpTaskGroupStorageTest, JoinReleasesTerminalFramesBeforeGroupDestruction) {
for (const int count : { 1'000, 10'000, 50'000 }) {
for (const bool fail : { false, true }) {
RunLoop loop {};
const auto retention = loop.run(measure_retention(count, fail));
EXPECT_EQ(retention.completed_, count);
EXPECT_EQ(retention.caught_, fail);
EXPECT_GT(retention.peak_, retention.before_);
EXPECT_LE(retention.after_, retention.before_);
std::println("storage count={} failure={} before={} peak={} after={}",
count, fail, retention.before_, retention.peak_, retention.after_);
}
}
}
} // namespace