b2d4376ff015444b385ecef322ef9578818de349
[strongswan.git] / Source / charon / testcases / event_queue_test.c
1 /**
2 * @file event_queue_test.h
3 *
4 * @brief Tests to test the Event-Queue type event_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 #include <stdlib.h>
24 #include <pthread.h>
25
26 #include "event_queue_test.h"
27
28 #include <utils/allocator.h>
29 #include <queues/event_queue.h>
30 #include <queues/jobs/initiate_ike_sa_job.h>
31
32 /**
33 * Number of different times to insert per thread
34 */
35 #define EVENT_QUEUE_TIMES 5
36 /**
37 * Number of entries per time per thread
38 */
39 #define EVENT_QUEUE_ENTRY_PER_TIME 20
40
41 /**
42 * Number of test-thread
43 */
44 #define EVENT_QUEUE_INSERT_THREADS 1
45
46 /**
47 * @brief Informations for the involved test-thread used in this test
48 *
49 */
50 typedef struct event_queue_test_s event_queue_test_t;
51
52 struct event_queue_test_s{
53 tester_t *tester;
54 event_queue_t *event_queue;
55
56 /**
57 * number of different event times to be inserted in the event-queue by each thread
58 */
59 int insert_times_count;
60
61 /**
62 * number of event to insert at one time
63 */
64 int entries_per_time;
65 };
66
67
68 static void event_queue_insert_thread(event_queue_test_t * testinfos)
69 {
70 timeval_t current_time;
71 tester_t *tester = testinfos->tester;
72 timeval_t time;
73 job_t * job;
74 int i,j;
75
76 gettimeofday(&current_time,NULL);
77 for (i = 0; i < testinfos->insert_times_count;i++)
78 {
79
80 for (j = 0; j < testinfos->entries_per_time;j++)
81 {
82 job = (job_t *) initiate_ike_sa_job_create("testvalue");
83 time.tv_usec = 0;
84 time.tv_sec = current_time.tv_sec + i;
85
86 tester->assert_true(tester,(testinfos->event_queue->add_absolute(testinfos->event_queue,job,time) == SUCCESS), "add call check");
87 }
88 }
89 }
90
91
92 void test_event_queue(tester_t *tester)
93 {
94 event_queue_t * event_queue = event_queue_create();
95 event_queue_test_t testinfos;
96 pthread_t threads[EVENT_QUEUE_INSERT_THREADS];
97 int i,j, number_of_total_events;
98 timeval_t current_time, start_time;
99
100 testinfos.tester = tester;
101 testinfos.event_queue = event_queue;
102 testinfos.insert_times_count = EVENT_QUEUE_TIMES;
103 testinfos.entries_per_time = EVENT_QUEUE_ENTRY_PER_TIME;
104
105 number_of_total_events = EVENT_QUEUE_ENTRY_PER_TIME * EVENT_QUEUE_TIMES * EVENT_QUEUE_INSERT_THREADS;
106
107 gettimeofday(&start_time,NULL);
108
109 for (i = 0; i < EVENT_QUEUE_INSERT_THREADS; i++)
110 {
111 int retval;
112 retval = pthread_create( &(threads[i]), NULL,(void*(*)(void*)) &event_queue_insert_thread, (void*) &testinfos);
113 tester->assert_true(tester,(retval== 0), "thread creation call check");
114 }
115
116
117 /* wait for all threads */
118 for (i = 0; i < EVENT_QUEUE_INSERT_THREADS; i++)
119 {
120 int retval;
121 retval = pthread_join(threads[i], NULL);
122 tester->assert_true(tester,(retval== 0), "thread creation call check");
123
124 }
125
126 tester->assert_true(tester,(event_queue->get_count(event_queue) == number_of_total_events), "event count check");
127
128 for (i = 0; i < EVENT_QUEUE_TIMES;i++)
129 {
130 for (j = 0; j < (EVENT_QUEUE_ENTRY_PER_TIME * EVENT_QUEUE_INSERT_THREADS);j++)
131 {
132 job_t *job;
133
134 tester->assert_true(tester,(event_queue->get(event_queue,&job) == SUCCESS), "get call check");
135 gettimeofday(&current_time,NULL);
136 tester->assert_true(tester,((current_time.tv_sec - start_time.tv_sec) == i), "value of entry check");
137 tester->assert_true(tester,(job->destroy(job) == SUCCESS), "job destroy call check");
138
139 }
140 }
141
142
143 tester->assert_true(tester,(event_queue->destroy(event_queue) == SUCCESS), "destroy call check");
144 return;
145 }