src/test.c
changeset 90 9d489b1039b2
parent 89 68345a9b99a3
child 91 bca23cbe1dce
--- a/src/test.c	Mon Mar 30 14:45:14 2009 +0300
+++ b/src/test.c	Mon Mar 30 16:31:24 2009 +0300
@@ -3,6 +3,7 @@
  */
 #include "sock_test.h"
 #include "line_proto.h"
+#include "irc_queue.h"
 #include "irc_conn.h"
 #include "irc_net.h"
 #include "log.h"
@@ -18,6 +19,15 @@
 #define DUMP_STR_COUNT 8
 #define DUMP_STR_TAIL 10
 
+/**
+ * Global test-running state
+ */
+struct test_ctx {
+    /** The event_base that we have setup */
+    struct event_base *ev_base;
+
+} _test_ctx;
+
 char *dump_str_append (char *buf, const char *str)
 {
     while (*str)
@@ -257,6 +267,20 @@
 }
 
 /**
+ * Setup the global sock_stream state
+ */
+struct event_base* setup_sock (void)
+{
+    struct event_base *ev_base;
+    struct error_info err;
+
+    assert((ev_base = event_base_new()));
+    assert_success(sock_init(ev_base, &err));
+
+    return ev_base;
+}
+
+/**
  * Create an empty sock_test
  */
 struct sock_test* setup_sock_test (void)
@@ -416,6 +440,63 @@
     line_proto_release(lp);
 }
 
+void test_irc_queue (void)
+{
+    struct sock_test *sock = sock_test_create();
+    struct line_proto *lp;
+    struct irc_queue *queue;
+    struct irc_queue_entry *queue_entry;
+    struct error_info err;
+
+    // create the lp
+    assert_success(line_proto_create(&lp, SOCK_TEST_BASE(sock), 128, &_lp_callbacks, NULL, &err));
+
+    // create the queue
+    assert_success(irc_queue_create(&queue, lp, &err));
+
+    struct irc_line line = {
+        NULL, "TEST", { "fooX" }
+    };
+
+    // then test simple writes, we should be able to push five lines directly
+    log_info("test irc_queue_process (irc_queue_send_direct)");
+    line.args[0] = "foo0"; assert_success(irc_queue_process(queue, &line));
+    line.args[0] = "foo1"; assert_success(irc_queue_process(queue, &line));
+    line.args[0] = "foo2"; assert_success(irc_queue_process(queue, &line));
+    line.args[0] = "foo3"; assert_success(irc_queue_process(queue, &line));
+    line.args[0] = "foo4"; assert_success(irc_queue_process(queue, &line));
+
+    // they should all be output
+    assert_sock_data(sock, 
+            "TEST foo0\r\n"
+            "TEST foo1\r\n"
+            "TEST foo2\r\n"
+            "TEST foo3\r\n"
+            "TEST foo4\r\n"
+    );
+
+    // then enqueue
+    log_info("test irc_queue_process (irc_queue_put)");
+    line.args[0] = "foo5"; assert_success(irc_queue_process(queue, &line));
+
+    // ensure it was enqueued
+    assert((queue_entry = TAILQ_FIRST(&queue->list)) != NULL);
+    assert_strcmp(queue_entry->line_buf, "TEST foo5\r\n");
+
+    // ensure timer is set
+    assert(event_pending(queue->ev, EV_TIMEOUT, NULL));
+
+    // run the event loop to let the timer run
+    log_info("running the event loop once...");
+    assert(event_base_loop(_test_ctx.ev_base, EVLOOP_ONCE) == 0);
+
+    // test to check that the line was now sent
+    log_info("checking that the delayed line was sent...");
+    assert_sock_data(sock, "TEST foo5\r\n");
+    assert(TAILQ_EMPTY(&queue->list));
+    assert(!event_pending(queue->ev, EV_TIMEOUT, NULL));
+}
+
 struct test_conn_ctx {
     /** Callback flags */
     bool on_registered, on_TEST, on_error, on_quit;
@@ -994,6 +1075,7 @@
     {   "dump_str",             &test_dump_str              },
     {   "sock_test",            &test_sock_test             },
     {   "line_proto",           &test_line_proto            },
+    {   "irc_queue",            &test_irc_queue             },
     // XXX: irc_line_parse_invalid_prefix
     {   "irc_conn",             &test_irc_conn              },
     {   "irc_conn_self_nick",   &test_irc_conn_self_nick    },
@@ -1017,6 +1099,7 @@
     OPT_HELP            = 'h',
     OPT_DEBUG           = 'd',
     OPT_QUIET           = 'q',
+    OPT_LIST            = 'l',
     
     /** Options without short names */
     _OPT_EXT_BEGIN      = 0x00ff,
@@ -1029,6 +1112,7 @@
     {"help",            0,  NULL,   OPT_HELP        },
     {"debug",           0,  NULL,   OPT_DEBUG       },
     {"quiet",           0,  NULL,   OPT_QUIET       },
+    {"list",            0,  NULL,   OPT_LIST        },
     {0,                 0,  0,      0               },
 };
 
@@ -1042,6 +1126,18 @@
     printf(" --help / -h            display this message\n");
     printf(" --debug / -d           display DEBUG log messages\n");
     printf(" --quiet / -q           supress INFO log messages\n");
+    printf(" --list / -l            list all tests\n");
+}
+
+static void list_tests (struct test *tests)
+{
+    struct test *test;
+    
+    printf("Available tests:\n");
+
+    for (test = tests; test->name; test++) {
+        printf("\t%s\n", test->name);
+    }
 }
 
 int main (int argc, char **argv)
@@ -1053,7 +1149,7 @@
     const char *filter = NULL;
 
     // parse options
-    while ((opt = getopt_long(argc, argv, "hdq", options, &option_index)) != -1) {
+    while ((opt = getopt_long(argc, argv, "hdql", options, &option_index)) != -1) {
         switch (opt) {
             case OPT_HELP:
                 usage(argv[0]);
@@ -1066,7 +1162,11 @@
             case OPT_QUIET:
                 set_log_level(LOG_WARN);
                 break;
-            
+           
+            case OPT_LIST:
+                list_tests(_tests);
+                exit(EXIT_SUCCESS);
+
             case '?':
                 usage(argv[0]);
                 exit(EXIT_FAILURE);
@@ -1084,6 +1184,9 @@
         }
     }
 
+    // setup the sockets stuff
+    _test_ctx.ev_base = setup_sock();
+
     // run tests
     for (test = _tests; test->name; test++) {
         if (filter && strcmp(test->name, filter))