- Tests are now separated in test files in the tests-directory
[strongswan.git] / Source / charon / tests / job_queue_test.c
1 /**
2 * @file job_queue_test.c
3 *
4 * @brief Tests to test the Job-Queue type job_queue_t
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23
24 //#include <stdlib.h>
25 #include <freeswan.h>
26 #include <pluto/constants.h>
27 #include <pluto/defs.h>
28 #include <pthread.h>
29
30 #include "../tester.h"
31 #include "../job_queue.h"
32
33
34 typedef struct job_queue_test_s job_queue_test_t;
35
36 struct job_queue_test_s{
37 tester_t *tester;
38 job_queue_t *job_queue;
39 int max_queue_item_count;
40 };
41
42 /**
43 * @brief sender thread used in the the job_queue test function
44 */
45 static void test_job_queue_sender(job_queue_test_t * testinfo)
46 {
47
48 int i;
49
50 for (i = 0; i < testinfo->max_queue_item_count; i++)
51 {
52 int *value = alloc_thing(int,"int");
53 *value = i;
54 job_t *job = job_create(INCOMING_PACKET,value);
55 testinfo->job_queue->add(testinfo->job_queue,job);
56 }
57 }
58
59 /**
60 * @brief receiver thread used in the the job_queue test function
61 */
62 static void test_job_queue_receiver(job_queue_test_t * testinfo)
63 {
64 int i;
65
66 for (i = 0; i < testinfo->max_queue_item_count; i++)
67 {
68 job_t *job;
69 testinfo->tester->assert_true(testinfo->tester,(testinfo->job_queue->get(testinfo->job_queue,&job) == SUCCESS), "get job call check");
70 testinfo->tester->assert_true(testinfo->tester,(job->type == INCOMING_PACKET), "job type check");
71 testinfo->tester->assert_true(testinfo->tester,((*((int *) (job->assigned_data))) == i), "job value check");
72
73 pfree(job->assigned_data);
74 testinfo->tester->assert_true(testinfo->tester,(job->destroy(job) == SUCCESS), "job destroy call check");
75 }
76 }
77
78 /*
79 *
80 * description is in header file
81 */
82 void test_job_queue(tester_t *tester)
83 {
84 int value = 1000;
85 pthread_t sender_thread, receiver_thread;
86 job_queue_t *job_queue = job_queue_create();
87 job_queue_test_t test_infos;
88 test_infos.tester = tester;
89 test_infos.job_queue = job_queue;
90 test_infos.max_queue_item_count = 100000;
91
92 pthread_create( &receiver_thread, NULL,(void*(*)(void*)) &test_job_queue_receiver, (void*) &test_infos);
93 pthread_create( &sender_thread, NULL,(void*(*)(void*)) &test_job_queue_sender, (void*) &test_infos);
94
95 pthread_join(sender_thread, NULL);
96 pthread_join(receiver_thread, NULL);
97
98 tester->assert_true(tester,(job_queue->get_count(job_queue,&value) == SUCCESS), "get count call check");
99 tester->assert_true(tester,(value == 0), "get count value check");
100 tester->assert_true(tester,(job_queue->destroy(job_queue) == SUCCESS), "destroy call check");
101 }