]> git.proxmox.com Git - mirror_frr.git/blob - pceplib/test/pcep_timers_event_loop_test.c
doc: Add `show ipv6 rpf X:X::X:X` command to docs
[mirror_frr.git] / pceplib / test / pcep_timers_event_loop_test.c
1 /*
2 * This file is part of the PCEPlib, a PCEP protocol library.
3 *
4 * Copyright (C) 2020 Volta Networks https://voltanet.io/
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author : Brady Johnson <brady@voltanet.io>
20 *
21 */
22
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <stdlib.h>
29
30 #include <CUnit/CUnit.h>
31
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"
36
37
38 typedef struct timer_expire_handler_info_ {
39 bool handler_called;
40 void *data;
41 int timerId;
42
43 } timer_expire_handler_info;
44
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
48
49
50 /* Called when a timer expires */
51 static void test_timer_expire_handler(void *data, int timerId)
52 {
53 expire_handler_info.handler_called = true;
54 expire_handler_info.data = data;
55 expire_handler_info.timerId = timerId;
56 }
57
58
59 /* Test case setup called before each test.
60 * Declared in pcep_timers_tests.c */
61 void pcep_timers_event_loop_test_setup()
62 {
63 test_timers_context =
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)
67 != 0) {
68 fprintf(stderr,
69 "ERROR initializing timers, cannot initialize the mutex\n");
70 }
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);
75
76 expire_handler_info.handler_called = false;
77 expire_handler_info.data = NULL;
78 expire_handler_info.timerId = -1;
79 }
80
81
82 /* Test case teardown called after each test.
83 * Declared in pcep_timers_tests.c */
84 void pcep_timers_event_loop_test_teardown()
85 {
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;
91 }
92
93
94 /*
95 * Test functions
96 */
97
98 void test_walk_and_process_timers_no_timers()
99 {
100 CU_ASSERT_EQUAL(test_timers_context->timer_list->num_entries, 0);
101 CU_ASSERT_PTR_NULL(test_timers_context->timer_list->head);
102
103 walk_and_process_timers(test_timers_context);
104
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);
108 }
109
110
111 void test_walk_and_process_timers_timer_not_expired()
112 {
113 pcep_timer timer;
114 timer.data = &timer;
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);
119
120 walk_and_process_timers(test_timers_context);
121
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);
126 }
127
128
129 void test_walk_and_process_timers_timer_expired()
130 {
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));
134 timer->data = 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);
141
142 walk_and_process_timers(test_timers_context);
143
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);
151 }
152
153 void test_event_loop_null_handle()
154 {
155 /* Verify that event_loop() correctly handles a NULL timers_context */
156 event_loop(NULL);
157 }
158
159
160 void test_event_loop_not_active()
161 {
162 /* Verify that event_loop() correctly handles an inactive timers_context
163 * flag */
164 test_timers_context->active = false;
165 event_loop(test_timers_context);
166 }