Skip to content
Snippets Groups Projects
Commit cd72179d authored by Marek Vavrusa's avatar Marek Vavrusa
Browse files

Implemented fdset tests (poll working).

refs #983
parent 85ae8087
Branches
Tags
No related merge requests found
...@@ -182,6 +182,8 @@ src/tests/common/skiplist_tests.c ...@@ -182,6 +182,8 @@ src/tests/common/skiplist_tests.c
src/tests/common/skiplist_tests.h src/tests/common/skiplist_tests.h
src/tests/common/slab_tests.c src/tests/common/slab_tests.c
src/tests/common/slab_tests.h src/tests/common/slab_tests.h
src/tests/common/os_fdset_tests.c
src/tests/common/os_fdset_tests.h
src/tests/knot/dthreads_tests.c src/tests/knot/dthreads_tests.c
src/tests/knot/dthreads_tests.h src/tests/knot/dthreads_tests.h
src/tests/knot/conf_tests.c src/tests/knot/conf_tests.c
......
...@@ -58,6 +58,8 @@ unittests_SOURCES = \ ...@@ -58,6 +58,8 @@ unittests_SOURCES = \
tests/common/skiplist_tests.h \ tests/common/skiplist_tests.h \
tests/common/slab_tests.c \ tests/common/slab_tests.c \
tests/common/slab_tests.h \ tests/common/slab_tests.h \
tests/common/os_fdset_tests.c \
tests/common/os_fdset_tests.h \
tests/knot/conf_tests.c \ tests/knot/conf_tests.c \
tests/knot/conf_tests.h \ tests/knot/conf_tests.h \
tests/knot/dthreads_tests.c \ tests/knot/dthreads_tests.c \
......
#include <stdlib.h>
#include <stdint.h>
#include "tests/common/os_fdset_tests.h"
#include "common/os_fdset.h"
static int os_fdset_tests_count(int argc, char *argv[]);
static int os_fdset_tests_run(int argc, char *argv[]);
/*! Exported unit API.
*/
unit_api os_fdset_tests_api = {
"Native fdset poll wrapper", //! Unit name
&os_fdset_tests_count, //! Count scheduled tests
&os_fdset_tests_run //! Run scheduled tests
};
static int os_fdset_tests_count(int argc, char *argv[])
{
return 11;
}
static int os_fdset_tests_run(int argc, char *argv[])
{
diag("os_fdset: implements '%s'", os_fdset_method());
/* 1. Create fdset. */
os_fdset_t *set = os_fdset_new();
ok(set != 0, "os_fdset: new");
/* 2. Create pipe. */
int fds[2];
int ret = pipe(fds);
ok(ret >= 0, "os_fdset: pipe() works");
/* 3. Add fd to set. */
ret = os_fdset_add(set, fds[0], OS_EV_READ);
ok(ret == 0, "os_fdset: add to set works");
/* 4. Watch fdset. */
const char pattern = 0xde;
ret = write(fds[1], &pattern, 1);
ret = os_fdset_poll(set);
ok(ret > 0, "os_fdset: poll returned events");
/* 5. Prepare event set. */
os_fdset_it it;
ret = os_fdset_begin(set, &it);
ok(ret == 0, "os_fdset: begin is valid");
/* 6. Receive data. */
char buf;
ret = read(it.fd, &buf, 1);
ok(ret >= 0 && buf == pattern, "os_fdset: contains valid data");
/* 7. Iterate event set. */
ret = os_fdset_next(set, &it);
ok(ret < 0, "os_fdset: boundary check works");
/* 8. Remove from event set. */
ret = os_fdset_remove(set, fds[0]);
ok(ret == 0, "os_fdset: remove from fdset works");
close(fds[0]);
close(fds[1]);
/* 9. Poll empty fdset. */
ret = os_fdset_poll(set);
ok(ret <= 0, "os_fdset: polling empty fdset returns");
/* 10. Crash test. */
lives_ok({
os_fdset_destroy(0);
os_fdset_add(0, -1, 0);
os_fdset_remove(0, -1);
os_fdset_poll(0);
os_fdset_begin(0, 0);
os_fdset_end(0, 0);
os_fdset_next(0, 0);
os_fdset_method();
}, "os_fdset: crash test successful");
/* 11. Destroy fdset. */
ret = os_fdset_destroy(set);
ok(ret == 0, "os_fdset: destroyed");
return 0;
}
#ifndef _KNOTD_OS_FDSET_TESTS_H_
#define _KNOTD_OS_FDSET_TESTS_H_
#include "common/libtap/tap_unit.h"
/* Unit API. */
unit_api os_fdset_tests_api;
#endif /* _KNOTD_OS_FDSET_TESTS_H_ */
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "tests/common/events_tests.h" #include "tests/common/events_tests.h"
#include "tests/common/da_tests.h" #include "tests/common/da_tests.h"
#include "tests/common/acl_tests.h" #include "tests/common/acl_tests.h"
#include "tests/common/os_fdset_tests.h"
#include "tests/knot/dthreads_tests.h" #include "tests/knot/dthreads_tests.h"
#include "tests/knot/journal_tests.h" #include "tests/knot/journal_tests.h"
#include "tests/knot/server_tests.h" #include "tests/knot/server_tests.h"
...@@ -30,6 +31,7 @@ int main(int argc, char *argv[]) ...@@ -30,6 +31,7 @@ int main(int argc, char *argv[])
&events_tests_api, //! Events testing unit &events_tests_api, //! Events testing unit
&da_tests_api, //! Dynamic array unit &da_tests_api, //! Dynamic array unit
&acl_tests_api, //! ACLs &acl_tests_api, //! ACLs
&os_fdset_tests_api, //! FDSET polling wrapper
/* Server parts. */ /* Server parts. */
&conf_tests_api, //! Configuration parser tests &conf_tests_api, //! Configuration parser tests
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment