darion-yaphet commented on code in PR #3638:
URL: https://github.com/apache/brpc/pull/3638#discussion_r4344999871
##########
test/bthread_key_unittest.cpp:
##########
@@ -566,6 +567,80 @@ TEST(KeyTest, borrow_and_return_keytable_when_using_pool) {
ASSERT_EQ(0, bthread_keytable_pool_destroy(&test_pool));
}
+struct ConcurrentPoolData {
+ bthread_keytable_pool_t pool;
+ CountdownEvent ready;
+ CountdownEvent start;
+ CountdownEvent destroyed;
+ Counters counters;
+
+ explicit ConcurrentPoolData(int nworkers) : ready(nworkers) {}
+};
+
+static void* create_concurrent_pool_data(const void* arg) {
+ Counters* counters = const_cast<Counters*>(static_cast<const
Counters*>(arg));
+ counters->ncreate.fetch_add(1, butil::memory_order_relaxed);
+ return counters;
+}
+
+static void destroy_concurrent_pool_data(void* arg) {
+ static_cast<Counters*>(arg)->ndestroy.fetch_add(1,
butil::memory_order_relaxed);
+}
+
+static void* borrow_and_return_during_pool_destruction(void* arg) {
+ ConcurrentPoolData* data = static_cast<ConcurrentPoolData*>(arg);
+ // Keep a borrowed table across destruction as well as racing new borrows
+ // with destruction. The pool and key remain valid until all workers exit.
+ KeyTable* held = borrow_keytable(&data->pool);
+ EXPECT_NE(nullptr, held);
+ data->ready.signal();
+ data->start.wait();
+ for (int i = 0; i < 4096; ++i) {
+ KeyTable* kt = borrow_keytable(&data->pool);
+ return_keytable(&data->pool, kt);
+ }
+ data->destroyed.wait();
+ return_keytable(&data->pool, held);
+ return nullptr;
+}
+
+TEST(KeyTest, borrow_and_return_keytable_during_pool_destruction) {
+ GFLAGS_NAMESPACE::FlagSaver flag_saver;
+ bthread::FLAGS_borrow_from_globle_size = 20;
+ const size_t NWORKERS = 8;
+ const size_t NTABLES = NWORKERS * bthread::FLAGS_borrow_from_globle_size;
+ ConcurrentPoolData data(NWORKERS);
+ ASSERT_EQ(0, bthread_keytable_pool_init(&data.pool));
+ bthread_key_t key;
+ ASSERT_EQ(0, bthread_key_create(&key, destroy_concurrent_pool_data));
+ bthread_keytable_pool_reserve(&data.pool, NTABLES, key,
+ create_concurrent_pool_data, &data.counters);
+ ASSERT_EQ(NTABLES,
data.counters.ncreate.load(butil::memory_order_relaxed));
+
+ pthread_t workers[NWORKERS];
+ size_t nstarted = 0;
+ for (; nstarted < NWORKERS; ++nstarted) {
+ int rc = pthread_create(&workers[nstarted], nullptr,
+ borrow_and_return_during_pool_destruction,
&data);
+ EXPECT_EQ(0, rc);
+ if (rc != 0) {
+ data.ready.signal(NWORKERS - nstarted);
+ break;
Review Comment:
The test accounts for workers that were not created by signaling the
remaining count. It then releases and joins the workers that did start.
Using `ASSERT_EQ` here would return early without releasing or joining those
workers, so it could leave them waiting or accessing the test's local state.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]