Skip to content

Commit 1fe887b

Browse files
committed
tests: extend misc_test_ug_reltimedwait_timeout
- test also timeout with timeout=0 - check that if timeouted, the remaining timeout is 0
1 parent 84e39cc commit 1fe887b

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

test/misc_test.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -161,27 +161,31 @@ misc_test_ug_reltimedwait_timeout()
161161
pthread_cond_init(&cv, nullptr);
162162
enum { TIMEOUT_20MS = 20 * 1000 * 1000, };
163163

164-
time_ns_t timeout = TIMEOUT_20MS;
165-
int rc = ETIMEDOUT;
166-
time_ns_t t0 = get_time_in_ns();
167-
168-
CHK_PTHR(pthread_mutex_lock(&lock));
169-
while (rc == ETIMEDOUT) {
170-
rc = ug_pthread_cond_reltimedwait(&cv, &lock, &timeout);
171-
time_ns_t t1 = get_time_in_ns();
172-
if (t1 - t0 > TIMEOUT_20MS) {
173-
break;
164+
time_ns_t timeouts[] = { 0, TIMEOUT_20MS };
165+
166+
for (unsigned i = 0; i < countof(timeouts); ++i) {
167+
time_ns_t timeout = timeouts[i];
168+
int rc = ETIMEDOUT;
169+
time_ns_t t0 = get_time_in_ns();
170+
171+
CHK_PTHR(pthread_mutex_lock(&lock));
172+
while (rc == ETIMEDOUT) {
173+
rc = ug_pthread_cond_reltimedwait(&cv, &lock, &timeout);
174+
time_ns_t t1 = get_time_in_ns();
175+
if (t1 - t0 > timeouts[i]) {
176+
break;
177+
}
174178
}
179+
CHK_PTHR(pthread_mutex_unlock(&lock));
180+
time_ns_t t1 = get_time_in_ns();
181+
ASSERT_EQUAL(ETIMEDOUT, rc);
182+
ASSERT_EQUAL(0, timeout);
183+
ASSERT_OP(t1 - t0, >, timeouts[i]);
175184
}
176-
time_ns_t t1 = get_time_in_ns();
177185

178-
CHK_PTHR(pthread_mutex_unlock(&lock));
179186
CHK_PTHR(pthread_mutex_destroy(&lock));
180187
CHK_PTHR(pthread_cond_destroy(&cv));
181188

182-
ASSERT_EQUAL(ETIMEDOUT, rc);
183-
ASSERT_OP(t1 - t0, >, TIMEOUT_20MS);
184-
185189
return 0;
186190
}
187191
static void *

0 commit comments

Comments
 (0)