From 65f1a892407f633e90806f0a977dd4236b134dce Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Tue, 15 Dec 2020 13:55:22 +0500 Subject: [PATCH] Add test "prod_pushes_too_long_message" --- .gitignore | 1 + Makefile.am | 5 ++ src/main.c | 11 ++-- tests/test_prod_pushes_too_long_message.c | 62 +++++++++++++++++++++++ 4 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 tests/test_prod_pushes_too_long_message.c diff --git a/.gitignore b/.gitignore index f1c9f67..122e338 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,5 @@ /tests/test_cons_reaches_queue_end /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 diff --git a/Makefile.am b/Makefile.am index a35b781..b501458 100644 --- a/Makefile.am +++ b/Makefile.am @@ -14,6 +14,7 @@ TESTS = \ tests/test_cons_reaches_queue_end \ 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 noinst_PROGRAMS = \ @@ -51,6 +52,10 @@ tests_test_prod_jumps_to_buffer_start_and_reaches_cons_SOURCES = \ $(libshmemq_a_SOURCES) \ tests/test_prod_jumps_to_buffer_start_and_reaches_cons.c +tests_test_prod_pushes_too_long_message_SOURCES = \ + $(libshmemq_a_SOURCES) \ + tests/test_prod_pushes_too_long_message.c + tests_test_prod_reaches_buffer_end_SOURCES = \ $(libshmemq_a_SOURCES) \ tests/test_prod_reaches_buffer_end.c diff --git a/src/main.c b/src/main.c index 995ce71..a51acb4 100644 --- a/src/main.c +++ b/src/main.c @@ -222,10 +222,13 @@ void shmemq_push_end( frame->header.message_frames_count; if ( - shmemq->buffer->header.write_frame_index < - shmemq->buffer->header.read_frame_index && - new_write_frame_index >= - shmemq->buffer->header.read_frame_index + new_write_frame_index > + shmemq->buffer->header.frames_count || ( + shmemq->buffer->header.write_frame_index < + shmemq->buffer->header.read_frame_index && + new_write_frame_index >= + shmemq->buffer->header.read_frame_index + ) ) { if (error_ptr) *error_ptr = SHMEMQ_ERROR_BUG_PUSH_END_OVERFLOW; return; diff --git a/tests/test_prod_pushes_too_long_message.c b/tests/test_prod_pushes_too_long_message.c new file mode 100644 index 0000000..7bcfa9e --- /dev/null +++ b/tests/test_prod_pushes_too_long_message.c @@ -0,0 +1,62 @@ +#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 < 99; ++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); + + shmemq_push_end(producer, 9, &error); + assert(error == SHMEMQ_ERROR_BUG_PUSH_END_OVERFLOW); + + 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(); +}