2 * This file is part of the PCEPlib, a PCEP protocol library.
4 * Copyright (C) 2020 Volta Networks https://voltanet.io/
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * Author : Brady Johnson <brady@voltanet.io>
30 #include <CUnit/CUnit.h>
32 #include "pcep_timers.h"
33 #include "pcep_utils_memory.h"
34 #include "pcep_timers_event_loop.h"
35 #include "pcep_timers_event_loop_test.h"
38 typedef struct timer_expire_handler_info_
{
43 } timer_expire_handler_info
;
45 static pcep_timers_context
*test_timers_context
= NULL
;
46 static timer_expire_handler_info expire_handler_info
;
47 #define TEST_EVENT_LOOP_TIMER_ID 500
50 /* Called when a timer expires */
51 static void test_timer_expire_handler(void *data
, int timerId
)
53 expire_handler_info
.handler_called
= true;
54 expire_handler_info
.data
= data
;
55 expire_handler_info
.timerId
= timerId
;
59 /* Test case setup called before each test.
60 * Declared in pcep_timers_tests.c */
61 void pcep_timers_event_loop_test_setup()
64 pceplib_malloc(PCEPLIB_INFRA
, sizeof(pcep_timers_context
));
65 memset(test_timers_context
, 0, sizeof(pcep_timers_context
));
66 if (pthread_mutex_init(&(test_timers_context
->timer_list_lock
), NULL
)
69 "ERROR initializing timers, cannot initialize the mutex\n");
71 test_timers_context
->active
= false;
72 test_timers_context
->expire_handler
= test_timer_expire_handler
;
73 test_timers_context
->timer_list
=
74 ordered_list_initialize(timer_list_node_timer_id_compare
);
76 expire_handler_info
.handler_called
= false;
77 expire_handler_info
.data
= NULL
;
78 expire_handler_info
.timerId
= -1;
82 /* Test case teardown called after each test.
83 * Declared in pcep_timers_tests.c */
84 void pcep_timers_event_loop_test_teardown()
86 pthread_mutex_unlock(&test_timers_context
->timer_list_lock
);
87 pthread_mutex_destroy(&(test_timers_context
->timer_list_lock
));
88 ordered_list_destroy(test_timers_context
->timer_list
);
89 pceplib_free(PCEPLIB_INFRA
, test_timers_context
);
90 test_timers_context
= NULL
;
98 void test_walk_and_process_timers_no_timers()
100 CU_ASSERT_EQUAL(test_timers_context
->timer_list
->num_entries
, 0);
101 CU_ASSERT_PTR_NULL(test_timers_context
->timer_list
->head
);
103 walk_and_process_timers(test_timers_context
);
105 CU_ASSERT_FALSE(expire_handler_info
.handler_called
);
106 CU_ASSERT_EQUAL(test_timers_context
->timer_list
->num_entries
, 0);
107 CU_ASSERT_PTR_NULL(test_timers_context
->timer_list
->head
);
111 void test_walk_and_process_timers_timer_not_expired()
115 // Set the timer to expire 100 seconds from now
116 timer
.expire_time
= time(NULL
) + 100;
117 timer
.timer_id
= TEST_EVENT_LOOP_TIMER_ID
;
118 ordered_list_add_node(test_timers_context
->timer_list
, &timer
);
120 walk_and_process_timers(test_timers_context
);
122 /* The timer should still be in the list, since it hasnt expired yet */
123 CU_ASSERT_FALSE(expire_handler_info
.handler_called
);
124 CU_ASSERT_EQUAL(test_timers_context
->timer_list
->num_entries
, 1);
125 CU_ASSERT_PTR_NOT_NULL(test_timers_context
->timer_list
->head
);
129 void test_walk_and_process_timers_timer_expired()
131 /* We need to alloc it, since it will be free'd in
132 * walk_and_process_timers */
133 pcep_timer
*timer
= pceplib_malloc(PCEPLIB_INFRA
, sizeof(pcep_timer
));
135 // Set the timer to expire 10 seconds ago
136 timer
->expire_time
= time(NULL
) - 10;
137 pthread_mutex_lock(&test_timers_context
->timer_list_lock
);
138 timer
->timer_id
= TEST_EVENT_LOOP_TIMER_ID
;
139 pthread_mutex_unlock(&test_timers_context
->timer_list_lock
);
140 ordered_list_add_node(test_timers_context
->timer_list
, timer
);
142 walk_and_process_timers(test_timers_context
);
144 /* Since the timer expired, the expire_handler should have been called
145 * and the timer should have been removed from the timer list */
146 CU_ASSERT_TRUE(expire_handler_info
.handler_called
);
147 CU_ASSERT_PTR_EQUAL(expire_handler_info
.data
, timer
);
148 CU_ASSERT_EQUAL(expire_handler_info
.timerId
, TEST_EVENT_LOOP_TIMER_ID
);
149 CU_ASSERT_EQUAL(test_timers_context
->timer_list
->num_entries
, 0);
150 CU_ASSERT_PTR_NULL(test_timers_context
->timer_list
->head
);
153 void test_event_loop_null_handle()
155 /* Verify that event_loop() correctly handles a NULL timers_context */
160 void test_event_loop_not_active()
162 /* Verify that event_loop() correctly handles an inactive timers_context
164 test_timers_context
->active
= false;
165 event_loop(test_timers_context
);