From a3f8f2f00437567c8a2f55b9aafd93b08ca8bded Mon Sep 17 00:00:00 2001 From: abitmore Date: Thu, 22 Mar 2018 12:33:17 -0400 Subject: [PATCH] Fix websocket_test --- tests/network/http/websocket_test.cpp | 38 ++++++++++++++++++++------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/tests/network/http/websocket_test.cpp b/tests/network/http/websocket_test.cpp index deb6bbe..3f89283 100644 --- a/tests/network/http/websocket_test.cpp +++ b/tests/network/http/websocket_test.cpp @@ -10,6 +10,7 @@ BOOST_AUTO_TEST_CASE(websocket_test) { fc::http::websocket_client client; fc::http::websocket_connection_ptr s_conn, c_conn; + int port; { fc::http::websocket_server server; server.on_connection([&]( const fc::http::websocket_connection_ptr& c ){ @@ -19,11 +20,26 @@ BOOST_AUTO_TEST_CASE(websocket_test) }); }); - server.listen( 8090 ); + bool listen_ok = false; + for( int i = 0; !listen_ok && i < 5; ++i ) + { + port = std::rand() % 50000 + 10000; + try + { + server.listen( port ); + listen_ok = true; + } + catch( std::exception ) + { + // if the port is busy, listen() will throw a std::exception, do nothing here. + } + } + BOOST_REQUIRE( listen_ok ); + server.start_accept(); std::string echo; - c_conn = client.connect( "ws://localhost:8090" ); + c_conn = client.connect( "ws://localhost:" + fc::to_string(port) ); c_conn->on_message_handler([&](const std::string& s){ echo = s; }); @@ -43,7 +59,7 @@ BOOST_AUTO_TEST_CASE(websocket_test) //std::cerr << e.to_string() << "\n"; } - c_conn = client.connect( "ws://localhost:8090" ); + c_conn = client.connect( "ws://localhost:" + fc::to_string(port) ); c_conn->on_message_handler([&](const std::string& s){ echo = s; }); @@ -55,19 +71,21 @@ BOOST_AUTO_TEST_CASE(websocket_test) try { c_conn->send_message( "again" ); BOOST_FAIL("expected assertion failure"); + } catch (const fc::assert_exception& e) { + std::cerr << "Expected assertion failure : " << e.to_string() << "\n"; } catch (const fc::exception& e) { - std::cerr << e.to_string() << "\n"; + BOOST_FAIL("Unexpected exception : " + e.to_string()); + } catch (const std::exception& e) { + BOOST_FAIL("Unexpected exception : " + std::string(e.what())); } try { - c_conn = client.connect( "ws://localhost:8090" ); - BOOST_FAIL("expected assertion failure"); + c_conn = client.connect( "ws://localhost:" + fc::to_string(port) ); + BOOST_FAIL("expected fc::exception (fail to connect)"); } catch (const fc::exception& e) { - std::cerr << e.to_string() << "\n"; - } catch (const fc::exception& e) { - BOOST_FAIL("Unexpected exception: " + e.to_string()); + std::cerr << "Excepted fc::exception : " << e.to_string() << "\n"; } catch (const std::exception& e) { - BOOST_FAIL("Unexpected exception: " + std::string(e.what())); + BOOST_FAIL("Unexpected exception : " + std::string(e.what())); } }