b06b16b008a31881084b59858c66ad6d79138876
[strongswan.git] / Source / charon / tests / 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 #include "../tester.h"
28 #include "../event_queue.h"
29
30 /**
31 * Number of different times to insert per thread
32 */
33 #define EVENT_QUEUE_TIMES 5
34 /**
35 * Number of entries per time per thread
36 */
37 #define EVENT_QUEUE_ENTRY_PER_TIME 20
38
39 /**
40 * Number of test-thread
41 */
42 #define EVENT_QUEUE_INSERT_THREADS 15
43
44 /**
45 * @brief Informations for the involved test-thread used in this test
46 *
47 */
48 typedef struct event_queue_test_s event_queue_test_t;
49
50 struct event_queue_test_s{
51 tester_t *tester;
52 event_queue_t *event_queue;
53
54 /**
55 * number of different event times to be inserted in the event-queue by each thread
56 */
57 int insert_times_count;
58
59 /**
60 * number of event to insert at one time
61 */
62 int entries_per_time;
63 };
64
65
66 static void event_queue_insert_thread(event_queue_test_t * testinfos)
67 {
68 timeval_t current_time;
69 tester_t *tester = testinfos->tester;
70 timeval_t time;
71 job_t * job;
72 int i,j;
73
74 gettimeofday(&current_time,NULL);
75 for (i = 0; i < testinfos->insert_times_count;i++)
76 {
77 for (j = 0; j < testinfos->entries_per_time;j++)
78 {
79 int *value = alloc_thing(int, "value");
80 *value = i;
81 job = job_create(INCOMING_PACKET,value);
82 time.tv_usec = 0;
83 time.tv_sec = current_time.tv_sec + i;
84
85 tester->assert_true(tester,(testinfos->event_queue->add_absolute(testinfos->event_queue,job,time) == SUCCESS), "add call check");
86 }
87 }
88 }
89
90
91 void test_event_queue(tester_t *tester)
92 {
93 event_queue_t * event_queue = event_queue_create();
94 event_queue_test_t testinfos;
95 pthread_t threads[EVENT_QUEUE_INSERT_THREADS];
96 int i,j, number_of_total_events;
97 int count;
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 pthread_create( &threads[i], NULL,(void*(*)(void*)) &event_queue_insert_thread, (void*) &testinfos);
112 }
113
114
115
116 /* wait for all threads */
117 for (i = 0; i < EVENT_QUEUE_INSERT_THREADS; i++)
118 {
119 pthread_join(threads[i], NULL);
120 }
121
122 tester->assert_true(tester,(event_queue->get_count(event_queue,&count) == SUCCESS), "get_count call check");
123 tester->assert_true(tester,(count == number_of_total_events), "event count check");
124
125 for (i = 0; i < EVENT_QUEUE_TIMES;i++)
126 {
127 for (j = 0; j < (EVENT_QUEUE_ENTRY_PER_TIME * EVENT_QUEUE_INSERT_THREADS);j++)
128 {
129 job_t *job;
130 tester->assert_true(tester,(event_queue->get(event_queue,&job) == SUCCESS), "get call check");
131 gettimeofday(&current_time,NULL);
132 tester->assert_true(tester,((current_time.tv_sec - start_time.tv_sec) == i), "value of entry check");
133
134 pfree(job->assigned_data);
135 tester->assert_true(tester,(job->destroy(job) == SUCCESS), "job destroy call check");
136 }
137 }
138
139
140 tester->assert_true(tester,(event_queue->destroy(event_queue) == SUCCESS), "destroy call check");
141 }