mirror of https://github.com/tailix/libshmemq.git
Add test "prod_reaches_buffer_end"
This commit is contained in:
parent
49def7d99f
commit
eb93a66013
|
@ -37,3 +37,4 @@
|
||||||
/examples/raw_receiver
|
/examples/raw_receiver
|
||||||
/examples/raw_sender
|
/examples/raw_sender
|
||||||
/tests/test_main
|
/tests/test_main
|
||||||
|
/tests/test_prod_reaches_buffer_end
|
||||||
|
|
|
@ -9,7 +9,8 @@ AM_CFLAGS = \
|
||||||
lib_LIBRARIES = libshmemq.a
|
lib_LIBRARIES = libshmemq.a
|
||||||
|
|
||||||
TESTS = \
|
TESTS = \
|
||||||
tests/test_main
|
tests/test_main \
|
||||||
|
tests/test_prod_reaches_buffer_end
|
||||||
|
|
||||||
noinst_PROGRAMS = \
|
noinst_PROGRAMS = \
|
||||||
$(TESTS) \
|
$(TESTS) \
|
||||||
|
@ -29,3 +30,7 @@ examples_raw_sender_SOURCES = \
|
||||||
tests_test_main_SOURCES = \
|
tests_test_main_SOURCES = \
|
||||||
$(libshmemq_a_SOURCES) \
|
$(libshmemq_a_SOURCES) \
|
||||||
tests/test_main.c
|
tests/test_main.c
|
||||||
|
|
||||||
|
tests_test_prod_reaches_buffer_end_SOURCES = \
|
||||||
|
$(libshmemq_a_SOURCES) \
|
||||||
|
tests/test_prod_reaches_buffer_end.c
|
||||||
|
|
|
@ -28,6 +28,7 @@ typedef enum ShmemqError {
|
||||||
|
|
||||||
// Bugs in user code.
|
// Bugs in user code.
|
||||||
SHMEMQ_ERROR_BUG_POP_END_ON_EMPTY_QUEUE = 50,
|
SHMEMQ_ERROR_BUG_POP_END_ON_EMPTY_QUEUE = 50,
|
||||||
|
SHMEMQ_ERROR_BUG_PUSH_END_ON_FULL_QUEUE = 51,
|
||||||
|
|
||||||
// Failed system calls.
|
// Failed system calls.
|
||||||
SHMEMQ_ERROR_FAILED_MALLOC = 100,
|
SHMEMQ_ERROR_FAILED_MALLOC = 100,
|
||||||
|
|
20
src/main.c
20
src/main.c
|
@ -144,6 +144,13 @@ void shmemq_init(
|
||||||
|
|
||||||
ShmemqFrame shmemq_push_start(const Shmemq shmemq)
|
ShmemqFrame shmemq_push_start(const Shmemq shmemq)
|
||||||
{
|
{
|
||||||
|
if (
|
||||||
|
shmemq->buffer->header.write_frame_index >=
|
||||||
|
shmemq->buffer->header.frames_count
|
||||||
|
) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
const ShmemqFrame low_frame = &shmemq->buffer->frames[0];
|
const ShmemqFrame low_frame = &shmemq->buffer->frames[0];
|
||||||
const ShmemqFrame high_frame = &shmemq->buffer->frames[
|
const ShmemqFrame high_frame = &shmemq->buffer->frames[
|
||||||
shmemq->buffer->header.write_frame_index
|
shmemq->buffer->header.write_frame_index
|
||||||
|
@ -167,6 +174,14 @@ void shmemq_push_end(
|
||||||
) {
|
) {
|
||||||
if (error_ptr) *error_ptr = SHMEMQ_ERROR_NONE;
|
if (error_ptr) *error_ptr = SHMEMQ_ERROR_NONE;
|
||||||
|
|
||||||
|
if (
|
||||||
|
shmemq->buffer->header.write_frame_index >=
|
||||||
|
shmemq->buffer->header.frames_count
|
||||||
|
) {
|
||||||
|
if (error_ptr) *error_ptr = SHMEMQ_ERROR_BUG_PUSH_END_ON_FULL_QUEUE;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const ShmemqFrame frame =
|
const ShmemqFrame frame =
|
||||||
&shmemq->buffer->frames[shmemq->buffer->header.write_frame_index];
|
&shmemq->buffer->frames[shmemq->buffer->header.write_frame_index];
|
||||||
|
|
||||||
|
@ -186,7 +201,10 @@ void shmemq_push_end(
|
||||||
shmemq->buffer->header.write_frame_index +
|
shmemq->buffer->header.write_frame_index +
|
||||||
frame->header.message_frames_count;
|
frame->header.message_frames_count;
|
||||||
|
|
||||||
if (new_write_frame_index >= shmemq->buffer->header.frames_count) {
|
if (
|
||||||
|
new_write_frame_index >= shmemq->buffer->header.frames_count &&
|
||||||
|
shmemq->buffer->header.read_frame_index > 0
|
||||||
|
) {
|
||||||
shmemq->buffer->header.write_frame_index = 0;
|
shmemq->buffer->header.write_frame_index = 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
#include <shmemq.h>
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
static const char name[] = "/foobar";
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ShmemqError error;
|
||||||
|
|
||||||
|
const Shmemq consumer = shmemq_new(name, true, &error);
|
||||||
|
assert(error == SHMEMQ_ERROR_NONE);
|
||||||
|
|
||||||
|
const Shmemq producer = shmemq_new(name, false, &error);
|
||||||
|
assert(error == SHMEMQ_ERROR_NONE);
|
||||||
|
|
||||||
|
for (unsigned i = 0; i < 100; ++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);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(shmemq_push_start(producer) == NULL);
|
||||||
|
|
||||||
|
shmemq_push_end(producer, sizeof(unsigned), &error);
|
||||||
|
assert(error == SHMEMQ_ERROR_BUG_PUSH_END_ON_FULL_QUEUE);
|
||||||
|
|
||||||
|
shmemq_delete(consumer, &error);
|
||||||
|
assert(error == SHMEMQ_ERROR_NONE);
|
||||||
|
|
||||||
|
shmemq_delete(producer, &error);
|
||||||
|
assert(error == SHMEMQ_ERROR_NONE);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue