summaryrefslogtreecommitdiff
path: root/lib/pthreadpool/tests_cmocka.c
blob: dc7b1150b5c0d90f052f6af63d4c4244695ffc41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/*
 * Unix SMB/CIFS implementation.
 * cmocka tests for thread pool implementation
 * Copyright (C) Christof Schmitt 2017
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"
#include <errno.h>
#include <pthread.h>
#include <setjmp.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <talloc.h>
#include <tevent.h>
#include <pthreadpool_tevent.h>

#include <cmocka.h>
#include <poll.h>

#ifdef HAVE_VALGRIND_HELGRIND_H
#include <valgrind/helgrind.h>
#endif
#ifndef ANNOTATE_BENIGN_RACE_SIZED
#define ANNOTATE_BENIGN_RACE_SIZED(address, size, describtion)
#endif

struct pthreadpool_tevent_test {
	struct tevent_context *ev;
	struct pthreadpool_tevent *upool;
	struct pthreadpool_tevent *spool;
	struct pthreadpool_tevent *opool;
};

static int setup_pthreadpool_tevent(void **state)
{
	struct pthreadpool_tevent_test *t;
	int ret;
	size_t max_threads;

	t = talloc_zero(NULL, struct pthreadpool_tevent_test);
	assert_non_null(t);

	t->ev = tevent_context_init(t);
	assert_non_null(t->ev);

	ret = pthreadpool_tevent_init(t->ev, UINT_MAX, &t->upool);
	assert_int_equal(ret, 0);

	max_threads = pthreadpool_tevent_max_threads(t->upool);
	assert_int_equal(max_threads, UINT_MAX);

	ret = pthreadpool_tevent_init(t->ev, 1, &t->opool);
	assert_int_equal(ret, 0);

	max_threads = pthreadpool_tevent_max_threads(t->opool);
	assert_int_equal(max_threads, 1);

	ret = pthreadpool_tevent_init(t->ev, 0, &t->spool);
	assert_int_equal(ret, 0);

	max_threads = pthreadpool_tevent_max_threads(t->spool);
	assert_int_equal(max_threads, 0);

	*state = t;

	return 0;
}

static int teardown_pthreadpool_tevent(void **state)
{
	struct pthreadpool_tevent_test *t = *state;

	TALLOC_FREE(t);

	return 0;
}

int __wrap_pthread_create(pthread_t *thread, const pthread_attr_t *attr,
			  void *(*start_routine) (void *), void *arg);
int __real_pthread_create(pthread_t *thread, const pthread_attr_t *attr,
			  void *(*start_routine) (void *),  void *arg);

int __wrap_pthread_create(pthread_t *thread, const pthread_attr_t *attr,
			  void *(*start_routine) (void *), void *arg)
{
	int error;

	error = mock_type(int);
	if (error != 0) {
		return error;
	}

	return __real_pthread_create(thread, attr, start_routine, arg);
}

static void test_job_threadid(void *ptr)
{
	pthread_t *threadid = ptr;

	*threadid = pthread_self();
}

static int test_create_do(struct tevent_context *ev,
			  struct pthreadpool_tevent *pool,
			  bool *executed,
			  bool *in_main_thread)
{
	struct tevent_req *req;
	pthread_t zero_thread;
	pthread_t main_thread;
	pthread_t worker_thread;
	bool ok;
	int ret;

	*executed = false;
	*in_main_thread = false;

	memset(&zero_thread, 0, sizeof(zero_thread));
	main_thread = pthread_self();
	worker_thread = zero_thread;

	req = pthreadpool_tevent_job_send(
		ev, ev, pool, test_job_threadid, &worker_thread);
	if (req == NULL) {
		fprintf(stderr, "pthreadpool_tevent_job_send failed\n");
		return ENOMEM;
	}

	ok = tevent_req_poll(req, ev);
	if (!ok) {
		ret = errno;
		fprintf(stderr, "tevent_req_poll failed: %s\n",
			strerror(ret));
		*executed = !pthread_equal(worker_thread, zero_thread);
		*in_main_thread = pthread_equal(worker_thread, main_thread);
		return ret;
	}


	ret = pthreadpool_tevent_job_recv(req);
	TALLOC_FREE(req);
	*executed = !pthread_equal(worker_thread, zero_thread);
	*in_main_thread = pthread_equal(worker_thread, main_thread);
	if (ret != 0) {
		fprintf(stderr, "tevent_req_recv failed: %s\n",
			strerror(ret));
		return ret;
	}

	return 0;
}

static void test_create(void **state)
{
	struct pthreadpool_tevent_test *t = *state;
	bool executed;
	bool in_main_thread;
	int ret;

	/*
	 * When pthreadpool cannot create the first worker thread,
	 * this job will run in the sync fallback in the main thread.
	 */
	will_return(__wrap_pthread_create, EAGAIN);
	ret = test_create_do(t->ev, t->upool, &executed, &in_main_thread);
	assert_int_equal(ret, EAGAIN);
	assert_false(executed);
	assert_false(in_main_thread);

	/*
	 * The sync pool won't trigger pthread_create()
	 * It will be triggered by the one pool.
	 */
	will_return(__wrap_pthread_create, EAGAIN);

	ret = test_create_do(t->ev, t->spool, &executed, &in_main_thread);
	assert_int_equal(ret, 0);
	assert_true(executed);
	assert_true(in_main_thread);

	ret = test_create_do(t->ev, t->opool, &executed, &in_main_thread);
	assert_int_equal(ret, EAGAIN);
	assert_false(executed);
	assert_false(in_main_thread);

	/*
	 * When a thread can be created, the job will run in the worker thread.
	 */
	will_return(__wrap_pthread_create, 0);
	ret = test_create_do(t->ev, t->upool, &executed, &in_main_thread);
	assert_int_equal(ret, 0);
	assert_true(executed);
	assert_false(in_main_thread);

	poll(NULL, 0, 10);

	/*
	 * Workerthread will still be active for a second; immediately
	 * running another job will also use the worker thread, even
	 * if a new thread cannot be created.
	 */
	ret = test_create_do(t->ev, t->upool, &executed, &in_main_thread);
	assert_int_equal(ret, 0);
	assert_true(executed);
	assert_false(in_main_thread);

	/*
	 * When a thread can be created, the job will run in the worker thread.
	 */
	will_return(__wrap_pthread_create, 0);
	ret = test_create_do(t->ev, t->opool, &executed, &in_main_thread);
	assert_int_equal(ret, 0);
	assert_true(executed);
	assert_false(in_main_thread);

	poll(NULL, 0, 10);

	/*
	 * Workerthread will still be active for a second; immediately
	 * running another job will also use the worker thread, even
	 * if a new thread cannot be created.
	 */
	ret = test_create_do(t->ev, t->opool, &executed, &in_main_thread);
	assert_int_equal(ret, 0);
	assert_true(executed);
	assert_false(in_main_thread);
}

struct test_cancel_job {
	int fdm; /* the main end of socketpair */
	int fdj; /* the job end of socketpair */
	bool started;
	bool canceled;
	bool orphaned;
	bool finished;
	size_t polls;
	size_t timeouts;
	int sleep_msec;
	struct tevent_req *req;
	bool completed;
	int ret;
};

static void test_cancel_job_done(struct tevent_req *req);

static int test_cancel_job_destructor(struct test_cancel_job *job)
{
	ANNOTATE_BENIGN_RACE_SIZED(&job->started,
				   sizeof(job->started),
				   "protected by pthreadpool_tevent code");
	if (job->started) {
		ANNOTATE_BENIGN_RACE_SIZED(&job->finished,
					   sizeof(job->finished),
					   "protected by pthreadpool_tevent code");
		assert_true(job->finished);
	}

	ANNOTATE_BENIGN_RACE_SIZED(&job->fdj,
				   sizeof(job->fdj),
				   "protected by pthreadpool_tevent code");

	if (job->fdm != -1) {
		close(job->fdm);
		job->fdm = -1;
	}
	if (job->fdj != -1) {
		close(job->fdj);
		job->fdj = -1;
	}

	return 0;
}

static struct test_cancel_job *test_cancel_job_create(TALLOC_CTX *mem_ctx)
{
	struct test_cancel_job *job = NULL;

	job = talloc(mem_ctx, struct test_cancel_job);
	if (job == NULL) {
		return NULL;
	}
	*job = (struct test_cancel_job) {
		.fdm = -1,
		.fdj = -1,
		.sleep_msec = 50,
	};

	talloc_set_destructor(job, test_cancel_job_destructor);
	return job;
}

static void test_cancel_job_fn(void *ptr)
{
	struct test_cancel_job *job = (struct test_cancel_job *)ptr;
	int fdj = -1;
	char c = 0;
	int ret;

	assert_non_null(job); /* make sure we abort without a job pointer */

	job->started = true;
	fdj = job->fdj;
	job->fdj = -1;

	if (!pthreadpool_tevent_current_job_continue()) {
		job->canceled = pthreadpool_tevent_current_job_canceled();
		job->orphaned = pthreadpool_tevent_current_job_orphaned();
		job->finished = true;
		close(fdj);
		return;
	}

	/*
	 * Notify that we main thread
	 *
	 * write of 1 byte should always work!
	 */
	ret = write(fdj, &c, 1);
	assert_int_equal(ret, 1);

	/*
	 * loop until the job was tried to
	 * be canceled or becomes orphaned.
	 *
	 * If there's some activity on the fd
	 * we directly finish.
	 */
	do {
		struct pollfd pfd = {
			.fd = fdj,
			.events = POLLIN,
		};

		job->polls += 1;

		ret = poll(&pfd, 1, job->sleep_msec);
		if (ret == 1) {
			job->finished = true;
			close(fdj);
			return;
		}
		assert_int_equal(ret, 0);

		job->timeouts += 1;

	} while (pthreadpool_tevent_current_job_continue());

	job->canceled = pthreadpool_tevent_current_job_canceled();
	job->orphaned = pthreadpool_tevent_current_job_orphaned();
	job->finished = true;
	close(fdj);
}

static void test_cancel_job_done(struct tevent_req *req)
{
	struct test_cancel_job *job =
		tevent_req_callback_data(req,
		struct test_cancel_job);

	job->ret = pthreadpool_tevent_job_recv(job->req);
	TALLOC_FREE(job->req);
	job->completed = true;
}

static void test_cancel_job_wait(struct test_cancel_job *job,
				 struct tevent_context *ev)
{
	/*
	 * We have to keep looping until
	 * test_cancel_job_done was triggered
	 */
	while (!job->completed) {
		int ret;

		ret = tevent_loop_once(ev);
		assert_int_equal(ret, 0);
	}
}

struct test_cancel_state {
	struct test_cancel_job *job1;
	struct test_cancel_job *job2;
	struct test_cancel_job *job3;
	struct test_cancel_job *job4;
	struct test_cancel_job *job5;
	struct test_cancel_job *job6;
};

static void test_cancel_job(void **private_data)
{
	struct pthreadpool_tevent_test *t = *private_data;
	struct tevent_context *ev = t->ev;
	struct pthreadpool_tevent *pool = t->opool;
	struct test_cancel_state *state = NULL;
	int ret;
	bool ok;
	int fdpair[2] = { -1, -1 };
	char c = 0;

	state = talloc_zero(t, struct test_cancel_state);
	assert_non_null(state);
	state->job1 = test_cancel_job_create(state);
	assert_non_null(state->job1);
	state->job2 = test_cancel_job_create(state);
	assert_non_null(state->job2);
	state->job3 = test_cancel_job_create(state);
	assert_non_null(state->job3);

	ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair);
	assert_int_equal(ret, 0);

	state->job1->fdm = fdpair[0];
	state->job1->fdj = fdpair[1];

	assert_int_equal(pthreadpool_tevent_queued_jobs(pool), 0);

	will_return(__wrap_pthread_create, 0);
	state->job1->req = pthreadpool_tevent_job_send(
		state->job1, ev, pool, test_cancel_job_fn, state->job1);
	assert_non_null(state->job1->req);
	tevent_req_set_callback(state->job1->req,
				test_cancel_job_done,
				state->job1);

	state->job2->req = pthreadpool_tevent_job_send(
		state->job2, ev, pool, test_cancel_job_fn, NULL);
	assert_non_null(state->job2->req);
	tevent_req_set_callback(state->job2->req,
				test_cancel_job_done,
				state->job2);

	state->job3->req = pthreadpool_tevent_job_send(
		state->job3, ev, pool, test_cancel_job_fn, NULL);
	assert_non_null(state->job3->req);
	tevent_req_set_callback(state->job3->req,
				test_cancel_job_done,
				state->job3);

	/*
	 * Wait for the job 1 to start.
	 */
	ret = read(state->job1->fdm, &c, 1);
	assert_int_equal(ret, 1);

	/*
	 * We cancel job 3 and destroy job2.
	 * Both should never be executed.
	 */
	assert_int_equal(pthreadpool_tevent_queued_jobs(pool), 2);
	TALLOC_FREE(state->job2->req);
	assert_int_equal(pthreadpool_tevent_queued_jobs(pool), 1);
	ok = tevent_req_cancel(state->job3->req);
	assert_true(ok);
	assert_int_equal(pthreadpool_tevent_queued_jobs(pool), 0);

	/*
	 * Job 3 should complete as canceled, while
	 * job 1 is still running.
	 */
	test_cancel_job_wait(state->job3, ev);
	assert_int_equal(state->job3->ret, ECANCELED);
	assert_null(state->job3->req);
	assert_false(state->job3->started);

	/*
	 * Now job1 is canceled while it's running,
	 * this should let it stop it's loop.
	 */
	ok = tevent_req_cancel(state->job1->req);
	assert_false(ok);

	/*
	 * Job 1 completes, It got at least one sleep
	 * timeout loop and has state->job1->canceled set.
	 */
	test_cancel_job_wait(state->job1, ev);
	assert_int_equal(state->job1->ret, 0);
	assert_null(state->job1->req);
	assert_true(state->job1->started);
	assert_true(state->job1->finished);
	assert_true(state->job1->canceled);
	assert_false(state->job1->orphaned);
	assert_in_range(state->job1->polls, 1, 100);
	assert_int_equal(state->job1->timeouts, state->job1->polls);

	/*
	 * Now we create jobs 4 and 5
	 * Both should execute.
	 * Job 4 is orphaned while running by a TALLOC_FREE()
	 * This should stop job 4 and let job 5 start.
	 * We do a "normal" exit in job 5 by creating some activity
	 * on the socketpair.
	 */

	state->job4 = test_cancel_job_create(state);
	assert_non_null(state->job4);

	ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair);
	assert_int_equal(ret, 0);

	state->job4->fdm = fdpair[0];
	state->job4->fdj = fdpair[1];

	state->job4->req = pthreadpool_tevent_job_send(
		state->job4, ev, pool, test_cancel_job_fn, state->job4);
	assert_non_null(state->job4->req);
	tevent_req_set_callback(state->job4->req,
				test_cancel_job_done,
				state->job4);

	state->job5 = test_cancel_job_create(state);
	assert_non_null(state->job5);

	ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair);
	assert_int_equal(ret, 0);

	state->job5->fdm = fdpair[0];
	state->job5->fdj = fdpair[1];

	state->job5->req = pthreadpool_tevent_job_send(
		state->job5, ev, pool, test_cancel_job_fn, state->job5);
	assert_non_null(state->job5->req);
	tevent_req_set_callback(state->job5->req,
				test_cancel_job_done,
				state->job5);

	/*
	 * Make sure job 5 can exit as soon as possible.
	 * It will never get a sleep/poll timeout.
	 */
	ret = write(state->job5->fdm, &c, 1);
	assert_int_equal(ret, 1);

	/*
	 * Wait for the job 4 to start
	 */
	ret = read(state->job4->fdm, &c, 1);
	assert_int_equal(ret, 1);

	assert_int_equal(pthreadpool_tevent_queued_jobs(pool), 1);

	/*
	 * destroy the request so that it's marked
	 * as orphaned.
	 */
	TALLOC_FREE(state->job4->req);

	/*
	 * Job 5 completes, It got no sleep timeout loop.
	 */
	test_cancel_job_wait(state->job5, ev);
	assert_int_equal(state->job5->ret, 0);
	assert_null(state->job5->req);
	assert_true(state->job5->started);
	assert_true(state->job5->finished);
	assert_false(state->job5->canceled);
	assert_false(state->job5->orphaned);
	assert_int_equal(state->job5->polls, 1);
	assert_int_equal(state->job5->timeouts, 0);

	assert_int_equal(pthreadpool_tevent_queued_jobs(pool), 0);

	/*
	 * Job 2 is still not executed as we did a TALLOC_FREE()
	 * before is was scheduled.
	 */
	assert_false(state->job2->completed);
	assert_false(state->job2->started);

	/*
	 * Job 4 is still wasn't completed as we did a TALLOC_FREE()
	 * while it is was running. but it was started and has
	 * orphaned set
	 */
	assert_false(state->job4->completed);
	assert_true(state->job4->started);
	assert_true(state->job4->finished);
	assert_false(state->job4->canceled);
	assert_true(state->job4->orphaned);
	assert_in_range(state->job4->polls, 1, 100);
	assert_int_equal(state->job4->timeouts, state->job4->polls);

	/*
	 * Now we create jobs 6
	 * We destroy the pool while it's executing.
	 */

	state->job6 = test_cancel_job_create(state);
	assert_non_null(state->job6);

	ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair);
	assert_int_equal(ret, 0);

	state->job6->fdm = fdpair[0];
	state->job6->fdj = fdpair[1];

	state->job6->req = pthreadpool_tevent_job_send(
		state->job6, ev, pool, test_cancel_job_fn, state->job6);
	assert_non_null(state->job6->req);
	tevent_req_set_callback(state->job6->req,
				test_cancel_job_done,
				state->job6);

	/*
	 * Wait for the job 6 to start
	 */
	ret = read(state->job6->fdm, &c, 1);
	assert_int_equal(ret, 1);

	assert_int_equal(pthreadpool_tevent_queued_jobs(pool), 0);

	/*
	 * destroy the request so that it's marked
	 * as orphaned.
	 */
	pool = NULL;
	TALLOC_FREE(t->opool);

	/*
	 * Wait until the job finished.
	 */
	ret = read(state->job6->fdm, &c, 1);
	assert_int_equal(ret, 0);

	/*
	 * Job 6 is still dangling arround.
	 *
	 * We need to convince valgrind --tool={drd,helgrind}
	 * that the read above is good enough to be
	 * sure the job is finished and closed the other end of
	 * the socketpair.
	 */
	ANNOTATE_BENIGN_RACE_SIZED(state->job6,
				   sizeof(*state->job6),
				   "protected by thread fence");
	assert_non_null(state->job6->req);
	assert_true(tevent_req_is_in_progress(state->job6->req));
	assert_false(state->job6->completed);
	assert_true(state->job6->started);
	assert_true(state->job6->finished);
	assert_false(state->job6->canceled);
	assert_true(state->job6->orphaned);
	assert_in_range(state->job6->polls, 1, 100);
	assert_int_equal(state->job6->timeouts, state->job4->polls);

	TALLOC_FREE(state);
}

int main(int argc, char **argv)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test_setup_teardown(test_create,
						setup_pthreadpool_tevent,
						teardown_pthreadpool_tevent),
		cmocka_unit_test_setup_teardown(test_cancel_job,
						setup_pthreadpool_tevent,
						teardown_pthreadpool_tevent),
	};

	cmocka_set_message_output(CM_OUTPUT_SUBUNIT);

	return cmocka_run_group_tests(tests, NULL, NULL);
}