Skip to content

Commit 4dc4a42

Browse files
authored
Adjust tests for sleeping (#738)
Assert a lower bound for sleeping, not an upper bound, to handle the case that the system is busy so the sleep might take longer than intended.
1 parent b2c386e commit 4dc4a42

3 files changed

Lines changed: 25 additions & 29 deletions

File tree

test/src/clock_nanosleep.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,13 @@
1212
t_error("%s failed (errno = %d)\n", #c, errno); \
1313
} while (0)
1414

15-
static void test_elapsed_not_too_long(struct timespec *start_time,
16-
struct timespec *end_time,
17-
long ns_to_sleep) {
15+
static void test_elapsed(struct timespec *start_time, struct timespec *end_time,
16+
long ns_to_sleep) {
1817
TEST(end_time->tv_sec - start_time->tv_sec <= 1);
1918

2019
long nanoseconds_elapsed = (end_time->tv_sec - start_time->tv_sec) * 1E9 -
2120
start_time->tv_nsec + end_time->tv_nsec;
22-
23-
// Test that the difference between the requested amount of sleep
24-
// and the actual elapsed time is within an acceptable margin
25-
double difference = abs(nanoseconds_elapsed - ns_to_sleep) / ns_to_sleep;
26-
27-
// Allow the actual sleep time to be twice as much as the requested time
28-
TEST(difference <= 1);
21+
TEST(nanoseconds_elapsed >= ns_to_sleep);
2922
}
3023

3124
int main(void) {
@@ -45,7 +38,7 @@ int main(void) {
4538
TEST(clock_nanosleep(CLOCK_MONOTONIC, 0, &time_to_sleep, NULL) == 0);
4639
clock_gettime(CLOCK_MONOTONIC, &end_time);
4740

48-
test_elapsed_not_too_long(&start_time, &end_time, ns_to_sleep);
41+
test_elapsed(&start_time, &end_time, ns_to_sleep);
4942

5043
// Sleep for another 50ms to get us pretty far from 5ms from the start of
5144
// this process to increase the chance the next test fails if it's not
@@ -64,7 +57,7 @@ int main(void) {
6457
0);
6558
clock_gettime(CLOCK_MONOTONIC, &end_time);
6659

67-
test_elapsed_not_too_long(&start_time, &end_time, ns_to_sleep);
60+
test_elapsed(&start_time, &end_time, ns_to_sleep);
6861

6962
return t_status;
7063
}

test/src/nanosleep.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdlib.h>
55
#include <sys/time.h>
66
#include <time.h>
7+
#include <wasi/version.h>
78

89
#define TEST(c) \
910
do { \
@@ -12,28 +13,28 @@
1213
t_error("%s failed (errno = %d)\n", #c, errno); \
1314
} while (0)
1415

16+
#ifdef __wasip1__
17+
#define CLOCK CLOCK_REALTIME
18+
#else
19+
#define CLOCK CLOCK_MONOTONIC
20+
#endif
21+
1522
int main(void) {
1623
// Sleep for a total of 5ms
1724
long ns_to_sleep = 5E6;
1825

1926
struct timespec start_time, end_time;
20-
clock_gettime(CLOCK_MONOTONIC, &start_time);
27+
clock_gettime(CLOCK, &start_time);
2128
struct timespec dur;
2229
dur.tv_sec = 0;
2330
dur.tv_nsec = ns_to_sleep;
2431
TEST(nanosleep(&dur, NULL) == 0);
25-
clock_gettime(CLOCK_MONOTONIC, &end_time);
32+
clock_gettime(CLOCK, &end_time);
2633
TEST(end_time.tv_sec - start_time.tv_sec <= 1);
2734

2835
long nanoseconds_elapsed = (end_time.tv_sec - start_time.tv_sec) * 1E9 -
2936
start_time.tv_nsec + end_time.tv_nsec;
30-
31-
// Test that the difference between the requested amount of sleep
32-
// and the actual elapsed time is within an acceptable margin
33-
double difference = abs(nanoseconds_elapsed - ns_to_sleep) / ns_to_sleep;
34-
35-
// Allow the actual sleep time to be twice as much as the requested time
36-
TEST(difference <= 1);
37+
TEST(nanoseconds_elapsed >= ns_to_sleep);
3738

3839
return t_status;
3940
}

test/src/usleep.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdlib.h>
55
#include <sys/time.h>
66
#include <time.h>
7+
#include <wasi/version.h>
78

89
#define TEST(c) \
910
do { \
@@ -12,25 +13,26 @@
1213
t_error("%s failed (errno = %d)\n", #c, errno); \
1314
} while (0)
1415

16+
#ifdef __wasip1__
17+
#define CLOCK CLOCK_REALTIME
18+
#else
19+
#define CLOCK CLOCK_MONOTONIC
20+
#endif
21+
1522
int main(void) {
1623
// Sleep for a total of 5ms
1724
long ns_to_sleep = 5E6;
1825

1926
struct timespec start_time, end_time;
20-
clock_gettime(CLOCK_MONOTONIC, &start_time);
27+
clock_gettime(CLOCK, &start_time);
2128
TEST(usleep(ns_to_sleep / 1000) == 0);
22-
clock_gettime(CLOCK_MONOTONIC, &end_time);
29+
clock_gettime(CLOCK, &end_time);
2330
TEST(end_time.tv_sec - start_time.tv_sec <= 1);
2431

2532
long nanoseconds_elapsed = (end_time.tv_sec - start_time.tv_sec) * 1E9 -
2633
start_time.tv_nsec + end_time.tv_nsec;
2734

28-
// Test that the difference between the requested amount of sleep
29-
// and the actual elapsed time is within an acceptable margin
30-
double difference = abs(nanoseconds_elapsed - ns_to_sleep) / ns_to_sleep;
31-
32-
// Allow the actual sleep time to be twice as much as the requested time
33-
TEST(difference <= 1);
35+
TEST(nanoseconds_elapsed >= ns_to_sleep);
3436

3537
return t_status;
3638
}

0 commit comments

Comments
 (0)