Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Robustify equeue multithread unit test #12126

Merged
merged 1 commit into from
Dec 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 59 additions & 8 deletions UNITTESTS/events/equeue/test_equeue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 200? What is the significance of this value ?

Copy link
Contributor Author

@kjbracey kjbracey Dec 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's from the original code.

Don't think it's particularly magic - it's using an 8-bit counter for "how many times was this called", but stops incrementing at 200 to avoid any wrap possibility.

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)
Expand Down Expand Up @@ -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);
}

Expand Down