commit 0af88f909661b8fd2987beb839c78b804212abc7 Author: Andrea Shepard andrea@torproject.org Date: Thu Jan 23 20:55:34 2014 -0800
Initial test_scheduler.c --- src/test/Makefile.nmake | 8 +-- src/test/include.am | 1 + src/test/test.c | 2 + src/test/test_scheduler.c | 141 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+), 4 deletions(-)
diff --git a/src/test/Makefile.nmake b/src/test/Makefile.nmake index 7986eb0..e2cf538 100644 --- a/src/test/Makefile.nmake +++ b/src/test/Makefile.nmake @@ -12,10 +12,10 @@ LIBS = ......\build-alpha\lib\libevent.lib \ crypt32.lib gdi32.lib user32.lib
TEST_OBJECTS = test.obj test_addr.obj test_channel.obj test_channeltls.obj \ - test_containers.obj test_controller_events.obj test_crypto.obj test_data.obj \ - test_dir.obj test_microdesc.obj test_pt.obj test_util.obj test_config.obj \ - test_cell_formats.obj test_relay.obj test_replay.obj \ - test_introduce.obj tinytest.obj test_hs.obj + test_containers.obj test_controller_events.obj test_crypto.obj \ + test_data.obj test_dir.obj test_microdesc.obj test_pt.obj test_util.obj \ + test_config.obj test_cell_formats.obj test_relay.obj test_replay.obj \ + test_scheduler.obj test_introduce.obj test_hs.obj tinytest.obj
tinytest.obj: ..\ext\tinytest.c $(CC) $(CFLAGS) /D snprintf=_snprintf /c ..\ext\tinytest.c diff --git a/src/test/include.am b/src/test/include.am index fdd88db..5a8652b 100644 --- a/src/test/include.am +++ b/src/test/include.am @@ -42,6 +42,7 @@ src_test_test_SOURCES = \ src/test/test_relay.c \ src/test/test_replay.c \ src/test/test_routerkeys.c \ + src/test/test_scheduler.c \ src/test/test_socks.c \ src/test/test_util.c \ src/test/test_config.c \ diff --git a/src/test/test.c b/src/test/test.c index d0f0041..8d45ef0 100644 --- a/src/test/test.c +++ b/src/test/test.c @@ -1312,6 +1312,7 @@ extern struct testcase_t router_tests[]; extern struct testcase_t channel_tests[]; extern struct testcase_t channeltls_tests[]; extern struct testcase_t relay_tests[]; +extern struct testcase_t scheduler_tests[];
static struct testgroup_t testgroups[] = { { "", test_array }, @@ -1347,6 +1348,7 @@ static struct testgroup_t testgroups[] = { { "channel/", channel_tests }, { "channeltls/", channeltls_tests }, { "relay/" , relay_tests }, + { "scheduler/", scheduler_tests }, END_OF_GROUPS };
diff --git a/src/test/test_scheduler.c b/src/test/test_scheduler.c new file mode 100644 index 0000000..e8bceeb --- /dev/null +++ b/src/test/test_scheduler.c @@ -0,0 +1,141 @@ +/* Copyright (c) 2014, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +#include <math.h> + +#include "orconfig.h" + +/* Libevent stuff */ +#ifdef HAVE_EVENT2_EVENT_H +#include <event2/event.h> +#else +#include <event.h> +#endif + +#define TOR_CHANNEL_INTERNAL_ +#include "or.h" +#include "compat_libevent.h" +#include "scheduler.h" + +/* Test suite stuff */ +#include "test.h" + +/* Statics in scheduler.c exposed to the test suite */ +extern smartlist_t *channels_pending; +extern struct event *run_sched_ev; +extern uint64_t queue_heuristic; +extern time_t queue_heuristic_timestamp; + +/* Event base for scheduelr tests */ +static struct event_base *mock_event_base = NULL; + +/* Setup for mock event stuff */ +static void mock_event_free_all(void); +static void mock_event_init(void); + +/* Mocks used by scheduler tests */ +static struct event_base * tor_libevent_get_base_mock(void); + +/* Scheduler test cases */ +static void test_scheduler_initfree(void *arg); + +/* Mock event init/free */ + +/* Shamelessly stolen from compat_libevent.c */ +#define V(major, minor, patch) \ + (((major) << 24) | ((minor) << 16) | ((patch) << 8)) + +static void +mock_event_free_all(void) +{ + test_assert(mock_event_base != NULL); + + if (mock_event_base) { + event_base_free(mock_event_base); + mock_event_base = NULL; + } + + test_eq(mock_event_base, NULL); + + done: + return; +} + +static void +mock_event_init(void) +{ +#ifdef HAVE_EVENT2_EVENT_H + struct event_config *cfg = NULL; +#endif + + test_eq(mock_event_base, NULL); + + /* + * Really cut down from tor_libevent_initialize of + * src/common/compat_libevent.c to kill config dependencies + */ + + if (!mock_event_base) { +#ifdef HAVE_EVENT2_EVENT_H + cfg = event_config_new(); +#if LIBEVENT_VERSION_NUMBER >= V(2,0,9) + /* We can enable changelist support with epoll, since we don't give + * Libevent any dup'd fds. This lets us avoid some syscalls. */ + event_config_set_flag(cfg, EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST); +#endif + mock_event_base = event_base_new_with_config(cfg); + event_config_free(cfg); +#else + mock_event_base = event_init(); +#endif + } + + test_assert(mock_event_base != NULL); + + done: + return; +} + +/* Mocks */ + +static struct event_base * +tor_libevent_get_base_mock(void) +{ + return mock_event_base; +} + +/* Test cases */ + +static void +test_scheduler_initfree(void *arg) +{ + (void)arg; + + test_eq(channels_pending, NULL); + test_eq(run_sched_ev, NULL); + + mock_event_init(); + MOCK(tor_libevent_get_base, tor_libevent_get_base_mock); + + scheduler_init(); + + test_assert(channels_pending != NULL); + test_assert(run_sched_ev != NULL); + + scheduler_free_all(); + + UNMOCK(tor_libevent_get_base); + mock_event_free_all(); + + test_eq(channels_pending, NULL); + test_eq(run_sched_ev, NULL); + + done: + return; +} + +struct testcase_t scheduler_tests[] = { + { "initfree", test_scheduler_initfree, TT_FORK, NULL, NULL }, + END_OF_TESTCASES +}; +