Skip to content

Commit f98c337

Browse files
authored
Fix stack-use-after-scope in resource manager test (ClickHouse#49908)
* Fix stack-use-after-scope in resource manager test * fix
1 parent dd5ee93 commit f98c337

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

src/IO/Resource/tests/gtest_resource_manager_hierarchical.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,18 @@ TEST(IOResourceDynamicResourceManager, Smoke)
4747

4848
TEST(IOResourceDynamicResourceManager, Fairness)
4949
{
50-
constexpr size_t T = 3; // threads per queue
51-
int N = 100; // requests per thread
52-
ResourceTest t(2 * T + 1);
50+
// Total cost for A and B cannot differ for more than 1 (every request has cost equal to 1).
51+
// Requests from A use `value = 1` and from B `value = -1` is used.
52+
std::atomic<Int64> unfairness = 0;
53+
auto fairness_diff = [&] (Int64 value)
54+
{
55+
Int64 cur_unfairness = unfairness.fetch_add(value, std::memory_order_relaxed) + value;
56+
EXPECT_NEAR(cur_unfairness, 0, 1);
57+
};
58+
59+
constexpr size_t threads_per_queue = 3;
60+
int requests_per_thread = 100;
61+
ResourceTest t(2 * threads_per_queue + 1);
5362

5463
t.update(R"CONFIG(
5564
<clickhouse>
@@ -70,39 +79,29 @@ TEST(IOResourceDynamicResourceManager, Fairness)
7079
</clickhouse>
7180
)CONFIG");
7281

73-
74-
// Total cost for A and B cannot differ for more than 1 (every request has cost equal to 1).
75-
// Requests from A use `value = 1` and from B `value = -1` is used.
76-
std::atomic<Int64> unfairness = 0;
77-
auto fairness_diff = [&] (Int64 value)
78-
{
79-
Int64 cur_unfairness = unfairness.fetch_add(value, std::memory_order_relaxed) + value;
80-
EXPECT_NEAR(cur_unfairness, 0, 1);
81-
};
82-
83-
for (int thr = 0; thr < T; thr++)
82+
for (int thread = 0; thread < threads_per_queue; thread++)
8483
{
8584
t.threads.emplace_back([&]
8685
{
8786
ClassifierPtr c = t.manager->acquire("A");
8887
ResourceLink link = c->get("res1");
89-
t.startBusyPeriod(link, 1, N);
90-
for (int req = 0; req < N; req++)
88+
t.startBusyPeriod(link, 1, requests_per_thread);
89+
for (int request = 0; request < requests_per_thread; request++)
9190
{
9291
TestGuard g(t, link, 1);
9392
fairness_diff(1);
9493
}
9594
});
9695
}
9796

98-
for (int thr = 0; thr < T; thr++)
97+
for (int thread = 0; thread < threads_per_queue; thread++)
9998
{
10099
t.threads.emplace_back([&]
101100
{
102101
ClassifierPtr c = t.manager->acquire("B");
103102
ResourceLink link = c->get("res1");
104-
t.startBusyPeriod(link, 1, N);
105-
for (int req = 0; req < N; req++)
103+
t.startBusyPeriod(link, 1, requests_per_thread);
104+
for (int request = 0; request < requests_per_thread; request++)
106105
{
107106
TestGuard g(t, link, 1);
108107
fairness_diff(-1);

0 commit comments

Comments
 (0)