From 4f28edb3f55048024301068417be00106ebdd09a Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 1 Mar 2012 21:57:09 +0400 Subject: Fix asynchronous IO API. Write test case for aio. --- test/CMakeLists.txt | 3 +- test/rspamd_async_test.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ test/rspamd_dns_test.c | 6 ++-- test/rspamd_test_suite.c | 1 + test/tests.h | 3 ++ 5 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 test/rspamd_async_test.c (limited to 'test') diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c15f77b75..f63f6402b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -5,7 +5,8 @@ SET(TESTSRC rspamd_expression_test.c rspamd_fuzzy_test.c rspamd_test_suite.c rspamd_url_test.c - rspamd_dns_test.c) + rspamd_dns_test.c + rspamd_async_test.c) ADD_EXECUTABLE(rspamd-test EXCLUDE_FROM_ALL ${TESTSRC}) SET_TARGET_PROPERTIES(rspamd-test PROPERTIES LINKER_LANGUAGE C) diff --git a/test/rspamd_async_test.c b/test/rspamd_async_test.c new file mode 100644 index 000000000..f2c6e6421 --- /dev/null +++ b/test/rspamd_async_test.c @@ -0,0 +1,87 @@ +/* Copyright (c) 2011, Vsevolod Stakhov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "tests.h" +#include "main.h" +#include "aio_event.h" + + +extern struct event_base *base; + +static void +aio_read_cb (gint fd, gint res, gsize len, gpointer data, gpointer ud) +{ + guchar *p = data; + guint i; + + g_assert (res != -1); + + g_assert (len == BUFSIZ); + for (i = 0; i < len; i ++) { + g_assert (p[i] == 0xef); + } + + event_base_loopbreak (base); +} + +static void +aio_write_cb (gint fd, gint res, gsize len, gpointer data, gpointer ud) +{ + struct aio_context *aio_ctx = ud; + static gchar testbuf[BUFSIZ]; + + g_assert (res != -1); + + g_assert (rspamd_aio_read (fd, testbuf, sizeof (testbuf), aio_ctx, aio_read_cb, aio_ctx) != -1); +} + +void +rspamd_async_test_func () +{ + struct aio_context *aio_ctx; + gchar *tmpfile; + static gchar testbuf[BUFSIZ]; + gint fd, afd, ret; + + aio_ctx = rspamd_aio_init (base); + + g_assert (aio_ctx != NULL); + + fd = g_file_open_tmp ("raXXXXXX", &tmpfile, NULL); + g_assert (fd != -1); + + afd = rspamd_aio_open (aio_ctx, tmpfile, O_RDWR); + g_assert (fd != -1); + + /* Write some data */ + memset (testbuf, 0xef, sizeof (testbuf)); + ret = rspamd_aio_write (afd, testbuf, sizeof (testbuf), aio_ctx, aio_write_cb, aio_ctx); + g_assert (ret != -1); + + event_base_loop (base, 0); + + close (afd); + close (fd); + unlink (tmpfile); +} diff --git a/test/rspamd_dns_test.c b/test/rspamd_dns_test.c index 839c00a79..9fa1d4901 100644 --- a/test/rspamd_dns_test.c +++ b/test/rspamd_dns_test.c @@ -69,6 +69,7 @@ rspamd_dns_test_func () struct config_file *cfg; memory_pool_t *pool; struct rspamd_async_session *s; + struct in_addr ina; cfg = (struct config_file *)g_malloc (sizeof (struct config_file)); bzero (cfg, sizeof (struct config_file)); @@ -78,15 +79,16 @@ rspamd_dns_test_func () pool = memory_pool_new (memory_pool_get_size ()); - event_init (); s = new_async_session (pool, session_fin, NULL, NULL, NULL); resolver = dns_resolver_init (base, cfg); + ina.s_addr = inet_addr ("81.19.70.3"); + requests ++; g_assert (make_dns_request (resolver, s, pool, test_dns_cb, NULL, DNS_REQUEST_A, "google.com")); requests ++; - g_assert (make_dns_request (resolver, s, pool, test_dns_cb, NULL, DNS_REQUEST_PTR, inet_addr ("81.19.70.3"))); + g_assert (make_dns_request (resolver, s, pool, test_dns_cb, NULL, DNS_REQUEST_PTR, &ina)); requests ++; g_assert (make_dns_request (resolver, s, pool, test_dns_cb, NULL, DNS_REQUEST_MX, "rambler.ru")); requests ++; diff --git a/test/rspamd_test_suite.c b/test/rspamd_test_suite.c index f035fcb81..c23f6fca2 100644 --- a/test/rspamd_test_suite.c +++ b/test/rspamd_test_suite.c @@ -63,6 +63,7 @@ main (int argc, char **argv) g_test_add_func ("/rspamd/expression", rspamd_expression_test_func); g_test_add_func ("/rspamd/statfile", rspamd_statfile_test_func); g_test_add_func ("/rspamd/dns", rspamd_dns_test_func); + g_test_add_func ("/rspamd/aio", rspamd_async_test_func); g_test_run (); diff --git a/test/tests.h b/test/tests.h index ee05c903d..be2c29fca 100644 --- a/test/tests.h +++ b/test/tests.h @@ -26,4 +26,7 @@ void rspamd_statfile_test_func (); /* DNS resolving */ void rspamd_dns_test_func (); +/* Async IO */ +void rspamd_async_test_func (); + #endif -- cgit v1.2.3