diff --git a/machinarium/mm_io.h b/machinarium/mm_io.h index 3ca53dfe..d2cc3bfd 100644 --- a/machinarium/mm_io.h +++ b/machinarium/mm_io.h @@ -50,12 +50,9 @@ mm_io_timer_start(uv_timer_t *timer, uv_timer_cb callback, uint64_t time_ms) } static inline void -mm_io_timer_stop(uv_timer_t *timer) +mm_io_timer_stop(mmio *io, uv_timer_t *timer) { uv_timer_stop(timer); - uv_handle_t *handle = (uv_handle_t*)timer; - if (! uv_is_closing(handle)) - uv_close(handle, NULL); } #endif diff --git a/machinarium/mm_ioconnect.c b/machinarium/mm_ioconnect.c index a52574e3..29e2b150 100644 --- a/machinarium/mm_ioconnect.c +++ b/machinarium/mm_ioconnect.c @@ -15,9 +15,7 @@ mm_io_connect_timeout_cb(uv_timer_t *handle) io->connect_timeout = 1; /* cancel connection request, * connect callback will be called anyway */ - uv_handle_t *to_cancel; - to_cancel = (uv_handle_t*)&io->handle; - uv_close(to_cancel, NULL); + mm_io_close_handle(io, (uv_handle_t*)&io->handle); } static void @@ -28,7 +26,7 @@ mm_io_connect_cb(uv_connect_t *handle, int status) goto wakeup; if (io->connect_timeout) goto wakeup; - mm_io_timer_stop(&io->connect_timer); + mm_io_timer_stop(io, &io->connect_timer); wakeup: io->connect_status = status; mm_wakeup(io->f, io->connect_fiber); @@ -39,10 +37,8 @@ mm_io_connect_cancel_cb(mmfiber *fiber, void *arg) { mmio *io = arg; io->write_timeout = 0; - mm_io_timer_stop(&io->connect_timer); - uv_handle_t *to_cancel; - to_cancel = (uv_handle_t*)&io->handle; - uv_close(to_cancel, NULL); + mm_io_timer_stop(io, &io->connect_timer); + mm_io_close_handle(io, (uv_handle_t*)&io->handle); } MM_API int diff --git a/tests/makefile b/tests/makefile index 7b3a3276..914c0435 100644 --- a/tests/makefile +++ b/tests/makefile @@ -10,7 +10,8 @@ TESTS = test_new \ test_wait \ test_cancel_sleep \ test_cancel_sleep_2 \ - test_io_new + test_io_new \ + test_io_connect all: validate clean $(TESTS) test_new: $(CC) $(CFLAGS) test_new.c $(LFLAGS) -o test_new @@ -26,6 +27,8 @@ test_cancel_sleep_2: $(CC) $(CFLAGS) test_cancel_sleep_2.c $(LFLAGS) -o test_cancel_sleep_2 test_io_new: $(CC) $(CFLAGS) test_io_new.c $(LFLAGS) -o test_io_new +test_io_connect: + $(CC) $(CFLAGS) test_io_connect.c $(LFLAGS) -o test_io_connect validate: @if [ ! -f $(LFLAGS_LIB) ]; then \ echo ""; \ diff --git a/tests/test_io_connect b/tests/test_io_connect new file mode 100755 index 00000000..f14be532 Binary files /dev/null and b/tests/test_io_connect differ diff --git a/tests/test_io_connect.c b/tests/test_io_connect.c new file mode 100644 index 00000000..74b340ee --- /dev/null +++ b/tests/test_io_connect.c @@ -0,0 +1,52 @@ + +/* + * machinarium. + * + * Cooperative multitasking engine. +*/ + +#include +#include + +static void +test_connect(void *arg) +{ + mm_t env = arg; + printf("child started\n"); + mmio_t client = mm_io_new(env); + int rc; + rc = mm_connect(client, "8.8.8.16", 1324, 0); + printf("child resumed\n"); + assert(rc < 0); + mm_close(client); + if (mm_is_cancel(env)) + printf("child marked as cancel\n"); + printf("child end\n"); +} + +static void +test_waiter(void *arg) +{ + mm_t env = arg; + + printf("waiter started\n"); + + int id = mm_create(env, test_connect, env); + mm_sleep(env, 0); + mm_cancel(env, id); + mm_wait(env, id); + + printf("waiter 1 ended \n"); + mm_stop(env); +} + +int +main(int argc, char *argv[]) +{ + mm_t env = mm_new(); + mm_create(env, test_waiter, env); + mm_start(env); + printf("shutting down\n"); + mm_free(env); + return 0; +}