File: test_apphook.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (186 lines) | stat: -rw-r--r-- 5,362 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright (c) 2018 Balabit
 * Copyright (c) 2018 Kokan
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

#include <criterion/criterion.h>

#include "apphook.h"

Test(test_apphook, app_is_tarting_up)
{
  cr_assert(app_is_starting_up());
  cr_assert_not(app_is_shutting_down());
}

static void
_hook_counter(gint type, gpointer user_data)
{
  gint *triggered_count = (gboolean *)user_data;
  (*triggered_count)++;
}

Test(test_apphook, post_daemon_hook)
{
  gint triggered_count = 0;
  register_application_hook(AH_POST_DAEMONIZED, _hook_counter, (gpointer)&triggered_count, AHM_RUN_ONCE);
  cr_assert(app_is_starting_up());
  app_post_daemonized();

  cr_assert_eq(triggered_count, 1);
}

Test(test_apphook, post_daemon_triggered_twice)
{
  gint triggered_count = 0;
  register_application_hook(AH_POST_DAEMONIZED, _hook_counter, (gpointer)&triggered_count, AHM_RUN_ONCE);

  app_post_daemonized();
  app_post_daemonized();

  cr_assert_eq(triggered_count, 1);
}

Test(test_apphook, pre_shutdown_hook)
{
  gint triggered_count = 0;
  register_application_hook(AH_PRE_SHUTDOWN, _hook_counter, (gpointer)&triggered_count, AHM_RUN_ONCE);

  app_pre_shutdown();

  cr_assert_eq(triggered_count, 1);
}

Test(test_apphook, shutdown_hook)
{
  gint triggered_count = 0;
  register_application_hook(AH_SHUTDOWN, _hook_counter, (gpointer)&triggered_count, AHM_RUN_ONCE);

  app_startup(); //This is needed for shutdown
  app_shutdown();

  cr_assert_eq(triggered_count, 1);
}

Test(test_apphook, config_changed)
{
  gint triggered_count = 0;
  register_application_hook(AH_CONFIG_CHANGED, _hook_counter, (gpointer)&triggered_count, AHM_RUN_ONCE);

  app_config_changed();

  cr_assert_eq(triggered_count, 1);
}


Test(test_apphook, reopen_hook)
{
  gint triggered_count = 0;
  register_application_hook(AH_REOPEN_FILES, _hook_counter, (gpointer)&triggered_count, AHM_RUN_ONCE);

  app_reopen_files();

  cr_assert_eq(triggered_count, 1);
}

static void
_re_registering_hook(gint type, gpointer user_data)
{
  _hook_counter(type, user_data);
  register_application_hook(type, _re_registering_hook, user_data, AHM_RUN_ONCE);
}

Test(test_apphook, hook_register_from_hook)
{
  gint triggered_count = 0;

  register_application_hook(AH_REOPEN_FILES, _re_registering_hook, (gpointer)&triggered_count, AHM_RUN_ONCE);

  app_reopen_files();
  cr_assert_eq(triggered_count, 1);
}

static void
_repeated_hook(gint type, gpointer user_data)
{
  _hook_counter(type, user_data);
}

Test(test_apphook, hook_run_mode_repeat_repeats_hook_invocation_multiple_times)
{
  gint triggered_count = 0;

  register_application_hook(AH_REOPEN_FILES, _repeated_hook, (gpointer)&triggered_count, AHM_RUN_REPEAT);

  app_reopen_files();
  cr_assert_eq(triggered_count, 1);
  app_reopen_files();
  cr_assert_eq(triggered_count, 2);
  app_reopen_files();
  cr_assert_eq(triggered_count, 3);
}

Test(test_apphook, hook_register_from_hook_and_other_not_triggered_hooks)
{
  gint triggered_count = 0;

  register_application_hook(AH_PRE_SHUTDOWN, NULL, NULL, AHM_RUN_ONCE);
  register_application_hook(AH_REOPEN_FILES, _re_registering_hook, (gpointer)&triggered_count, AHM_RUN_ONCE);
  register_application_hook(AH_PRE_SHUTDOWN, NULL, NULL, AHM_RUN_ONCE);

  app_reopen_files();
  cr_assert_eq(triggered_count, 1);
}

Test(test_apphook, trigger_all_state_hook)
{
  gint triggered_count = 0;
  register_application_hook(AH_POST_DAEMONIZED, _hook_counter, (gpointer)&triggered_count, AHM_RUN_ONCE);
  register_application_hook(AH_RUNNING, _hook_counter, (gpointer)&triggered_count, AHM_RUN_ONCE);
  register_application_hook(AH_PRE_SHUTDOWN, _hook_counter, (gpointer)&triggered_count, AHM_RUN_ONCE);
  register_application_hook(AH_SHUTDOWN, _hook_counter, (gpointer)&triggered_count, AHM_RUN_ONCE);

  register_application_hook(AH_CONFIG_CHANGED, _hook_counter, (gpointer)&triggered_count, AHM_RUN_ONCE);
  register_application_hook(AH_REOPEN_FILES, _hook_counter, (gpointer)&triggered_count, AHM_RUN_ONCE);

  app_startup();
  app_post_daemonized();
  cr_assert_eq(triggered_count, 1);

  app_running();
  cr_assert_eq(triggered_count, 2);

  /* check that a state that has already passed would be invoked immediately */
  register_application_hook(AH_POST_DAEMONIZED, _hook_counter, (gpointer)&triggered_count, AHM_RUN_ONCE);

  cr_assert_eq(triggered_count, 3);

  app_config_changed();
  app_reopen_files();
  cr_assert_eq(triggered_count, 5);

  app_pre_shutdown();
  app_shutdown();

  cr_assert_eq(triggered_count, 7);
}