FOSSology  4.4.0
Open Source License Compliance by Open Source Software
testEvent.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2011-2013 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
11 /* include functions to test */
12 #include <testRun.h>
13 #include <event.h>
14 
15 /* ************************************************************************** */
16 /* **** local declarations ************************************************** */
17 /* ************************************************************************** */
18 
19 extern struct event_loop_internal vl_singleton;
20 extern int el_created;
21 
23 
24 void* sample_args;
25 int call_num;
26 int samp_num;
27 char* s_name = NULL;
28 uint16_t s_line = 0;
29 
35 void sample_event(scheduler_t* scheduler, void* args)
36 {
37  samp_num++;
38 }
39 
45 void other_event(void* args)
46 {
47  samp_num--;
48 }
49 
55 void sample_callback(scheduler_t* scheduler)
56 {
57  call_num++;
58 }
59 
65 void terminate_event(void* args)
66 {
68 }
69 
70 /* ************************************************************************** */
71 /* **** meta agent function tests ******************************************* */
72 /* ************************************************************************** */
84 {
85  FO_ASSERT_FALSE(el_created);
86 
87  void* vl_addr = &vl_singleton;
89 
90  FO_ASSERT_PTR_EQUAL(vl, vl_addr);
91  FO_ASSERT_TRUE(el_created);
92  FO_ASSERT_PTR_NOT_NULL(vl->queue);
93  FO_ASSERT_FALSE(vl->occupied);
94  FO_ASSERT_FALSE(vl->terminated);
95 
97 
98  FO_ASSERT_PTR_EQUAL(ol, vl_addr);
99  FO_ASSERT_PTR_EQUAL(vl, ol);
100 }
101 
109 {
110  scheduler_t* scheduler;
111  scheduler = scheduler_init(testdb, NULL);
112  scheduler_foss_config(scheduler);
113 
114  event_t* e;
115 
117  e = event_init(sample_event, sample_args, "sample", s_name, s_line);
118 
119  FO_ASSERT_PTR_EQUAL( e->func, sample_event);
120  FO_ASSERT_PTR_EQUAL( e->argument, sample_args);
121  FO_ASSERT_STRING_EQUAL(e->name, "sample");
122 
123  g_free(e);
124  scheduler_destroy(scheduler);
125 }
126 
134 {
135  scheduler_t* scheduler;
136  scheduler = scheduler_init(testdb, NULL);
137  scheduler_foss_config(scheduler);
138 
139  event_t* e;
140 
143 
144  e = g_async_queue_pop(event_loop_get()->queue);
145 
146  FO_ASSERT_PTR_EQUAL( e->func, sample_event);
147  FO_ASSERT_PTR_EQUAL( e->argument, sample_args);
148  FO_ASSERT_STRING_EQUAL(e->name, "sample");
149 
150  g_free(e);
151  scheduler_destroy(scheduler);
152 }
153 
163 {
164  scheduler_t* scheduler;
165  scheduler = scheduler_init(testdb, NULL);
166  scheduler_foss_config(scheduler);
167  event_t* e;
168 
170  event_signal(sample_event, sample_args);
171 
172  e = g_async_queue_pop(event_loop_get()->queue);
173 
174  FO_ASSERT_PTR_EQUAL( e->func, sample_event);
175  FO_ASSERT_PTR_EQUAL( e->argument, sample_args);
176  FO_ASSERT_STRING_EQUAL(e->name, "sample_event");
177 
178  g_free(e);
179  scheduler_destroy(scheduler);
180 }
181 
190 {
191  scheduler_t* scheduler;
192  scheduler = scheduler_init(testdb, NULL);
193  scheduler_agent_config(scheduler);
194 
196  int retval = 0;
197 
198  call_num = 0;
199  samp_num = 0;
200  event_signal(NULL, NULL);
201  event_signal(terminate_event, NULL);
202 
203  retval = event_loop_enter(scheduler, sample_callback, NULL);
204  FO_ASSERT_EQUAL(retval, 0x0);
205  FO_ASSERT_EQUAL(call_num,1);
206  FO_ASSERT_TRUE(vl->terminated);
207  FO_ASSERT_FALSE(vl->occupied);
208 
209  event_signal(sample_event, NULL);
210  event_signal(terminate_event, NULL);
211 
212  retval = event_loop_enter(scheduler, sample_callback, NULL);
213  FO_ASSERT_EQUAL(retval, 0x0);
214  FO_ASSERT_EQUAL(samp_num, 1);
215  FO_ASSERT_EQUAL(call_num, 3);
216 
217  vl->occupied = 0;
218  vl->terminated = 0;
219  event_signal(terminate_event, NULL);
220 
221  retval = event_loop_enter(scheduler, sample_callback, NULL);
222  FO_ASSERT_EQUAL(retval, 0x0);
223  FO_ASSERT_EQUAL(samp_num, 1);
224  FO_ASSERT_EQUAL(call_num, 4);
225 
226  vl->occupied = 0;
227  vl->terminated = 0;
228  samp_num = 0;
229  call_num = 0;
230 
231  event_signal(sample_event, NULL);
232  event_signal(sample_event, NULL);
233  event_signal(other_event, NULL);
234  event_signal(sample_event, NULL);
235  event_signal(sample_event, NULL);
236  event_signal(other_event, NULL);
237  event_signal(terminate_event, NULL);
238 
239  retval = event_loop_enter(scheduler, sample_callback, NULL);
240  FO_ASSERT_EQUAL(retval, 0x0);
241  FO_ASSERT_EQUAL(samp_num, 2);
242  FO_ASSERT_EQUAL(call_num, 7);
243 
244  vl->occupied = 0;
245  vl->terminated = 1;
246  samp_num = 0;
247  call_num = 0;
248 
249  event_signal(sample_event, NULL);
250  event_signal(sample_event, NULL);
251  event_signal(other_event, NULL);
252  event_signal(sample_event, NULL);
253  event_signal(sample_event, NULL);
254  event_signal(other_event, NULL);
255  event_signal(terminate_event, NULL);
256 
257  retval = event_loop_enter(scheduler, sample_callback, NULL);
258  FO_ASSERT_EQUAL(retval, 0x0);
259  FO_ASSERT_EQUAL(samp_num, 2);
260  FO_ASSERT_EQUAL(call_num, 7);
261 
262  scheduler_destroy(scheduler);
263 }
264 
275 {
276  int retval = 0;
277  scheduler_t* scheduler;
278  scheduler = scheduler_init(testdb, NULL);
279  scheduler_foss_config(scheduler);
281 
282  vl->occupied = 0;
283  vl->terminated = 1;
284 
285  event_signal(terminate_event, NULL);
286  retval = event_loop_enter(scheduler, NULL, NULL);
287  FO_ASSERT_EQUAL(retval, 0x0);
288  FO_ASSERT_FALSE(vl->occupied);
289  FO_ASSERT_TRUE(vl->terminated);
290 
291  scheduler_destroy(scheduler);
292 }
293 
303 {
304  event_t* retval;
305  scheduler_t* scheduler;
306  scheduler = scheduler_init(testdb, NULL);
307  scheduler_foss_config(scheduler);
309 
310  vl->occupied = 0;
311  vl->terminated = 1;
312 
313  retval = event_loop_take(vl);
314  FO_ASSERT_PTR_NULL(retval);
315  FO_ASSERT_FALSE(vl->occupied);
316  FO_ASSERT_TRUE(vl->terminated);
317 
318  scheduler_destroy(scheduler);
319 }
320 
329 {
330  scheduler_t* scheduler;
331  scheduler = scheduler_init(testdb, NULL);
332  scheduler_foss_config(scheduler);
333  event_t* e;
334 
336  event_signal(sample_event, sample_args);
337 
338  e = g_async_queue_pop(event_loop_get()->queue);
339 
340  FO_ASSERT_PTR_EQUAL( e->func, sample_event);
341  FO_ASSERT_PTR_EQUAL( e->argument, sample_args);
342  FO_ASSERT_STRING_EQUAL(e->name, "sample_event");
343 
345  scheduler_destroy(scheduler);
346 }
347 
348 /* ************************************************************************** */
349 /* *** suite decl *********************************************************** */
350 /* ************************************************************************** */
351 
352 CU_TestInfo tests_event[] =
353 {
354  {"Test event_loop_get", test_event_loop_get },
355  {"Test event_init", test_event_init },
356  {"Test event_signal_ext", test_event_signal_ext },
357  {"Test event_signal", test_event_signal },
358  //{"Test event_loop_enter", test_event_loop_enter },
359  {"Test event_loop_terminate", test_event_loop_terminate },
360  {"Test event_loop_take", test_event_loop_take },
361  {"Test event_loop_put", test_event_loop_put },
362  CU_TEST_INFO_NULL
363 };
int event_loop_put(event_loop_t *event_loop, event_t *e)
Definition: event.c:75
void event_loop_terminate()
Stops the event loop from executing.
Definition: event.c:275
void event_signal_ext(void *func, void *args, char *name, char *s_name, uint16_t s_line)
Definition: event.c:200
event_t * event_loop_take(event_loop_t *event_loop)
Definition: event.c:90
int event_loop_enter(scheduler_t *scheduler, void(*update_call)(scheduler_t *), void(*signal_call)(scheduler_t *))
Enters the event loop.
Definition: event.c:221
event_t * event_init(void(*func)(scheduler_t *, void *), void *arg, char *name, char *source_name, uint16_t source_line)
Definition: event.c:147
Event handling operations.
void scheduler_agent_config(scheduler_t *scheduler)
Loads a particular agents configuration file.
Definition: scheduler.c:743
scheduler_t * scheduler_init(gchar *sysconfigdir, log_t *log)
Create a new scheduler object.
Definition: scheduler.c:249
void scheduler_foss_config(scheduler_t *scheduler)
Loads the configuration data from fossology.conf.
Definition: scheduler.c:853
void scheduler_destroy(scheduler_t *scheduler)
Free any memory associated with a scheduler_t.
Definition: scheduler.c:362
GAsyncQueue * queue
The queue that is the core of the event loop.
Definition: event.h:34
int terminated
Flag that signals the end of the event loop.
Definition: event.h:35
int occupied
Does this loop already have a worker thread.
Definition: event.h:36
Definition: event.h:24
char * name
Name of the event, used for debugging.
Definition: event.h:27
void(* func)(scheduler_t *, void *)
The function that will be executed for this event.
Definition: event.h:25
void * argument
The arguments for the function.
Definition: event.h:26
struct event_loop_internal vl_singleton
Definition: event.c:29
void test_event_init()
Test for event_init()
Definition: testEvent.c:108
void test_event_loop_put()
Test for event_loop_put()
Definition: testEvent.c:328
void test_event_signal()
Test for event_signal()
Definition: testEvent.c:162
event_loop_t * event_loop_get()
Definition: event.c:48
void test_event_loop_terminate()
Test for event_loop_enter() with terminate_event.
Definition: testEvent.c:274
void sample_callback(scheduler_t *scheduler)
Sample callback function.
Definition: testEvent.c:55
void test_event_loop_enter()
Test for event_loop_enter()
Definition: testEvent.c:189
int samp_num
Number of sample event calls.
Definition: testEvent.c:26
char * s_name
Sample source file name.
Definition: testEvent.c:27
void sample_event(scheduler_t *scheduler, void *args)
Sample event.
Definition: testEvent.c:35
int call_num
Number of callback calls.
Definition: testEvent.c:25
int el_created
Definition: event.c:35
void * sample_args
Sample args to pass.
Definition: testEvent.c:24
void other_event(void *args)
Sample event 2.
Definition: testEvent.c:45
void test_event_signal_ext()
Test for event_signal_ext()
Definition: testEvent.c:133
void test_event_loop_take()
Test for event_loop_take()
Definition: testEvent.c:302
uint16_t s_line
Sample source line number.
Definition: testEvent.c:28
void terminate_event(void *args)
Terminate event.
Definition: testEvent.c:65
void test_event_loop_get()
Test for event_loop_get()
Definition: testEvent.c:83