]> git.ozlabs.org Git - ccan/blobdiff - ccan/io/test/run-08-hangup-on-idle.c
ccan/io: rewrite.
[ccan] / ccan / io / test / run-08-hangup-on-idle.c
index c8257703d3333ef2a5199db85b7232022157102a..c1ff1e6eec9745a730a2b5677adfa91824c5d306 100644 (file)
@@ -8,39 +8,49 @@
 
 static int fds2[2];
 
 
 static int fds2[2];
 
-static struct io_plan timeout_wakeup(struct io_conn *conn, char *buf)
+static struct io_plan *read_in(struct io_conn *conn, char *buf)
 {
 {
-       /* This kills the dummy connection. */
-       close(fds2[1]);
-       return io_read(buf, 16, io_close_cb, NULL);
+       return io_read(conn, buf, 16, io_close_cb, NULL);
 }
 
 }
 
-static struct io_plan never(struct io_conn *conn, void *unused)
+static struct io_plan *setup_waiter(struct io_conn *conn, char *buf)
 {
 {
-       abort();
+       return io_wait(conn, buf, IO_IN, read_in, buf);
+}
+
+static struct io_plan *wake_and_close(struct io_conn *conn, char *buf)
+{
+       io_wake(buf);
+       return io_close(conn);
+}
+
+static struct io_plan *setup_waker(struct io_conn *conn, char *buf)
+{
+       return io_read(conn, buf, 1, wake_and_close, buf);
 }
 
 int main(void)
 {
        int fds[2];
 }
 
 int main(void)
 {
        int fds[2];
-       struct io_conn *conn;
        char buf[16];
 
        plan_tests(4);
 
        ok1(pipe(fds) == 0);
 
        char buf[16];
 
        plan_tests(4);
 
        ok1(pipe(fds) == 0);
 
-       /* Write then close. */
-       io_new_conn(fds[1], io_write("hello there world", 16,
-                                    io_close_cb, NULL));
-       conn = io_new_conn(fds[0], io_wait(buf, never, NULL));
-
-       /* To avoid assert(num_waiting) */
+       io_new_conn(NULL, fds[0], setup_waiter, buf);
        ok1(pipe(fds2) == 0);
        ok1(pipe(fds2) == 0);
-       io_new_conn(fds2[0], io_read(buf, 16, io_close_cb, NULL));
+       io_new_conn(NULL, fds2[0], setup_waker, buf);
+
+       if (fork() == 0) {
+               write(fds[1], "hello there world", 16);
+               close(fds[1]);
 
 
-       /* After half a second, it will read. */
-       io_timeout(conn, time_from_msec(500), timeout_wakeup, buf);
+               /* Now wake it. */
+               sleep(1);
+               write(fds2[1], "", 1);
+               exit(0);
+       }
 
        ok1(io_loop() == NULL);
        ok1(memcmp(buf, "hello there world", 16) == 0);
 
        ok1(io_loop() == NULL);
        ok1(memcmp(buf, "hello there world", 16) == 0);