Skip to content

Commit

Permalink
Fix objcache ut, waits acquires done by semaphore
Browse files Browse the repository at this point in the history
  • Loading branch information
Coldwings authored and beef9999 committed Aug 17, 2023
1 parent 8b6259b commit 72c37b6
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions common/test/test_objcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,19 @@ TEST(ObjectCache, timeout_refresh) {
ocache.expire();
EXPECT_EQ(1, release_cnt);
}
struct ph_arg {
ObjectCache<int, ShowOnDtor *> *ocache;
photon::semaphore *sem;
};

void* ph_act(void* arg) {
auto ocache = (ObjectCache<int, ShowOnDtor*>*)(arg);
void *ph_act(void *arg) {
auto a = (ph_arg *)arg;
DEFER(a->sem->signal(1));
auto ctor = [] {
photon::thread_usleep(1000);
return nullptr;
photon::thread_usleep(1000);
return nullptr;
};
ocache->acquire(0, ctor);
a->ocache->acquire(0, ctor);
return nullptr;
}

Expand All @@ -128,11 +133,13 @@ TEST(ObjectCache, ctor_may_yield_and_null) {
set_log_output_level(ALOG_INFO);
DEFER(set_log_output_level(ALOG_DEBUG));
ObjectCache<int, ShowOnDtor*> ocache(1000UL * 1000);
photon::semaphore sem(0);
ph_arg a{&ocache, &sem};
// 1s
for (int i = 0; i < 10; i++) {
photon::thread_create(&ph_act, &ocache);
photon::thread_create(&ph_act, &a);
}
photon::thread_usleep(110UL * 1000);
sem.wait(10);
EXPECT_EQ(1, ocache._set.size());
ocache.expire();
photon::thread_usleep(1100UL * 1000);
Expand Down

0 comments on commit 72c37b6

Please sign in to comment.