Skip to content

Commit

Permalink
core tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredrik Widlund committed May 17, 2020
1 parent d680409 commit 14f36d5
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 7 deletions.
9 changes: 9 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ CHECK_LDFLAGS_EXTRA = \
-Wl,--wrap=send \
-Wl,--wrap=pthread_create \
-Wl,--wrap=socketpair \
-Wl,--wrap=epoll_create1 \
-Wl,--wrap=epoll_ctl \
-Wl,--wrap=epoll_wait \
-pthread

check_PROGRAMS = test/hash
Expand Down Expand Up @@ -123,6 +126,12 @@ test_pool_LDADD = $(CHECK_LDADD)
test_pool_LDFLAGS = $(CHECK_LDFLAGS_EXTRA)
test_pool_SOURCES = test/pool.c test/mock.c

check_PROGRAMS += test/core
test_core_CFLAGS = $(CHECK_CFLAGS)
test_core_LDADD = $(CHECK_LDADD)
test_core_LDFLAGS = $(CHECK_LDFLAGS_EXTRA)
test_core_SOURCES = test/core.c test/mock.c

dist_noinst_SCRIPTS = test/valgrind.sh test/coverage.sh

TESTS = $(check_PROGRAMS) test/coverage.sh test/valgrind.sh
Expand Down
3 changes: 2 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AC_INIT([libdynamic], [1.3.0], [fredrik.widlund@gmail.com])
AC_INIT([libdynamic], [2.0.0], [fredrik.widlund@gmail.com])
AC_CONFIG_AUX_DIR(autotools)
AC_CONFIG_MACRO_DIR([m4])
AM_INIT_AUTOMAKE([-Wall -Werror foreign no-define])
Expand All @@ -11,5 +11,6 @@ AC_PROG_LIBTOOL
AM_PROG_CC_C_O
AC_PROG_CXX

AC_PREFIX_DEFAULT(/usr)
AC_CONFIG_FILES([Makefile docs/Makefile benchmark/Makefile examples/Makefile libdynamic.pc])
AC_OUTPUT
7 changes: 2 additions & 5 deletions src/dynamic/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ void core_construct(core *core)
core = core_get(core);
if (!core->ref)
{
core = core_get(core);
vector_construct(&core->handlers, sizeof(core_handler));
vector_construct(&core->next, sizeof(core_handler));
core->fd = epoll_create1(EPOLL_CLOEXEC);
Expand All @@ -51,6 +50,7 @@ void core_destruct(core *core)
(void) close(core->fd);
vector_destruct(&core->handlers, NULL);
vector_destruct(&core->next, NULL);
*core = (struct core) {0};
}
}

Expand All @@ -71,7 +71,7 @@ void core_loop(core *core)
(void) core_dispatch(vector_at(&core->next, i), 0, 0);
vector_clear(&core->next, NULL);

n = epoll_wait(core->fd, events, CORE_MAX_EVENTS, -1);
n = core->handlers_active ? epoll_wait(core->fd, events, CORE_MAX_EVENTS, -1) : 0;
if (n == -1)
core->errors ++;

Expand Down Expand Up @@ -122,9 +122,6 @@ void core_delete(core *core, int fd)
int e;

core = core_get(core);
if (fd < 0)
return;

e = epoll_ctl(core->fd, EPOLL_CTL_DEL, fd, NULL);
if (e == -1)
core->errors ++;
Expand Down
137 changes: 137 additions & 0 deletions test/core.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <setjmp.h>
#include <signal.h>
#include <netdb.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/queue.h>

#include <cmocka.h>

#include "../src/dynamic/buffer.h"
#include "../src/dynamic/vector.h"
#include "../src/dynamic/core.h"

extern int debug_epoll_create1;
extern int debug_epoll_ctl;
extern int debug_epoll_wait;

static core_status next(core_event *event)
{
int *n = event->state;

(*n)++;
return CORE_OK;
}

static core_status in(core_event *event)
{
int *fd = event->state;

core_delete(NULL, *fd);
return CORE_ABORT;
}

static void basic()
{
core c;
int n, id, e, p[2];

/* construct and destruct local instance */
core_construct(&c);
core_loop(&c);
core_destruct(&c);

/* construct and destruct */
core_construct(NULL);
core_construct(NULL);
core_destruct(NULL);
core_destruct(NULL);

/* run empty core */
core_construct(NULL);
core_loop(NULL);
core_destruct(NULL);

/* add, modify and remove fd */
core_construct(NULL);
core_add(NULL, NULL, NULL, 0, 0);
core_modify(NULL, 0, EPOLLIN);
core_delete(NULL, 0);
core_loop(NULL);
core_destruct(NULL);

/* poll event */
e = pipe(p);
assert_int_equal(e, 0);
e = write(p[1], ".", 1);
assert_int_equal(e, 1);
core_construct(NULL);
core_add(NULL, in, &p[0], p[0], EPOLLIN);
core_loop(NULL);
core_destruct(NULL);

/* next, cancel */
n = 0;
core_construct(NULL);
core_next(NULL, next, &n);
id = core_next(NULL, next, &n);
core_cancel(NULL, id);
core_cancel(NULL, 0);
core_loop(NULL);
core_destruct(NULL);
assert_int_equal(n, 1);
}

static void error()
{
/* epoll_create1 error */
debug_epoll_create1 = 1;
core_construct(NULL);
assert_int_equal(core_errors(NULL), 1);
core_destruct(NULL);
assert_int_equal(core_errors(NULL), 0);
debug_epoll_create1 = 0;

/* epoll_ctl error */
debug_epoll_ctl = 1;
core_construct(NULL);
core_add(NULL, NULL, NULL, 0, 0);
core_modify(NULL, 0, 0);
core_delete(NULL, 0);
assert_int_equal(core_errors(NULL), 3);
core_destruct(NULL);
debug_epoll_ctl = 0;

/* epoll_wait */
debug_epoll_wait = 1;
core_construct(NULL);
core_add(NULL, NULL, NULL, 0, 0);
core_loop(NULL);
assert_int_equal(core_errors(NULL), 1);
core_destruct(NULL);
debug_epoll_wait = 0;
}

int main()
{
int e;

const struct CMUnitTest tests[] =
{
cmocka_unit_test(basic),
cmocka_unit_test(error)
};

e = cmocka_run_group_tests(tests, NULL, NULL);
(void) close(0);
(void) close(1);
(void) close(2);
return e;
}
3 changes: 2 additions & 1 deletion test/coverage.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh

for file in hash buffer list vector string maps mapi map pool
#for file in hash buffer list vector string maps mapi map pool
for file in buffer core hash list map mapi maps pool string vector
do
echo [$file]
test=`gcov -b src/dynamic/libdynamic_test_a-$file | grep -A4 File.*$file`
Expand Down
23 changes: 23 additions & 0 deletions test/mock.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <setjmp.h>

#include <cmocka.h>

int debug_out_of_memory = 0;
Expand All @@ -9,6 +11,9 @@ int debug_recv = 0;
int debug_send = 0;
int debug_pthread_create = 0;
int debug_socketpair = 0;
int debug_epoll_create1 = 0;
int debug_epoll_ctl = 0;
int debug_epoll_wait = 0;

void *__real_malloc(size_t);
void *__wrap_malloc(size_t size)
Expand Down Expand Up @@ -66,3 +71,21 @@ int __wrap_socketpair(int domain, int type, int protocol, int socket_vector[2])
{
return debug_socketpair ? -1 : __real_socketpair(domain, type, protocol, socket_vector);
}

int __real_epoll_create1(int);
int __wrap_epoll_create1(int flags)
{
return debug_epoll_create1 ? -1 : __real_epoll_create1(flags);
}

int __real_epoll_ctl(int, int ,int, struct epoll_event *);
int __wrap_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
{
return debug_epoll_ctl ? -1 : __real_epoll_ctl(epfd, op, fd, event);
}

int __real_epoll_wait(int, struct epoll_event *, int, int);
int __wrap_epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout)
{
return debug_epoll_wait ? -1 : __real_epoll_wait(epfd, events, maxevents, timeout);
}

0 comments on commit 14f36d5

Please sign in to comment.