aboutsummaryrefslogtreecommitdiffstats
path: root/test/rspamd_memcached_test.c
diff options
context:
space:
mode:
authorcebka@mailsupport.rambler.ru <cebka@mailsupport.rambler.ru>2008-09-16 18:42:02 +0400
committercebka@mailsupport.rambler.ru <cebka@mailsupport.rambler.ru>2008-09-16 18:42:02 +0400
commit4ad814a6c631883bbb55ae3db2f8806c63f31c9a (patch)
tree93cc3253d8046dfa7ca28f64c2cb01546e32eead /test/rspamd_memcached_test.c
parent9e442d87b2907ff011a80b8a795c6710b6665705 (diff)
downloadrspamd-4ad814a6c631883bbb55ae3db2f8806c63f31c9a.tar.gz
rspamd-4ad814a6c631883bbb55ae3db2f8806c63f31c9a.zip
* Write test case for async memcached library
* Fix memcached async library to pass test :)
Diffstat (limited to 'test/rspamd_memcached_test.c')
-rw-r--r--test/rspamd_memcached_test.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/test/rspamd_memcached_test.c b/test/rspamd_memcached_test.c
new file mode 100644
index 000000000..1993c756e
--- /dev/null
+++ b/test/rspamd_memcached_test.c
@@ -0,0 +1,85 @@
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <sys/param.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <syslog.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <event.h>
+
+#include "../config.h"
+#include "../main.h"
+#include "../cfg_file.h"
+#include "../memcached.h"
+#include "tests.h"
+
+u_char *buf = "test";
+
+static void
+memcached_callback (memcached_ctx_t *ctx, memc_error_t error, void *data)
+{
+ struct timeval tv;
+
+ switch (ctx->op) {
+ case CMD_CONNECT:
+ g_assert (error == OK);
+ msg_debug ("Connect ok");
+ memc_set (ctx, ctx->param, 60);
+ break;
+ case CMD_READ:
+ g_assert (error == OK);
+ g_assert (!strcmp(ctx->param->buf, buf));
+ msg_debug ("Read ok");
+ memc_close_ctx (ctx);
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ event_loopexit (&tv);
+ break;
+ case CMD_WRITE:
+ g_assert (error == OK);
+ msg_debug ("Write ok");
+ ctx->param->buf = g_malloc (sizeof (buf));
+ bzero (ctx->param->buf, sizeof (buf));
+ memc_get (ctx, ctx->param);
+ break;
+ }
+}
+
+void
+rspamd_memcached_test_func ()
+{
+ memcached_ctx_t *ctx;
+ memcached_param_t *param;
+ struct in_addr addr;
+
+ ctx = g_malloc (sizeof (memcached_ctx_t));
+ param = g_malloc (sizeof (memcached_param_t));
+ bzero (ctx, sizeof (memcached_ctx_t));
+ bzero (param, sizeof (memcached_param_t));
+
+ event_init ();
+
+ ctx->callback = memcached_callback;
+ ctx->callback_data = (void *)param;
+ ctx->protocol = TCP_TEXT;
+ inet_aton ("127.0.0.1", &addr);
+ memcpy (&ctx->addr, &addr, sizeof (struct in_addr));
+ ctx->port = htons (11211);
+ ctx->timeout.tv_sec = 1;
+ ctx->timeout.tv_usec = 0;
+ ctx->sock = -1;
+ ctx->options = MEMC_OPT_DEBUG;
+ strlcpy (param->key, buf, sizeof (param->key));
+ param->buf = buf;
+ param->bufsize = strlen (buf);
+ ctx->param = param;
+ g_assert (memc_init_ctx (ctx) != -1);
+
+ event_loop (0);
+}
+