Skip to content

Commit 684dae7

Browse files
committed
Add tests for timeout on select with PROXY_TO_PTHREAD
Signed-off-by: Kohei Tokunaga <[email protected]>
1 parent ba813f7 commit 684dae7

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#include <sys/select.h>
2+
#include <time.h>
3+
#include <assert.h>
4+
#include <stdio.h>
5+
#include <unistd.h>
6+
#include <pthread.h>
7+
#include <string.h>
8+
#include <sys/time.h>
9+
10+
// Check if timeout works without fds
11+
void test_timeout_without_fds()
12+
{
13+
struct timeval tv;
14+
time_t before_select;
15+
16+
tv.tv_sec = 1;
17+
tv.tv_usec = 0;
18+
before_select = time(NULL);
19+
assert(select(0, NULL, NULL, NULL, &tv) == 0);
20+
assert(time(NULL) - before_select >= 1);
21+
}
22+
23+
// Check if timeout works with fds without events
24+
void test_timeout_with_fds_without_events()
25+
{
26+
struct timeval tv, begin, end;
27+
fd_set readfds;
28+
int pipe_a[2];
29+
30+
assert(pipe(pipe_a) == 0);
31+
32+
tv.tv_sec = 1;
33+
tv.tv_usec = 0;
34+
FD_ZERO(&readfds);
35+
FD_SET(pipe_a[0], &readfds);
36+
gettimeofday(&begin, NULL);
37+
assert(select(pipe_a[0] + 1, &readfds, NULL, NULL, &tv) == 0);
38+
gettimeofday(&end, NULL);
39+
assert((end.tv_sec - begin.tv_sec) * 1000000 + end.tv_usec - begin.tv_usec > 1000000);
40+
41+
close(pipe_a[0]); close(pipe_a[1]);
42+
}
43+
44+
int pipe_shared[2];
45+
46+
void *wakeup_after_2s(void * arg)
47+
{
48+
const char *t = "test\n";
49+
50+
sleep(2);
51+
write(pipe_shared[1], t, strlen(t));
52+
53+
return NULL;
54+
}
55+
56+
// Check if select can unblock on an event
57+
void test_unblock_select()
58+
{
59+
struct timeval begin, end;
60+
fd_set readfds;
61+
int maxfd;
62+
pthread_t tid;
63+
int pipe_a[2];
64+
65+
assert(pipe(pipe_a) == 0);
66+
assert(pipe(pipe_shared) == 0);
67+
68+
FD_ZERO(&readfds);
69+
FD_SET(pipe_a[0], &readfds);
70+
FD_SET(pipe_shared[0], &readfds);
71+
maxfd = (pipe_a[0] > pipe_shared[0] ? pipe_a[0] : pipe_shared[0]);
72+
assert(pthread_create(&tid, NULL, wakeup_after_2s, NULL) == 0);
73+
gettimeofday(&begin, NULL);
74+
assert(select(maxfd + 1, &readfds, NULL, NULL, NULL) == 1);
75+
gettimeofday(&end, NULL);
76+
assert(FD_ISSET(pipe_shared[0], &readfds));
77+
assert((end.tv_sec - begin.tv_sec) * 1000000 + end.tv_usec - begin.tv_usec > 1000000);
78+
79+
pthread_join(tid, NULL);
80+
81+
close(pipe_a[0]); close(pipe_a[1]);
82+
close(pipe_shared[0]); close(pipe_shared[1]);
83+
}
84+
85+
// Check if select works with ready fds
86+
void test_ready_fds()
87+
{
88+
struct timeval tv;
89+
fd_set readfds;
90+
int maxfd;
91+
const char *t = "test\n";
92+
int pipe_c[2];
93+
int pipe_d[2];
94+
95+
assert(pipe(pipe_c) == 0);
96+
assert(pipe(pipe_d) == 0);
97+
98+
write(pipe_c[1], t, strlen(t));
99+
write(pipe_d[1], t, strlen(t));
100+
maxfd = (pipe_c[0] > pipe_d[0] ? pipe_c[0] : pipe_d[0]);
101+
102+
tv.tv_sec = 0;
103+
tv.tv_usec = 0;
104+
FD_ZERO(&readfds);
105+
FD_SET(pipe_c[0], &readfds);
106+
FD_SET(pipe_d[0], &readfds);
107+
assert(select(maxfd + 1, &readfds, NULL, NULL, &tv) == 2);
108+
assert(FD_ISSET(pipe_c[0], &readfds));
109+
assert(FD_ISSET(pipe_d[0], &readfds));
110+
111+
FD_ZERO(&readfds);
112+
FD_SET(pipe_c[0], &readfds);
113+
FD_SET(pipe_d[0], &readfds);
114+
assert(select(maxfd + 1, &readfds, NULL, NULL, NULL) == 2);
115+
assert(FD_ISSET(pipe_c[0], &readfds));
116+
assert(FD_ISSET(pipe_d[0], &readfds));
117+
118+
close(pipe_c[0]); close(pipe_c[1]);
119+
close(pipe_d[0]); close(pipe_d[1]);
120+
}
121+
122+
int main()
123+
{
124+
test_timeout_without_fds();
125+
test_timeout_with_fds_without_events();
126+
test_unblock_select();
127+
test_ready_fds();
128+
return 0;
129+
}

test/test_core.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9680,6 +9680,9 @@ def test_externref_emjs(self, dynlink):
96809680
def test_syscall_intercept(self):
96819681
self.do_core_test('test_syscall_intercept.c')
96829682

9683+
def test_pthread_select_timeout(self):
9684+
self.do_runf('core/test_pthread_select_timeout.c', cflags=['-pthread', '-sPROXY_TO_PTHREAD=1', '-sEXIT_RUNTIME=1'])
9685+
96839686
@also_without_bigint
96849687
def test_jslib_i64_params(self):
96859688
# Tests the defineI64Param and receiveI64ParamAsI53 helpers that are

0 commit comments

Comments
 (0)