From 4189552a86efd074e64e8567f09fb5e10a121679 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Tue, 15 Dec 2020 16:06:18 +0500 Subject: [PATCH] Add test "push_and_pop_one_frame_messages_without_jump" --- .gitignore | 1 + Makefile.am | 7 +- src/main.c | 1 + ..._and_pop_one_frame_messages_without_jump.c | 96 +++++++++++++++++++ 4 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 tests/test_push_and_pop_one_frame_messages_without_jump.c diff --git a/.gitignore b/.gitignore index 122e338..2fa2e80 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,4 @@ /tests/test_prod_jumps_to_buffer_start_and_reaches_cons /tests/test_prod_pushes_too_long_message /tests/test_prod_reaches_buffer_end +/tests/test_push_and_pop_one_frame_messages_without_jump diff --git a/Makefile.am b/Makefile.am index b501458..577294e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -15,7 +15,8 @@ TESTS = \ tests/test_prod_jumps_to_buffer_start_and_pushes_too_long_message \ tests/test_prod_jumps_to_buffer_start_and_reaches_cons \ tests/test_prod_pushes_too_long_message \ - tests/test_prod_reaches_buffer_end + tests/test_prod_reaches_buffer_end \ + tests/test_push_and_pop_one_frame_messages_without_jump noinst_PROGRAMS = \ $(TESTS) \ @@ -59,3 +60,7 @@ tests_test_prod_pushes_too_long_message_SOURCES = \ tests_test_prod_reaches_buffer_end_SOURCES = \ $(libshmemq_a_SOURCES) \ tests/test_prod_reaches_buffer_end.c + +tests_test_push_and_pop_one_frame_messages_without_jump_SOURCES = \ + $(libshmemq_a_SOURCES) \ + tests/test_push_and_pop_one_frame_messages_without_jump.c diff --git a/src/main.c b/src/main.c index a51acb4..79eae0e 100644 --- a/src/main.c +++ b/src/main.c @@ -257,6 +257,7 @@ ShmemqFrame shmemq_pop_start(const Shmemq shmemq) return high_frame; } + shmemq->buffer->header.read_frame_index = 0; return low_frame; } diff --git a/tests/test_push_and_pop_one_frame_messages_without_jump.c b/tests/test_push_and_pop_one_frame_messages_without_jump.c new file mode 100644 index 0000000..5558c42 --- /dev/null +++ b/tests/test_push_and_pop_one_frame_messages_without_jump.c @@ -0,0 +1,96 @@ +#include + +#include +#include +#include + +static const char name[] = "/foobar"; + +static ShmemqError error = SHMEMQ_ERROR_NONE; +static Shmemq consumer = NULL; +static Shmemq producer = NULL; + +static void on_exit(); +static void on_signal(int signo); + +int main() +{ + atexit(on_exit); + signal(SIGABRT, on_signal); + + consumer = shmemq_new(name, true, &error); + assert(error == SHMEMQ_ERROR_NONE); + + producer = shmemq_new(name, false, &error); + assert(error == SHMEMQ_ERROR_NONE); + + for (unsigned i = 0; i < 25; ++i) { + const ShmemqFrame frame = shmemq_push_start(producer); + assert(frame != NULL); + + *(unsigned*)frame->data = i; + + shmemq_push_end(producer, sizeof(unsigned), &error); + assert(error == SHMEMQ_ERROR_NONE); + } + + for (unsigned i = 0; i < 25; ++i) { + const ShmemqFrame frame = shmemq_pop_start(consumer); + assert(frame != NULL); + + assert(*(unsigned*)frame->data == i); + + shmemq_pop_end(consumer, &error); + assert(error == SHMEMQ_ERROR_NONE); + } + + for (unsigned i = 25; i < 124; ++i) { + const ShmemqFrame frame = shmemq_push_start(producer); + assert(frame != NULL); + + *(unsigned*)frame->data = i; + + shmemq_push_end(producer, sizeof(unsigned), &error); + assert(error == SHMEMQ_ERROR_NONE); + } + + { + const ShmemqFrame frame = shmemq_push_start(producer); + assert(frame == NULL); + } + + for (unsigned i = 25; i < 124; ++i) { + const ShmemqFrame frame = shmemq_pop_start(consumer); + assert(frame != NULL); + + assert(*(unsigned*)frame->data == i); + + shmemq_pop_end(consumer, &error); + assert(error == SHMEMQ_ERROR_NONE); + } + + { + const ShmemqFrame frame = shmemq_pop_start(producer); + assert(frame == NULL); + } + + return 0; +} + +void on_exit() +{ + if (consumer) { + SHMEMQ_DELETE(consumer, &error); + assert(error == SHMEMQ_ERROR_NONE); + } + + if (producer) { + SHMEMQ_DELETE(producer, &error); + assert(error == SHMEMQ_ERROR_NONE); + } +} + +void on_signal(const int signo __attribute__((unused))) +{ + on_exit(); +}