mirror of https://github.com/tailix/libshmemq.git
Add test "fork"
This commit is contained in:
parent
c64e5053d4
commit
669ae301ff
|
@ -36,10 +36,11 @@
|
|||
|
||||
/examples/raw_receiver
|
||||
/examples/raw_sender
|
||||
/tests/test_main
|
||||
/tests/test_alternatively_push_and_pop_one_frame_messages_with_jump
|
||||
/tests/test_cons_pops_buffer_start
|
||||
/tests/test_cons_reaches_queue_end
|
||||
/tests/test_fork
|
||||
/tests/test_main
|
||||
/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
|
||||
|
|
15
Makefile.am
15
Makefile.am
|
@ -9,10 +9,11 @@ AM_CFLAGS = \
|
|||
lib_LIBRARIES = libshmemq.a
|
||||
|
||||
TESTS = \
|
||||
tests/test_main \
|
||||
tests/test_alternatively_push_and_pop_one_frame_messages_with_jump \
|
||||
tests/test_cons_pops_buffer_start \
|
||||
tests/test_cons_reaches_queue_end \
|
||||
tests/test_fork \
|
||||
tests/test_main \
|
||||
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 \
|
||||
|
@ -38,10 +39,6 @@ tests_test_alternatively_push_and_pop_one_frame_messages_with_jump_SOURCES = \
|
|||
$(libshmemq_a_SOURCES) \
|
||||
tests/test_alternatively_push_and_pop_one_frame_messages_with_jump.c
|
||||
|
||||
tests_test_main_SOURCES = \
|
||||
$(libshmemq_a_SOURCES) \
|
||||
tests/test_main.c
|
||||
|
||||
tests_test_cons_pops_buffer_start_SOURCES = \
|
||||
$(libshmemq_a_SOURCES) \
|
||||
tests/test_cons_pops_buffer_start.c
|
||||
|
@ -50,6 +47,14 @@ tests_test_cons_reaches_queue_end_SOURCES = \
|
|||
$(libshmemq_a_SOURCES) \
|
||||
tests/test_cons_reaches_queue_end.c
|
||||
|
||||
tests_test_fork_SOURCES = \
|
||||
$(libshmemq_a_SOURCES) \
|
||||
tests/test_fork.c
|
||||
|
||||
tests_test_main_SOURCES = \
|
||||
$(libshmemq_a_SOURCES) \
|
||||
tests/test_main.c
|
||||
|
||||
tests_test_prod_jumps_to_buffer_start_and_pushes_too_long_message_SOURCES = \
|
||||
$(libshmemq_a_SOURCES) \
|
||||
tests/test_prod_jumps_to_buffer_start_and_pushes_too_long_message.c
|
||||
|
|
|
@ -36,16 +36,21 @@ AC_CHECK_HEADERS([stdlib.h])
|
|||
AC_CHECK_HEADERS([string.h])
|
||||
AC_CHECK_HEADERS([sys/mman.h])
|
||||
AC_CHECK_HEADERS([sys/stat.h])
|
||||
AC_CHECK_HEADERS([sys/types.h])
|
||||
AC_CHECK_HEADERS([sys/wait.h])
|
||||
AC_CHECK_HEADERS([unistd.h])
|
||||
|
||||
AC_CHECK_FUNCS([close])
|
||||
AC_CHECK_FUNCS([fork])
|
||||
AC_CHECK_FUNCS([free])
|
||||
AC_CHECK_FUNCS([ftruncate])
|
||||
AC_CHECK_FUNCS([memset])
|
||||
AC_CHECK_FUNCS([munmap])
|
||||
AC_CHECK_FUNCS([sleep])
|
||||
AC_CHECK_FUNCS([strcmp])
|
||||
AC_CHECK_FUNCS([strcpy])
|
||||
AC_CHECK_FUNCS([strlen])
|
||||
AC_CHECK_FUNCS([waitpid])
|
||||
|
||||
AC_SEARCH_LIBS([shm_open], [rt])
|
||||
AC_SEARCH_LIBS([shm_unlink], [rt])
|
||||
|
|
|
@ -0,0 +1,102 @@
|
|||
#include <shmemq.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static const char name[] = "/foobar";
|
||||
|
||||
static pid_t pid = 0;
|
||||
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()
|
||||
{
|
||||
pid = fork();
|
||||
assert(pid != -1);
|
||||
|
||||
if (pid) {
|
||||
atexit(on_exit);
|
||||
signal(SIGABRT, on_signal);
|
||||
|
||||
consumer = shmemq_new(name, true, &error);
|
||||
assert(error == SHMEMQ_ERROR_NONE);
|
||||
|
||||
for (unsigned index = 0; index < 1000;) {
|
||||
const ShmemqFrame frame = shmemq_pop_start(consumer);
|
||||
|
||||
if (frame == NULL) {
|
||||
sleep(1);
|
||||
continue;
|
||||
}
|
||||
|
||||
++index;
|
||||
|
||||
const unsigned data = *(unsigned*)frame->data;
|
||||
|
||||
assert(data == index);
|
||||
|
||||
shmemq_pop_end(consumer, &error);
|
||||
assert(error == SHMEMQ_ERROR_NONE);
|
||||
}
|
||||
}
|
||||
else {
|
||||
atexit(on_exit);
|
||||
signal(SIGABRT, on_signal);
|
||||
|
||||
sleep(1);
|
||||
|
||||
producer = shmemq_new(name, false, &error);
|
||||
assert(error == SHMEMQ_ERROR_NONE);
|
||||
|
||||
for (unsigned index = 0; index < 1000;) {
|
||||
const ShmemqFrame frame = shmemq_push_start(producer);
|
||||
|
||||
if (frame == NULL) {
|
||||
sleep(1);
|
||||
continue;
|
||||
}
|
||||
|
||||
++index;
|
||||
|
||||
*(unsigned*)frame->data = index;
|
||||
|
||||
shmemq_push_end(producer, sizeof(unsigned), &error);
|
||||
assert(error == SHMEMQ_ERROR_NONE);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
if (pid) {
|
||||
int wstatus;
|
||||
waitpid(pid, &wstatus, 0);
|
||||
assert(WIFEXITED(wstatus) == true);
|
||||
assert(WEXITSTATUS(wstatus) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
void on_signal(const int signo __attribute__((unused)))
|
||||
{
|
||||
on_exit();
|
||||
}
|
Loading…
Reference in New Issue