FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
testEvent.c
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright (C) 2011~2013 Hewlett-Packard Development Company, L.P.
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 *********************************************************************/
22 /* include functions to test */
23 #include <testRun.h>
24 #include <event.h>
25 
26 /* ************************************************************************** */
27 /* **** local declarations ************************************************** */
28 /* ************************************************************************** */
29 
30 extern struct event_loop_internal vl_singleton;
31 extern int el_created;
32 
34 
35 void* sample_args;
36 int call_num;
37 int samp_num;
38 char* s_name = NULL;
39 uint16_t s_line = 0;
40 
46 void sample_event(scheduler_t* scheduler, void* args)
47 {
48  samp_num++;
49 }
50 
56 void other_event(void* args)
57 {
58  samp_num--;
59 }
60 
66 void sample_callback(scheduler_t* scheduler)
67 {
68  call_num++;
69 }
70 
76 void terminate_event(void* args)
77 {
79 }
80 
81 /* ************************************************************************** */
82 /* **** meta agent function tests ******************************************* */
83 /* ************************************************************************** */
95 {
96  FO_ASSERT_FALSE(el_created);
97 
98  void* vl_addr = &vl_singleton;
100 
101  FO_ASSERT_PTR_EQUAL(vl, vl_addr);
102  FO_ASSERT_TRUE(el_created);
103  FO_ASSERT_PTR_NOT_NULL(vl->queue);
104  FO_ASSERT_FALSE(vl->occupied);
105  FO_ASSERT_FALSE(vl->terminated);
106 
108 
109  FO_ASSERT_PTR_EQUAL(ol, vl_addr);
110  FO_ASSERT_PTR_EQUAL(vl, ol);
111 }
112 
120 {
121  scheduler_t* scheduler;
122  scheduler = scheduler_init(testdb, NULL);
123  scheduler_foss_config(scheduler);
124 
125  event_t* e;
126 
128  e = event_init(sample_event, sample_args, "sample", s_name, s_line);
129 
130  FO_ASSERT_PTR_EQUAL( e->func, sample_event);
131  FO_ASSERT_PTR_EQUAL( e->argument, sample_args);
132  FO_ASSERT_STRING_EQUAL(e->name, "sample");
133 
134  g_free(e);
135  scheduler_destroy(scheduler);
136 }
137 
145 {
146  scheduler_t* scheduler;
147  scheduler = scheduler_init(testdb, NULL);
148  scheduler_foss_config(scheduler);
149 
150  event_t* e;
151 
154 
155  e = g_async_queue_pop(event_loop_get()->queue);
156 
157  FO_ASSERT_PTR_EQUAL( e->func, sample_event);
158  FO_ASSERT_PTR_EQUAL( e->argument, sample_args);
159  FO_ASSERT_STRING_EQUAL(e->name, "sample");
160 
161  g_free(e);
162  scheduler_destroy(scheduler);
163 }
164 
174 {
175  scheduler_t* scheduler;
176  scheduler = scheduler_init(testdb, NULL);
177  scheduler_foss_config(scheduler);
178  event_t* e;
179 
181  event_signal(sample_event, sample_args);
182 
183  e = g_async_queue_pop(event_loop_get()->queue);
184 
185  FO_ASSERT_PTR_EQUAL( e->func, sample_event);
186  FO_ASSERT_PTR_EQUAL( e->argument, sample_args);
187  FO_ASSERT_STRING_EQUAL(e->name, "sample_event");
188 
189  g_free(e);
190  scheduler_destroy(scheduler);
191 }
192 
201 {
202  scheduler_t* scheduler;
203  scheduler = scheduler_init(testdb, NULL);
204  scheduler_agent_config(scheduler);
205 
207  int retval = 0;
208 
209  call_num = 0;
210  samp_num = 0;
211  event_signal(NULL, NULL);
212  event_signal(terminate_event, NULL);
213 
214  retval = event_loop_enter(scheduler, sample_callback, NULL);
215  FO_ASSERT_EQUAL(retval, 0x0);
216  FO_ASSERT_EQUAL(call_num,1);
217  FO_ASSERT_TRUE(vl->terminated);
218  FO_ASSERT_FALSE(vl->occupied);
219 
220  event_signal(sample_event, NULL);
221  event_signal(terminate_event, NULL);
222 
223  retval = event_loop_enter(scheduler, sample_callback, NULL);
224  FO_ASSERT_EQUAL(retval, 0x0);
225  FO_ASSERT_EQUAL(samp_num, 1);
226  FO_ASSERT_EQUAL(call_num, 3);
227 
228  vl->occupied = 0;
229  vl->terminated = 0;
230  event_signal(terminate_event, NULL);
231 
232  retval = event_loop_enter(scheduler, sample_callback, NULL);
233  FO_ASSERT_EQUAL(retval, 0x0);
234  FO_ASSERT_EQUAL(samp_num, 1);
235  FO_ASSERT_EQUAL(call_num, 4);
236 
237  vl->occupied = 0;
238  vl->terminated = 0;
239  samp_num = 0;
240  call_num = 0;
241 
242  event_signal(sample_event, NULL);
243  event_signal(sample_event, NULL);
244  event_signal(other_event, NULL);
245  event_signal(sample_event, NULL);
246  event_signal(sample_event, NULL);
247  event_signal(other_event, NULL);
248  event_signal(terminate_event, NULL);
249 
250  retval = event_loop_enter(scheduler, sample_callback, NULL);
251  FO_ASSERT_EQUAL(retval, 0x0);
252  FO_ASSERT_EQUAL(samp_num, 2);
253  FO_ASSERT_EQUAL(call_num, 7);
254 
255  vl->occupied = 0;
256  vl->terminated = 1;
257  samp_num = 0;
258  call_num = 0;
259 
260  event_signal(sample_event, NULL);
261  event_signal(sample_event, NULL);
262  event_signal(other_event, NULL);
263  event_signal(sample_event, NULL);
264  event_signal(sample_event, NULL);
265  event_signal(other_event, NULL);
266  event_signal(terminate_event, NULL);
267 
268  retval = event_loop_enter(scheduler, sample_callback, NULL);
269  FO_ASSERT_EQUAL(retval, 0x0);
270  FO_ASSERT_EQUAL(samp_num, 2);
271  FO_ASSERT_EQUAL(call_num, 7);
272 
273  scheduler_destroy(scheduler);
274 }
275 
286 {
287  int retval = 0;
288  scheduler_t* scheduler;
289  scheduler = scheduler_init(testdb, NULL);
290  scheduler_foss_config(scheduler);
292 
293  vl->occupied = 0;
294  vl->terminated = 1;
295 
296  event_signal(terminate_event, NULL);
297  retval = event_loop_enter(scheduler, NULL, NULL);
298  FO_ASSERT_EQUAL(retval, 0x0);
299  FO_ASSERT_FALSE(vl->occupied);
300  FO_ASSERT_TRUE(vl->terminated);
301 
302  scheduler_destroy(scheduler);
303 }
304 
314 {
315  event_t* retval;
316  scheduler_t* scheduler;
317  scheduler = scheduler_init(testdb, NULL);
318  scheduler_foss_config(scheduler);
320 
321  vl->occupied = 0;
322  vl->terminated = 1;
323 
324  retval = event_loop_take(vl);
325  FO_ASSERT_PTR_NULL(retval);
326  FO_ASSERT_FALSE(vl->occupied);
327  FO_ASSERT_TRUE(vl->terminated);
328 
329  scheduler_destroy(scheduler);
330 }
331 
340 {
341  scheduler_t* scheduler;
342  scheduler = scheduler_init(testdb, NULL);
343  scheduler_foss_config(scheduler);
344  event_t* e;
345 
347  event_signal(sample_event, sample_args);
348 
349  e = g_async_queue_pop(event_loop_get()->queue);
350 
351  FO_ASSERT_PTR_EQUAL( e->func, sample_event);
352  FO_ASSERT_PTR_EQUAL( e->argument, sample_args);
353  FO_ASSERT_STRING_EQUAL(e->name, "sample_event");
354 
356  scheduler_destroy(scheduler);
357 }
358 
359 /* ************************************************************************** */
360 /* *** suite decl *********************************************************** */
361 /* ************************************************************************** */
362 
363 CU_TestInfo tests_event[] =
364 {
365  {"Test event_loop_get", test_event_loop_get },
366  {"Test event_init", test_event_init },
367  {"Test event_signal_ext", test_event_signal_ext },
368  {"Test event_signal", test_event_signal },
369  //{"Test event_loop_enter", test_event_loop_enter },
370  {"Test event_loop_terminate", test_event_loop_terminate },
371  {"Test event_loop_take", test_event_loop_take },
372  {"Test event_loop_put", test_event_loop_put },
373  CU_TEST_INFO_NULL
374 };
int event_loop_put(event_loop_t *event_loop, event_t *e)
Definition: event.c:86
void sample_callback(scheduler_t *scheduler)
Sample callback function.
Definition: testEvent.c:66
void other_event(void *args)
Sample event 2.
Definition: testEvent.c:56
void terminate_event(void *args)
Terminate event.
Definition: testEvent.c:76
void test_event_loop_put()
Test for event_loop_put()
Definition: testEvent.c:339
char * name
Name of the event, used for debugging.
Definition: event.h:38
void scheduler_destroy(scheduler_t *scheduler)
Free any memory associated with a scheduler_t.
Definition: scheduler.c:373
int occupied
Does this loop already have a worker thread.
Definition: event.h:47
void * sample_args
Sample args to pass.
Definition: testEvent.c:35
event_t * event_init(void(*func)(scheduler_t *, void *), void *arg, char *name, char *source_name, uint16_t source_line)
Definition: event.c:153
void test_event_loop_enter()
Test for event_loop_enter()
Definition: testEvent.c:200
void event_loop_terminate()
Stops the event loop from executing.
Definition: event.c:281
void event_signal_ext(void *func, void *args, char *name, char *s_name, uint16_t s_line)
Definition: event.c:206
uint16_t s_line
Sample source line number.
Definition: testEvent.c:39
void sample_event(scheduler_t *scheduler, void *args)
Sample event.
Definition: testEvent.c:46
event_t * event_loop_take(event_loop_t *event_loop)
Definition: event.c:101
int call_num
Number of callback calls.
Definition: testEvent.c:36
void * argument
The arguments for the function.
Definition: event.h:37
void test_event_signal_ext()
Test for event_signal_ext()
Definition: testEvent.c:144
void(* func)(scheduler_t *, void *)
The function that will be executed for this event.
Definition: event.h:36
void test_event_loop_terminate()
Test for event_loop_enter() with terminate_event.
Definition: testEvent.c:285
void test_event_init()
Test for event_init()
Definition: testEvent.c:119
event_loop_t * event_loop_get()
Definition: event.c:59
void scheduler_agent_config(scheduler_t *scheduler)
Loads a particular agents configuration file.
Definition: scheduler.c:754
void scheduler_foss_config(scheduler_t *scheduler)
Loads the configuration data from fossology.conf.
Definition: scheduler.c:864
struct event_loop_internal vl_singleton
Definition: event.c:40
void test_event_signal()
Test for event_signal()
Definition: testEvent.c:173
scheduler_t * scheduler_init(gchar *sysconfigdir, log_t *log)
Create a new scheduler object.
Definition: scheduler.c:260
void test_event_loop_get()
Test for event_loop_get()
Definition: testEvent.c:94
int el_created
Definition: event.c:46
int samp_num
Number of sample event calls.
Definition: testEvent.c:37
Event handling operations.
Definition: event.h:35
GAsyncQueue * queue
The queue that is the core of the event loop.
Definition: event.h:45
char * s_name
Sample source file name.
Definition: testEvent.c:38
void test_event_loop_take()
Test for event_loop_take()
Definition: testEvent.c:313
int terminated
Flag that signals the end of the event loop.
Definition: event.h:46
int event_loop_enter(scheduler_t *scheduler, void(*update_call)(scheduler_t *), void(*signal_call)(scheduler_t *))
Enters the event loop.
Definition: event.c:227