Robustify equeue multithread test

Rather than wait 10ms before breaking the other thread, and expecting at
least one event to have been run, wait for one event to be run, then
break.

Should avoid some spurious failures that have been seen.
pull/12173/head
Kevin Bracey 2019-12-17 16:16:27 +02:00 committed by Bartek Szatkowski
parent 50455c0aa4
commit 5f495db06b
1 changed files with 59 additions and 8 deletions

View File

@ -139,11 +139,64 @@ static void *multithread_thread(void *p)
return 0;
}
class ecount {
mutable pthread_mutex_t mutex;
pthread_cond_t cond;
uint8_t count;
public:
ecount() : count(0)
{
int err = pthread_mutex_init(&mutex, NULL);
EXPECT_EQ(0, err);
err = pthread_cond_init(&cond, NULL);
EXPECT_EQ(0, err);
}
~ecount()
{
int err = pthread_mutex_destroy(&mutex);
EXPECT_EQ(0, err);
err = pthread_cond_destroy(&cond);
EXPECT_EQ(0, err);
}
void lock() const
{
int err = pthread_mutex_lock(&mutex);
EXPECT_EQ(0, err);
}
void unlock() const
{
int err = pthread_mutex_unlock(&mutex);
EXPECT_EQ(0, err);
}
void touch()
{
lock();
if (count < 200) {
count++;
}
unlock();
int err = pthread_cond_broadcast(&cond);
EXPECT_EQ(0, err);
}
void wait_for_touches(uint8_t n)
{
lock();
while (count < n) {
int err = pthread_cond_wait(&cond, &mutex);
EXPECT_EQ(0, err);
}
unlock();
}
};
static void multithread_func(void *p)
{
if ((*(reinterpret_cast<uint8_t *>(p))) < 200) {
(*(reinterpret_cast<uint8_t *>(p)))++;
}
static_cast<ecount *>(p)->touch();
}
static void background_func(void *p, int ms)
@ -665,20 +718,18 @@ TEST_F(TestEqueue, test_equeue_multithread)
int err = equeue_create(&q, TEST_EQUEUE_SIZE);
ASSERT_EQ(0, err);
uint8_t touched = 0;
equeue_call_every(&q, 1, multithread_func, &touched);
ecount t;
equeue_call_every(&q, 1, multithread_func, &t);
pthread_t thread;
err = pthread_create(&thread, 0, multithread_thread, &q);
ASSERT_EQ(0, err);
usleep(10000);
t.wait_for_touches(1);
equeue_break(&q);
err = pthread_join(thread, 0);
ASSERT_EQ(0, err);
EXPECT_TRUE(touched > 1);
equeue_destroy(&q);
}