test_dthreads.c 3.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*  Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    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 <pthread.h>
#include <sched.h>
#include <signal.h>
#include <stdlib.h>
21
#include <tap/basic.h>
22 23 24 25 26 27 28 29 30

#include "knot/server/dthreads.h"

/* Unit runnable data. */
static pthread_mutex_t _runnable_mx;
static volatile int _runnable_i = 0;
static const int _runnable_cycles = 10000;

/*! \brief Unit runnable. */
31
int runnable(struct dthread *thread)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
{
	for (int i = 0; i < _runnable_cycles; ++i) {

		// Increase counter
		pthread_mutex_lock(&_runnable_mx);
		++_runnable_i;
		pthread_mutex_unlock(&_runnable_mx);

		// Cancellation point
		if (dt_is_cancelled(thread)) {
			break;
		}

		// Yield
		sched_yield();
	}

	return 0;
}

52
/* Destructor data. */
Marek Vavrusa's avatar
Marek Vavrusa committed
53
static volatile int _destructor_data = 0;
54 55 56
static pthread_mutex_t _destructor_mx;

/*! \brief Thread destructor. */
57
int destruct(struct dthread *thread)
58 59 60 61 62 63 64 65
{
	pthread_mutex_lock(&_destructor_mx);
	_destructor_data += 1;
	pthread_mutex_unlock(&_destructor_mx);

	return 0;
}

66 67 68 69 70 71 72 73
// Signal handler
static void interrupt_handle(int s)
{
}

/*! API: run tests. */
int main(int argc, char *argv[])
{
Marek Vavrusa's avatar
Marek Vavrusa committed
74
	plan(8);
75 76 77 78 79 80 81 82 83 84 85

	// Register service and signal handler
	struct sigaction sa;
	sa.sa_handler = interrupt_handle;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = 0;
	sigaction(SIGALRM, &sa, NULL); // Interrupt

	/* Initialize */
	srand(time(NULL));
	pthread_mutex_init(&_runnable_mx, NULL);
86
	pthread_mutex_init(&_destructor_mx, NULL);
87 88

	/* Test 1: Create unit */
89 90 91
	int size = 2;
	dt_unit_t *unit = dt_create(size, &runnable, NULL, NULL);
	ok(unit != NULL, "dthreads: create unit (size %d)", size);
Marek Vavrusa's avatar
Marek Vavrusa committed
92 93
	if (unit == NULL) {
		skip_block(7, "No dthreads unit");
94 95 96
		goto skip_all;
	}

Marek Vavrusa's avatar
Marek Vavrusa committed
97
	/* Test 2: Start tasks. */
98
	_runnable_i = 0;
Marek Vavrusa's avatar
Marek Vavrusa committed
99
	ok(dt_start(unit) == 0, "dthreads: start single task");
100

Marek Vavrusa's avatar
Marek Vavrusa committed
101 102
	/* Test 3: Wait for tasks. */
	ok(dt_join(unit) == 0, "dthreads: join threads");
103

Marek Vavrusa's avatar
Marek Vavrusa committed
104 105
	/* Test 4: Compare counter. */
	int expected = _runnable_cycles * 2;
106
	is_int(expected, _runnable_i, "dthreads: result ok");
107

Marek Vavrusa's avatar
Marek Vavrusa committed
108
	/* Test 5: Deinitialize */
109
	dt_delete(&unit);
110
	ok(unit == NULL, "dthreads: delete unit");
111

Marek Vavrusa's avatar
Marek Vavrusa committed
112 113
	/* Test 6: Wrong values. */
	unit = dt_create(-1, NULL, NULL, NULL);
114
	ok(unit == NULL, "dthreads: create with negative count");
115

Marek Vavrusa's avatar
Marek Vavrusa committed
116
	/* Test 7: NULL operations crashing. */
117
	int ret = 0;
Marek Vavrusa's avatar
Marek Vavrusa committed
118 119 120 121 122 123 124 125 126 127 128
	ret += dt_activate(0);
	ret += dt_cancel(0);
	ret += dt_compact(0);
	dt_delete(0);
	ret += dt_is_cancelled(0);
	ret += dt_join(0);
	ret += dt_signalize(0, SIGALRM);
	ret += dt_start(0);
	ret += dt_stop(0);
	ret += dt_unit_lock(0);
	ret += dt_unit_unlock(0);
129
	is_int(-198, ret, "dthreads: correct values when passed NULL context");
Marek Vavrusa's avatar
Marek Vavrusa committed
130 131

	/* Test 8: Thread destructor. */
132
	_destructor_data = 0;
Marek Vavrusa's avatar
Marek Vavrusa committed
133
	unit = dt_create(2, 0, destruct, 0);
134 135 136 137 138 139
	dt_start(unit);
	dt_stop(unit);
	dt_join(unit);
	is_int(2, _destructor_data, "dthreads: destructor with dt_create_coherent()");
	dt_delete(&unit);

140 141 142
skip_all:

	pthread_mutex_destroy(&_runnable_mx);
143
	pthread_mutex_destroy(&_destructor_mx);
144 145
	return 0;
}