@@ -10,6 +10,7 @@ BOOST_AUTO_TEST_CASE(websocket_test)
10
10
{
11
11
fc::http::websocket_client client;
12
12
fc::http::websocket_connection_ptr s_conn, c_conn;
13
+ int port;
13
14
{
14
15
fc::http::websocket_server server;
15
16
server.on_connection ([&]( const fc::http::websocket_connection_ptr& c ){
@@ -19,11 +20,26 @@ BOOST_AUTO_TEST_CASE(websocket_test)
19
20
});
20
21
});
21
22
22
- server.listen ( 8090 );
23
+ bool listen_ok = false ;
24
+ for ( int i = 0 ; !listen_ok && i < 5 ; ++i )
25
+ {
26
+ port = std::rand () % 50000 + 10000 ;
27
+ try
28
+ {
29
+ server.listen ( port );
30
+ listen_ok = true ;
31
+ }
32
+ catch ( std::exception )
33
+ {
34
+ // if the port is busy, listen() will throw a std::exception, do nothing here.
35
+ }
36
+ }
37
+ BOOST_REQUIRE ( listen_ok );
38
+
23
39
server.start_accept ();
24
40
25
41
std::string echo;
26
- c_conn = client.connect ( " ws://localhost:8090 " );
42
+ c_conn = client.connect ( " ws://localhost:" + fc::to_string (port) );
27
43
c_conn->on_message_handler ([&](const std::string& s){
28
44
echo = s;
29
45
});
@@ -43,7 +59,7 @@ BOOST_AUTO_TEST_CASE(websocket_test)
43
59
// std::cerr << e.to_string() << "\n";
44
60
}
45
61
46
- c_conn = client.connect ( " ws://localhost:8090 " );
62
+ c_conn = client.connect ( " ws://localhost:" + fc::to_string (port) );
47
63
c_conn->on_message_handler ([&](const std::string& s){
48
64
echo = s;
49
65
});
@@ -55,19 +71,21 @@ BOOST_AUTO_TEST_CASE(websocket_test)
55
71
try {
56
72
c_conn->send_message ( " again" );
57
73
BOOST_FAIL (" expected assertion failure" );
74
+ } catch (const fc::assert_exception& e) {
75
+ std::cerr << " Expected assertion failure : " << e.to_string () << " \n " ;
58
76
} catch (const fc::exception & e) {
59
- std::cerr << e.to_string () << " \n " ;
77
+ BOOST_FAIL (" Unexpected exception : " + e.to_string ());
78
+ } catch (const std::exception & e) {
79
+ BOOST_FAIL (" Unexpected exception : " + std::string (e.what ()));
60
80
}
61
81
62
82
try {
63
- c_conn = client.connect ( " ws://localhost:8090" );
64
- BOOST_FAIL (" expected assertion failure" );
65
- } catch (const fc::exception & e) {
66
- std::cerr << e.to_string () << " \n " ;
83
+ c_conn = client.connect ( " ws://localhost:" + fc::to_string (port) );
84
+ BOOST_FAIL (" expected fc::exception (fail to connect)" );
67
85
} catch (const fc::exception & e) {
68
- BOOST_FAIL ( " Unexpected exception: " + e.to_string ()) ;
86
+ std::cerr << " Excepted fc:: exception : " << e.to_string () << " \n " ;
69
87
} catch (const std::exception & e) {
70
- BOOST_FAIL (" Unexpected exception: " + std::string (e.what ()));
88
+ BOOST_FAIL (" Unexpected exception : " + std::string (e.what ()));
71
89
}
72
90
}
73
91
0 commit comments