test async operation
authorTero Marttila <terom@fixme.fi>
Thu, 12 Mar 2009 21:23:33 +0200
changeset 42 13cfc41f76a7
parent 41 40f7aa051acb
child 43 42f5dc680930
test async operation
src/sock_test.c
src/sock_test.h
src/test.c
--- a/src/sock_test.c	Thu Mar 12 21:12:48 2009 +0200
+++ b/src/sock_test.c	Thu Mar 12 21:23:33 2009 +0200
@@ -129,6 +129,9 @@
 {
     struct sock_test *sock = SOCK_FROM_BASE(base_sock, struct sock_test);
 
+    // store mask
+    sock->ev_mask = mask;
+
     return SUCCESS;
 }
 
@@ -212,6 +215,14 @@
     
     // copy    
     *(buf->write_vec++) = new_vec;
+
+    // notify events?
+    if (sock->ev_mask) {
+        int mask = sock->ev_mask;
+        sock->ev_mask = 0;
+
+        sock_stream_invoke_callbacks(SOCK_TEST_BASE(sock), mask);
+    }
 }
 
 void sock_test_get_send_data (struct sock_test *sock, char **buf_ptr, size_t *len_ptr)
--- a/src/sock_test.h	Thu Mar 12 21:12:48 2009 +0200
+++ b/src/sock_test.h	Thu Mar 12 21:23:33 2009 +0200
@@ -52,6 +52,9 @@
 
     /** No more data is going to be added, return EOF once all the rest is consumed */
     bool eof;
+
+    /** event flags */
+    int ev_mask;
 };
 
 /**
@@ -84,7 +87,9 @@
 void sock_test_set_recv_buffer (struct sock_test *sock, struct io_vec *vecs, size_t count, bool eof);
 
 /**
- * Add some data to the recieve buffer
+ * Add some data to the recieve buffer.
+ *
+ * If events are enabled, they are triggered.
  */
 void sock_test_add_recv_vec (struct sock_test *sock, struct io_vec vec);
 
--- a/src/test.c	Thu Mar 12 21:12:48 2009 +0200
+++ b/src/test.c	Thu Mar 12 21:23:33 2009 +0200
@@ -11,16 +11,16 @@
 #include <string.h>
 #include <assert.h>
 
-void assert_strcmp (const char *a, const char *b)
+void assert_strcmp (const char *is, const char *should_be)
 {
-    if (strcmp(a, b))
-        FATAL("'%s' != '%s'", a, b);
+    if (strcmp(is, should_be))
+        FATAL("'%s' != '%s'", is, should_be);
 }
 
-void assert_strncmp (const char *a, const char *b, size_t n)
+void assert_strncmp (const char *is, const char *should_be, size_t n)
 {
-    if (strncmp(a, b, n))
-        FATAL("'%s':%d != '%s'", a, n, b);
+    if (strncmp(is, should_be, n))
+        FATAL("'%s':%d != '%s'", is, n, should_be);
 }
 
 void assert_strlen (const char *str, size_t n)
@@ -29,7 +29,7 @@
         FATAL("strlen('%s') != %u", str, n);
 }
 
-void assert_strnul (const char *str)
+void assert_strnull (const char *str)
 {
     if (str != NULL)
         FATAL("'%s' != NULL", str);
@@ -45,7 +45,13 @@
 void assert_err (err_t err, err_t target)
 {
     if (err != target)
-        FATAL("error: <%s> != target <%s>", error_name(err), error_name(target));
+        FATAL("error: <%s> != <%s>", error_name(err), error_name(target));
+}
+
+void assert_error_info (struct error_info *is, struct error_info *should_be)
+{
+    if (ERROR_CODE(is) != ERROR_CODE(should_be) || ERROR_EXTRA(is) != ERROR_EXTRA(should_be))
+        FATAL("error: <%s> != <%s>", error_msg(is), error_msg(should_be));
 }
 
 void assert_sock_read (struct sock_stream *sock, const char *str)
@@ -135,14 +141,43 @@
         assert_strcmp(line_buf, line_str);
 
     } else {
-        assert_strnul(line_buf);
+        assert_strnull(line_buf);
 
     }
 }
 
+/**
+ * Context info for test_line_proto callbacks
+ */
+struct _lp_test_ctx {
+    /** Expected line */
+    const char *line;
+
+    /** Expected error */
+    struct error_info err;
+};
+
+static void _lp_on_line (char *line, void *arg)
+{
+    struct _lp_test_ctx *ctx = arg;
+
+    log_debug("'%s'", line);
+
+    assert_strcmp(line, ctx->line);
+
+    ctx->line = NULL;
+}
+
+static void _lp_on_error (struct error_info *err, void *arg)
+{
+    struct _lp_test_ctx *ctx = arg;
+
+    assert_error_info(err, &ctx->err);
+}
+
 static struct line_proto_callbacks _lp_callbacks = {
-    .on_line        = NULL,
-    .on_error       = NULL,
+    .on_line        = &_lp_on_line,
+    .on_error       = &_lp_on_error,
 };
 
 void test_line_proto (void)
@@ -156,6 +191,7 @@
         {   "\nfragment",   9   },
     }, _trailing_data = {   "\r\n",     2 };
     struct line_proto *lp;
+    struct _lp_test_ctx ctx;
     struct error_info err;
     
     // put the read data
@@ -163,7 +199,7 @@
     sock_test_set_recv_buffer(sock, _read_data, 5, false);
     
     // create the lp
-    assert_success(line_proto_create(&lp, SOCK_TEST_BASE(sock), 128, &_lp_callbacks, NULL, &err));
+    assert_success(line_proto_create(&lp, SOCK_TEST_BASE(sock), 128, &_lp_callbacks, &ctx, &err));
     
     log_info("test line_proto_recv");
 
@@ -173,11 +209,12 @@
     assert_read_line(lp, "this is a line");
     assert_read_line(lp, NULL);
 
-    // then add a final bit
+    // then add a final bit to trigger on_line
+    log_info("test on_line");
+
+    ctx.line = "fragment";
     sock_test_add_recv_vec(sock, _trailing_data);
-
-    // read the final bit
-    assert_read_line(lp, "fragment");
+    assert_strnull(ctx.line);
 
     // cleanup
     line_proto_release(lp);