Skip to content

Commit 6c34428

Browse files
committed
tests: internal: add peek/seek test for ring_buffer.
Signed-off-by: Phillip Adair Stewart Whelan <phillip.whelan@chronosphere.io>
1 parent d9b079f commit 6c34428

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

tests/internal/ring_buffer.c

+50
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,56 @@ static void test_smart_flush()
161161
flb_bucket_queue_destroy(bktq);
162162
mk_event_loop_destroy(evl);
163163
}
164+
165+
void test_peek_seek()
166+
{
167+
int i;
168+
int ret;
169+
int elements;
170+
struct check *c;
171+
struct check *tmp;
172+
struct flb_ring_buffer *rb;
173+
174+
elements = sizeof(checks) / sizeof(struct check);
175+
176+
rb = flb_ring_buffer_create(sizeof(struct check *) * elements);
177+
TEST_CHECK(rb != NULL);
178+
if (!rb) {
179+
exit(EXIT_FAILURE);
180+
}
181+
182+
for (i = 0; i < elements; i++) {
183+
c = &checks[i];
184+
ret = flb_ring_buffer_write(rb, (void *) &c, sizeof(c));
185+
TEST_CHECK(ret == 0);
186+
}
187+
188+
/* try to write another record, it must fail */
189+
tmp = c;
190+
ret = flb_ring_buffer_write(rb, (void *) &tmp, sizeof(tmp));
191+
TEST_CHECK(ret == -1);
192+
193+
c = NULL;
194+
195+
/* consume one entry */
196+
ret = flb_ring_buffer_peek(rb, (void *) &c, sizeof(c));
197+
TEST_CHECK(ret == 0);
198+
199+
/* the consumed entry must be equal to the first one */
200+
c = &checks[0];
201+
TEST_CHECK(strcmp(c->buf_a, "a1") == 0 && strcmp(c->buf_b, "a2") ==0);
202+
203+
/* consume one entry */
204+
ret = flb_ring_buffer_peek(rb, (void *) &c, sizeof(c));
205+
TEST_CHECK(ret == 0);
206+
207+
/* the consumed entry must be equal to the first one */
208+
c = &checks[0];
209+
TEST_CHECK(strcmp(c->buf_a, "a1") == 0 && strcmp(c->buf_b, "a2") ==0);
210+
211+
flb_ring_buffer_destroy(rb);
212+
}
213+
164214
TEST_LIST = {
165215
{ "basic", test_basic},
166216
{ "smart_flush", test_smart_flush},

0 commit comments

Comments
 (0)