File: test_instant_ack_tracker.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 (232 lines) | stat: -rw-r--r-- 7,366 bytes parent folder | download | duplicates (2)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * Copyright (c) 2020 One Identity
 * Copyright (c) 2020 Laszlo Budai <laszlo.budai@outlook.com>
 *
 * 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 "ack-tracker/instant_ack_tracker.h"
#include "ack-tracker/ack_tracker_factory.h"
#include "logsource.h"
#include "apphook.h"

GlobalConfig *cfg;

typedef struct _TestBookmarkData
{
  guint *saved_ctr;
} TestBookmarkData;

static void
_save_bookmark(Bookmark *bookmark)
{
  TestBookmarkData *bookmark_data = (TestBookmarkData *) &bookmark->container;
  (*bookmark_data->saved_ctr)++;
}

static void
_fill_bookmark(Bookmark *bookmark, guint *ctr)
{
  TestBookmarkData *bookmark_data = (TestBookmarkData *) &bookmark->container;

  bookmark_data->saved_ctr = ctr;
  bookmark->save = _save_bookmark;
}

typedef struct _TestLogPipeDst
{
  LogPipe super;
} TestLogPipeDst;

static gboolean
_test_logpipe_dst_init(LogPipe *s)
{
  return TRUE;
}

static void
_test_logpipe_dst_queue(LogPipe *s, LogMessage *msg, const LogPathOptions *path_options)
{
}
static TestLogPipeDst *
_init_test_logpipe_dst(void)
{
  TestLogPipeDst *dst = g_new0(TestLogPipeDst, 1);

  log_pipe_init_instance(&dst->super, cfg);
  dst->super.init = _test_logpipe_dst_init;
  dst->super.queue = _test_logpipe_dst_queue;

  cr_assert(log_pipe_init(&dst->super));

  return dst;
}

static void
_deinit_test_logpipe_dst(TestLogPipeDst *dst)
{
  log_pipe_deinit(&dst->super);
  log_pipe_unref(&dst->super);
}

static LogSource *
_init_log_source(AckTrackerFactory *factory)
{
  LogSource *src = g_new0(LogSource, 1);
  LogSourceOptions *options = g_new0(LogSourceOptions, 1);

  log_source_options_defaults(options);
  options->init_window_size = 10;
  log_source_init_instance(src, cfg);
  log_source_options_init(options, cfg, "testgroup");
  log_source_set_options(src, options, "test_stats_id", NULL, TRUE, NULL);
  log_source_set_ack_tracker_factory(src, factory);

  cr_assert(log_pipe_init(&src->super));

  return src;
}

static void
_deinit_log_source(LogSource *src)
{
  log_pipe_deinit(&src->super);
  g_free(src->options);
  log_pipe_unref(&src->super);
}

static void
_setup(void)
{
  cfg = cfg_new_snippet();
  app_startup();
}

static void
_teardown(void)
{
  app_shutdown();
  cfg_free(cfg);
}

TestSuite(instant_ack_tracker_bookmarkless, .init = _setup, .fini = _teardown);

Test(instant_ack_tracker_bookmarkless, request_bookmark_returns_the_same_bookmark)
{
  LogSource *src = _init_log_source(instant_ack_tracker_bookmarkless_factory_new());
  cr_assert_not_null(src->ack_tracker);
  AckTracker *ack_tracker = src->ack_tracker;
  Bookmark *bm1 = ack_tracker_request_bookmark(ack_tracker);
  Bookmark *bm2 = ack_tracker_request_bookmark(ack_tracker);
  cr_expect_eq(bm1, bm2);
  _deinit_log_source(src);
}

Test(instant_ack_tracker_bookmarkless, bookmark_save_not_called_when_acked)
{
  LogSource *src = _init_log_source(instant_ack_tracker_bookmarkless_factory_new());
  TestLogPipeDst *dst = _init_test_logpipe_dst();
  log_pipe_append(&src->super, &dst->super);
  cr_assert_not_null(src->ack_tracker);
  AckTracker *ack_tracker = src->ack_tracker;
  Bookmark *bm = ack_tracker_request_bookmark(ack_tracker);
  guint saved_ctr = 0;
  _fill_bookmark(bm, &saved_ctr);
  LogMessage *msg = log_msg_new_empty();
  cr_expect_eq(window_size_counter_get(&src->window_size, NULL), 10);
  log_source_post(src, msg);
  // this is why we need a dummy 'destination' : keep back ACK
  cr_expect_eq(window_size_counter_get(&src->window_size, NULL), 9);
  cr_assert_eq(msg->ack_record->tracker, ack_tracker);
  LogPathOptions path_options = LOG_PATH_OPTIONS_INIT;
  log_msg_ack(msg, &path_options, AT_PROCESSED);
  // okay, ACK is done, but save counter is 0 -> so Bookmark::save not called
  cr_expect_eq(saved_ctr, 0);
  cr_expect_eq(window_size_counter_get(&src->window_size, NULL), 10);
  log_msg_unref(msg);
  _deinit_log_source(src);
  _deinit_test_logpipe_dst(dst);
}

Test(instant_ack_tracker_bookmarkless, same_bookmark_for_all_messages)
{
  LogSource *src = _init_log_source(instant_ack_tracker_bookmarkless_factory_new());
  cr_assert_not_null(src->ack_tracker);
  AckTracker *ack_tracker = src->ack_tracker;
  Bookmark *bm1 = ack_tracker_request_bookmark(ack_tracker);
  LogMessage *msg1 = log_msg_new_empty();
  ack_tracker_track_msg(ack_tracker, msg1);
  Bookmark *bm2 = ack_tracker_request_bookmark(ack_tracker);
  LogMessage *msg2 = log_msg_new_empty();
  ack_tracker_track_msg(ack_tracker, msg2);
  ack_tracker_manage_msg_ack(ack_tracker, msg1, AT_PROCESSED);
  ack_tracker_manage_msg_ack(ack_tracker, msg2, AT_PROCESSED);
  cr_expect_eq(bm1, bm2);
  _deinit_log_source(src);
}


TestSuite(instant_ack_tracker, .init = _setup, .fini = _teardown);

Test(instant_ack_tracker, request_bookmark_returns_same_bookmarks_until_pending_not_assigned)
{
  LogSource *src = _init_log_source(instant_ack_tracker_factory_new());
  cr_assert_not_null(src->ack_tracker);
  AckTracker *ack_tracker = src->ack_tracker;
  Bookmark *bm1 = ack_tracker_request_bookmark(ack_tracker);
  Bookmark *bm2 = ack_tracker_request_bookmark(ack_tracker);
  cr_expect_eq(bm1, bm2);
  LogMessage *msg = log_msg_new_empty();
  ack_tracker_track_msg(ack_tracker, msg);
  log_msg_ref(msg);
  bm2 = ack_tracker_request_bookmark(ack_tracker);
  cr_expect_neq(bm1, bm2);
  ack_tracker_manage_msg_ack(ack_tracker, msg, AT_PROCESSED);
  log_msg_unref(msg);
  _deinit_log_source(src);
}

Test(instant_ack_tracker, bookmark_saving)
{
  LogSource *src = _init_log_source(instant_ack_tracker_factory_new());
  TestLogPipeDst *dst = _init_test_logpipe_dst();
  log_pipe_append(&src->super, &dst->super);
  cr_assert_not_null(src->ack_tracker);
  AckTracker *ack_tracker = src->ack_tracker;
  Bookmark *bm = ack_tracker_request_bookmark(ack_tracker);
  guint saved_ctr = 0;
  _fill_bookmark(bm, &saved_ctr);
  LogMessage *msg = log_msg_new_empty();
  cr_expect_eq(window_size_counter_get(&src->window_size, NULL), 10);
  log_source_post(src, msg);
  // this is why we need a dummy 'destination' : keep back ACK
  cr_expect_eq(window_size_counter_get(&src->window_size, NULL), 9);
  cr_assert_eq(msg->ack_record->tracker, ack_tracker);
  LogPathOptions path_options = LOG_PATH_OPTIONS_INIT;
  log_msg_ack(msg, &path_options, AT_PROCESSED);
  // okay, ACK is done, save counter is 1
  cr_expect_eq(saved_ctr, 1);
  cr_expect_eq(window_size_counter_get(&src->window_size, NULL), 10);
  log_msg_unref(msg);
  _deinit_log_source(src);
  _deinit_test_logpipe_dst(dst);
}