@@ -9,16 +9,14 @@ static const int MAX_LOOP_ITERATIONS = 10000;
99#define SPIN_WHILE (condition ) \
1010 { \
1111 bool timed_out = false ; \
12- timeout_timer.data = &timed_out; \
13- uv_timer_start (&timeout_timer, set_timeout_flag, 5000 , 0 ); \
12+ uv_timer_t * timer = start_timer (&timed_out); \
1413 while (((condition)) && !timed_out) { \
1514 uv_run (&loop, UV_RUN_NOWAIT); \
1615 } \
1716 ASSERT_FALSE ((condition)); \
18- uv_timer_stop (&timeout_timer); \
17+ cleanup_timer (timer); \
1918 }
2019
21- static uv_timer_t timeout_timer;
2220static bool connected = false ;
2321static bool inspector_ready = false ;
2422static int handshake_events = 0 ;
@@ -46,10 +44,33 @@ static const char HANDSHAKE_REQ[] = "GET /ws/path HTTP/1.1\r\n"
4644 " Sec-WebSocket-Key: aaa==\r\n "
4745 " Sec-WebSocket-Version: 13\r\n\r\n " ;
4846
47+ static void dispose_handle (uv_handle_t * handle) {
48+ *static_cast <bool *>(handle->data ) = true ;
49+ }
50+
4951static void set_timeout_flag (uv_timer_t * timer) {
5052 *(static_cast <bool *>(timer->data )) = true ;
5153}
5254
55+ static uv_timer_t * start_timer (bool * flag) {
56+ uv_timer_t * timer = new uv_timer_t ();
57+ uv_timer_init (&loop, timer);
58+ timer->data = flag;
59+ uv_timer_start (timer, set_timeout_flag, 5000 , 0 );
60+ return timer;
61+ }
62+
63+ static void cleanup_timer (uv_timer_t * timer) {
64+ bool done = false ;
65+ timer->data = &done;
66+ uv_timer_stop (timer);
67+ uv_close (reinterpret_cast <uv_handle_t *>(timer), dispose_handle);
68+ while (!done) {
69+ uv_run (&loop, UV_RUN_NOWAIT);
70+ }
71+ delete timer;
72+ }
73+
5374static void stop_if_stop_path (enum inspector_handshake_event state,
5475 const std::string& path, bool * cont) {
5576 *cont = path.empty () || path != " /close" ;
@@ -87,7 +108,7 @@ static void do_write(const char* data, int len) {
87108 uv_buf_t buf[1 ];
88109 buf[0 ].base = const_cast <char *>(data);
89110 buf[0 ].len = len;
90- uv_write (&req, reinterpret_cast <uv_stream_t *>(&client_socket), buf, 1 ,
111+ uv_write (&req, reinterpret_cast <uv_stream_t *>(&client_socket), buf, 1 ,
91112 write_done);
92113 SPIN_WHILE (req.data );
93114}
@@ -124,7 +145,7 @@ static void check_data_cb(read_expects* expectation, ssize_t nread,
124145static void check_data_cb (uv_stream_t * stream, ssize_t nread,
125146 const uv_buf_t * buf) {
126147 bool retval = false ;
127- read_expects* expects = static_cast <read_expects *>(stream->data );
148+ read_expects* expects = static_cast <read_expects*>(stream->data );
128149 expects->callback_called = true ;
129150 check_data_cb (expects, nread, buf, &retval);
130151 if (retval) {
@@ -154,17 +175,18 @@ static void fail_callback(uv_stream_t* stream, ssize_t nread,
154175}
155176
156177static void expect_nothing_on_client () {
157- int err = uv_read_start ( reinterpret_cast <uv_stream_t *>(&client_socket),
158- buffer_alloc_cb, fail_callback);
178+ uv_stream_t * stream = reinterpret_cast <uv_stream_t *>(&client_socket);
179+ int err = uv_read_start (stream, buffer_alloc_cb, fail_callback);
159180 GTEST_ASSERT_EQ (0 , err);
160181 for (int i = 0 ; i < MAX_LOOP_ITERATIONS; i++)
161182 uv_run (&loop, UV_RUN_NOWAIT);
183+ uv_read_stop (stream);
162184}
163185
164186static void expect_on_client (const char * data, size_t len) {
165187 read_expects expectation = prepare_expects (data, len);
166188 client_socket.data = ℰ
167- uv_read_start (reinterpret_cast <uv_stream_t *>(&client_socket),
189+ uv_read_start (reinterpret_cast <uv_stream_t *>(&client_socket),
168190 buffer_alloc_cb, check_data_cb);
169191 SPIN_WHILE (!expectation.read_expected );
170192}
@@ -256,7 +278,7 @@ static void inspector_record_error_code(uv_stream_t* stream, ssize_t nread,
256278 const uv_buf_t * buf) {
257279 inspector_socket_t *inspector = inspector_from_stream (stream);
258280 // Increment instead of assign is to ensure the function is only called once
259- *(static_cast <int *>(inspector->data )) += nread;
281+ *(static_cast <int *>(inspector->data )) += nread;
260282}
261283
262284static void expect_server_read_error () {
@@ -325,26 +347,24 @@ class InspectorSocketTest : public ::testing::Test {
325347 client_socket = uv_tcp_t ();
326348 server.data = &inspector;
327349 sockaddr_in addr;
328- uv_timer_init (&loop, &timeout_timer);
329350 uv_tcp_init (&loop, &server);
330351 uv_tcp_init (&loop, &client_socket);
331- uv_ip4_addr (" localhost " , PORT, &addr);
332- uv_tcp_bind (&server, reinterpret_cast <const struct sockaddr *>(&addr), 0 );
333- int err = uv_listen (reinterpret_cast <uv_stream_t *>(&server),
334- 0 , on_new_connection);
352+ uv_ip4_addr (" 127.0.0.1 " , PORT, &addr);
353+ uv_tcp_bind (&server, reinterpret_cast <const struct sockaddr *>(&addr), 0 );
354+ int err = uv_listen (reinterpret_cast <uv_stream_t *>(&server),
355+ 1 , on_new_connection);
335356 GTEST_ASSERT_EQ (0 , err);
336357 uv_connect_t connect;
337358 connect.data = nullptr ;
338359 uv_tcp_connect (&connect, &client_socket,
339- reinterpret_cast <const sockaddr *>(&addr), on_connection);
360+ reinterpret_cast <const sockaddr*>(&addr), on_connection);
340361 uv_tcp_nodelay (&client_socket, 1 ); // The buffering messes up the test
341362 SPIN_WHILE (!connect.data || !connected);
342363 really_close (reinterpret_cast <uv_handle_t *>(&server));
343364 }
344365
345366 virtual void TearDown () {
346367 really_close (reinterpret_cast <uv_handle_t *>(&client_socket));
347- really_close (reinterpret_cast <uv_handle_t *>(&timeout_timer));
348368 EXPECT_TRUE (inspector.buffer .empty ());
349369 expectations* expects = static_cast <expectations*>(inspector.data );
350370 if (expects != nullptr ) {
@@ -753,7 +773,7 @@ TEST_F(InspectorSocketTest, WriteBeforeHandshake) {
753773
754774static void CleanupSocketAfterEOF_close_cb (inspector_socket_t * inspector,
755775 int status) {
756- *(static_cast <bool *>(inspector->data )) = true ;
776+ *(static_cast <bool *>(inspector->data )) = true ;
757777}
758778
759779static void CleanupSocketAfterEOF_read_cb (uv_stream_t * stream, ssize_t nread,
0 commit comments