about summary refs log tree commit diff stats
path: root/test
diff options
context:
space:
mode:
authorFedor Indutny <fedor@indutny.com>2016-06-03 13:59:37 -0400
committerFedor Indutny <fedor@indutny.com>2016-06-03 13:59:37 -0400
commit88ffbdbebace7624f934e91c97731f18f3f8ee03 (patch)
tree18183f3e21aa2551a675bf201c9f2621484501c2 /test
parent1d9649b1ae65b2b119b01a376f5c232982af1b89 (diff)
downloaduv_link_t-88ffbdbebace7624f934e91c97731f18f3f8ee03.tar.gz
test: add test for close protection
Diffstat (limited to 'test')
-rw-r--r--test/src/test-close.c68
-rw-r--r--test/src/test-list.h1
-rw-r--r--test/test.gyp1
3 files changed, 70 insertions, 0 deletions
diff --git a/test/src/test-close.c b/test/src/test-close.c
new file mode 100644
index 0000000..bbbaf26
--- /dev/null
+++ b/test/src/test-close.c
@@ -0,0 +1,68 @@
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include "test-common.h"
+
+static uv_link_observer_t a;
+static uv_link_observer_t b;
+static uv_link_observer_t c;
+
+static int b_reads;
+static int c_reads;
+static int close_cb_called;
+
+static void c_close_cb(uv_link_t* l) {
+  close_cb_called++;
+}
+
+
+static void b_read_cb(uv_link_observer_t* o, ssize_t nread, const uv_buf_t* d) {
+  b_reads |= 1 << nread;
+  if (nread == 2) {
+    CHECK_EQ(a.close_depth, 0, "depth check");
+    CHECK_EQ(b.close_depth, 2, "depth check");
+    CHECK_EQ(c.close_depth, 1, "depth check");
+    uv_link_close((uv_link_t*) &c, c_close_cb);
+  } else {
+    CHECK_EQ(a.close_depth, 0, "depth check");
+    CHECK_EQ(b.close_depth, 1, "depth check");
+    CHECK_EQ(c.close_depth, 0, "depth check");
+  }
+}
+
+
+static void c_read_cb(uv_link_observer_t* o, ssize_t nread, const uv_buf_t* d) {
+  uv_buf_t tmp;
+
+  CHECK(nread > 0, "c_read_cb nread");
+
+  c_reads |= 1 << nread;
+  if (nread == 1) {
+    uv_link_propagate_alloc_cb((uv_link_t*) &a, nread + 1, &tmp);
+    uv_link_propagate_read_cb((uv_link_t*) &a, nread + 1, &tmp);
+  }
+}
+
+
+TEST_IMPL(close_depth) {
+  uv_buf_t tmp;
+
+  CHECK_EQ(uv_link_observer_init(&a), 0, "uv_link_observer_init()");
+  CHECK_EQ(uv_link_observer_init(&b), 0, "uv_link_observer_init()");
+  CHECK_EQ(uv_link_observer_init(&c), 0, "uv_link_observer_init()");
+
+  CHECK_EQ(uv_link_chain((uv_link_t*) &a, (uv_link_t*) &b), 0,
+           "uv_link_observer_chain()");
+  CHECK_EQ(uv_link_chain((uv_link_t*) &b, (uv_link_t*) &c), 0,
+           "uv_link_observer_chain()");
+
+  b.observer_read_cb = b_read_cb;
+  c.observer_read_cb = c_read_cb;
+
+  uv_link_propagate_alloc_cb((uv_link_t*) &a, 1, &tmp);
+  uv_link_propagate_read_cb((uv_link_t*) &a, 1, &tmp);
+
+  CHECK_EQ(close_cb_called, 1, "close_cb must be called");
+  CHECK_EQ(b_reads, 6, "b read_cb must be called");
+  CHECK_EQ(c_reads, 2, "c read_cb must be called");
+}
diff --git a/test/src/test-list.h b/test/src/test-list.h
index 35b1ab0..9385095 100644
--- a/test/src/test-list.h
+++ b/test/src/test-list.h
@@ -4,6 +4,7 @@
 #define TEST_ENUM(V)                                                          \
     V(uv_link_source_t)                                                       \
     V(uv_link_observer_t)                                                     \
+    V(close_depth)                                                            \
 
 #define TEST_DECL(N) void test__##N();
 
diff --git a/test/test.gyp b/test/test.gyp
index ef59c95..2a5d1d4 100644
--- a/test/test.gyp
+++ b/test/test.gyp
@@ -16,6 +16,7 @@
       "src/main.c",
       "src/test-uv-link-source-t.c",
       "src/test-uv-link-observer-t.c",
+      "src/test-close.c",
     ],
   }],
 }