From ee85957dc454f88aa72e11192d574b8159359b3f Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Tue, 22 May 2018 14:08:24 +0200 Subject: [PATCH] Problem: no test case for polling pending connect that ultimately fails Solution: added test case, reproduces the likely cause for #3107 --- unittests/unittest_poller.cpp | 54 +++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/unittests/unittest_poller.cpp b/unittests/unittest_poller.cpp index ec3409721e..d48f6a7918 100644 --- a/unittests/unittest_poller.cpp +++ b/unittests/unittest_poller.cpp @@ -205,6 +205,56 @@ void test_add_fd_and_remove_by_timer () close_fdpair (w, r); } +#ifdef _WIN32 +void test_add_fd_with_pending_failing_connect () +{ + zmq::thread_ctx_t thread_ctx; + zmq::poller_t poller (thread_ctx); + + zmq::fd_t bind_socket = socket (AF_INET, SOCK_STREAM, 0); + sockaddr_in addr = {0}; + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); + addr.sin_port = 0; + TEST_ASSERT_EQUAL_INT (0, bind (bind_socket, + reinterpret_cast (&addr), + sizeof (addr))); + + int addr_len = static_cast (sizeof (addr)); + TEST_ASSERT_EQUAL_INT (0, getsockname (bind_socket, + reinterpret_cast (&addr), + &addr_len)); + + zmq::fd_t connect_socket = socket (AF_INET, SOCK_STREAM, 0); + zmq::unblock_socket (connect_socket); + + TEST_ASSERT_EQUAL_INT ( + -1, connect (connect_socket, reinterpret_cast (&addr), + sizeof (addr))); + TEST_ASSERT_EQUAL_INT (WSAEWOULDBLOCK, WSAGetLastError ()); + + test_events_t events (connect_socket, poller); + + zmq::poller_t::handle_t handle = poller.add_fd (connect_socket, &events); + events.set_handle (handle); + poller.set_pollin (handle); + poller.start (); + + wait_in_events (events); + + int value; + int value_len = sizeof (value); + TEST_ASSERT_EQUAL_INT (0, getsockopt (connect_socket, SOL_SOCKET, SO_ERROR, + reinterpret_cast (&value), + &value_len)); + TEST_ASSERT_EQUAL_INT (WSAECONNREFUSED, value); + + // required cleanup + close (connect_socket); + close (bind_socket); +} +#endif + int main (void) { UNITY_BEGIN (); @@ -216,6 +266,10 @@ int main (void) RUN_TEST (test_add_fd_and_start_and_receive_data); RUN_TEST (test_add_fd_and_remove_by_timer); +#if defined _WIN32 + RUN_TEST (test_add_fd_with_pending_failing_connect); +#endif + zmq::shutdown_network (); return UNITY_END ();