10 #include "pthreadpool_pipe.h"
11 #include "pthreadpool_tevent.h"
13 static int test_init(void)
15 struct pthreadpool_pipe
*p
;
18 ret
= pthreadpool_pipe_init(1, &p
);
20 fprintf(stderr
, "pthreadpool_pipe_init failed: %s\n",
24 ret
= pthreadpool_pipe_destroy(p
);
26 fprintf(stderr
, "pthreadpool_pipe_destroy failed: %s\n",
33 static void test_sleep(void *ptr
)
35 int *ptimeout
= (int *)ptr
;
37 ret
= poll(NULL
, 0, *ptimeout
);
39 fprintf(stderr
, "poll returned %d (%s)\n",
40 ret
, strerror(errno
));
44 static int test_jobs(int num_threads
, int num_jobs
)
47 struct pthreadpool_pipe
*p
;
51 finished
= (char *)calloc(1, num_jobs
);
52 if (finished
== NULL
) {
53 fprintf(stderr
, "calloc failed\n");
57 ret
= pthreadpool_pipe_init(num_threads
, &p
);
59 fprintf(stderr
, "pthreadpool_pipe_init failed: %s\n",
64 for (i
=0; i
<num_jobs
; i
++) {
65 ret
= pthreadpool_pipe_add_job(p
, i
, test_sleep
, &timeout
);
67 fprintf(stderr
, "pthreadpool_pipe_add_job failed: "
68 "%s\n", strerror(ret
));
73 for (i
=0; i
<num_jobs
; i
++) {
75 ret
= pthreadpool_pipe_finished_jobs(p
, &jobid
, 1);
77 fprintf(stderr
, "pthreadpool_pipe_finished_jobs "
78 "failed: %s\n", strerror(-ret
));
81 if ((ret
!= 1) || (jobid
>= num_jobs
)) {
82 fprintf(stderr
, "invalid job number %d\n", jobid
);
88 for (i
=0; i
<num_jobs
; i
++) {
89 if (finished
[i
] != 1) {
90 fprintf(stderr
, "finished[%d] = %d\n",
96 ret
= pthreadpool_pipe_destroy(p
);
98 fprintf(stderr
, "pthreadpool_pipe_destroy failed: %s\n",
107 static int test_busydestroy(void)
109 struct pthreadpool_pipe
*p
;
114 ret
= pthreadpool_pipe_init(1, &p
);
116 fprintf(stderr
, "pthreadpool_pipe_init failed: %s\n",
120 ret
= pthreadpool_pipe_add_job(p
, 1, test_sleep
, &timeout
);
122 fprintf(stderr
, "pthreadpool_pipe_add_job failed: %s\n",
126 ret
= pthreadpool_pipe_destroy(p
);
128 fprintf(stderr
, "Could destroy a busy pool\n");
132 pfd
.fd
= pthreadpool_pipe_signal_fd(p
);
133 pfd
.events
= POLLIN
|POLLERR
;
136 ret
= poll(&pfd
, 1, -1);
137 } while ((ret
== -1) && (errno
== EINTR
));
139 ret
= pthreadpool_pipe_finished_jobs(p
, &jobid
, 1);
141 fprintf(stderr
, "pthreadpool_pipe_finished_jobs failed: %s\n",
146 ret
= pthreadpool_pipe_destroy(p
);
148 fprintf(stderr
, "pthreadpool_pipe_destroy failed: %s\n",
155 static int test_fork(void)
157 struct pthreadpool_pipe
*p
;
161 ret
= pthreadpool_pipe_init(1, &p
);
163 fprintf(stderr
, "pthreadpool_pipe_init failed: %s\n",
167 ret
= pthreadpool_pipe_destroy(p
);
169 fprintf(stderr
, "pthreadpool_pipe_destroy failed: %s\n",
176 perror("fork failed");
182 waited
= wait(&status
);
184 perror("wait failed");
187 if (waited
!= child
) {
188 fprintf(stderr
, "expected child %d, got %d\n",
189 (int)child
, (int)waited
);
195 static void test_tevent_wait(void *private_data
)
197 int *timeout
= private_data
;
198 poll(NULL
, 0, *timeout
);
201 static int test_tevent_1(void)
203 struct tevent_context
*ev
;
204 struct pthreadpool_tevent
*pool
;
205 struct tevent_req
*req1
, *req2
;
207 int timeout100
= 100;
211 ev
= tevent_context_init(NULL
);
214 fprintf(stderr
, "tevent_context_init failed: %s\n",
218 ret
= pthreadpool_tevent_init(ev
, 0, &pool
);
220 fprintf(stderr
, "pthreadpool_tevent_init failed: %s\n",
225 req1
= pthreadpool_tevent_job_send(
226 ev
, ev
, pool
, test_tevent_wait
, &timeout10
);
228 fprintf(stderr
, "pthreadpool_tevent_job_send failed\n");
232 req2
= pthreadpool_tevent_job_send(
233 ev
, ev
, pool
, test_tevent_wait
, &timeout100
);
235 fprintf(stderr
, "pthreadpool_tevent_job_send failed\n");
239 ok
= tevent_req_poll(req2
, ev
);
242 fprintf(stderr
, "tevent_req_poll failed: %s\n",
247 ret
= pthreadpool_tevent_job_recv(req1
);
250 fprintf(stderr
, "tevent_req_poll failed: %s\n",
258 ret
= tevent_loop_wait(ev
);
260 fprintf(stderr
, "tevent_loop_wait failed\n");
273 ret
= test_tevent_1();
275 fprintf(stderr
, "test_event_1 failed: %s\n",
282 fprintf(stderr
, "test_init failed\n");
288 fprintf(stderr
, "test_fork failed\n");
292 ret
= test_jobs(10, 10000);
294 fprintf(stderr
, "test_jobs failed\n");
298 ret
= test_busydestroy();
300 fprintf(stderr
, "test_busydestroy failed\n");