@@ -9,16 +9,14 @@ static const int MAX_LOOP_ITERATIONS = 10000;
9
9
#define SPIN_WHILE (condition ) \
10
10
{ \
11
11
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); \
14
13
while (((condition)) && !timed_out) { \
15
14
uv_run (&loop, UV_RUN_NOWAIT); \
16
15
} \
17
16
ASSERT_FALSE ((condition)); \
18
- uv_timer_stop (&timeout_timer); \
17
+ cleanup_timer (timer); \
19
18
}
20
19
21
- static uv_timer_t timeout_timer;
22
20
static bool connected = false ;
23
21
static bool inspector_ready = false ;
24
22
static int handshake_events = 0 ;
@@ -46,10 +44,33 @@ static const char HANDSHAKE_REQ[] = "GET /ws/path HTTP/1.1\r\n"
46
44
" Sec-WebSocket-Key: aaa==\r\n "
47
45
" Sec-WebSocket-Version: 13\r\n\r\n " ;
48
46
47
+ static void dispose_handle (uv_handle_t * handle) {
48
+ *static_cast <bool *>(handle->data ) = true ;
49
+ }
50
+
49
51
static void set_timeout_flag (uv_timer_t * timer) {
50
52
*(static_cast <bool *>(timer->data )) = true ;
51
53
}
52
54
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
+
53
74
static void stop_if_stop_path (enum inspector_handshake_event state,
54
75
const std::string& path, bool * cont) {
55
76
*cont = path.empty () || path != " /close" ;
@@ -87,7 +108,7 @@ static void do_write(const char* data, int len) {
87
108
uv_buf_t buf[1 ];
88
109
buf[0 ].base = const_cast <char *>(data);
89
110
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 ,
91
112
write_done);
92
113
SPIN_WHILE (req.data );
93
114
}
@@ -124,7 +145,7 @@ static void check_data_cb(read_expects* expectation, ssize_t nread,
124
145
static void check_data_cb (uv_stream_t * stream, ssize_t nread,
125
146
const uv_buf_t * buf) {
126
147
bool retval = false ;
127
- read_expects* expects = static_cast <read_expects *>(stream->data );
148
+ read_expects* expects = static_cast <read_expects*>(stream->data );
128
149
expects->callback_called = true ;
129
150
check_data_cb (expects, nread, buf, &retval);
130
151
if (retval) {
@@ -154,17 +175,18 @@ static void fail_callback(uv_stream_t* stream, ssize_t nread,
154
175
}
155
176
156
177
static 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);
159
180
GTEST_ASSERT_EQ (0 , err);
160
181
for (int i = 0 ; i < MAX_LOOP_ITERATIONS; i++)
161
182
uv_run (&loop, UV_RUN_NOWAIT);
183
+ uv_read_stop (stream);
162
184
}
163
185
164
186
static void expect_on_client (const char * data, size_t len) {
165
187
read_expects expectation = prepare_expects (data, len);
166
188
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),
168
190
buffer_alloc_cb, check_data_cb);
169
191
SPIN_WHILE (!expectation.read_expected );
170
192
}
@@ -256,7 +278,7 @@ static void inspector_record_error_code(uv_stream_t* stream, ssize_t nread,
256
278
const uv_buf_t * buf) {
257
279
inspector_socket_t *inspector = inspector_from_stream (stream);
258
280
// 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;
260
282
}
261
283
262
284
static void expect_server_read_error () {
@@ -325,26 +347,24 @@ class InspectorSocketTest : public ::testing::Test {
325
347
client_socket = uv_tcp_t ();
326
348
server.data = &inspector;
327
349
sockaddr_in addr;
328
- uv_timer_init (&loop, &timeout_timer);
329
350
uv_tcp_init (&loop, &server);
330
351
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);
335
356
GTEST_ASSERT_EQ (0 , err);
336
357
uv_connect_t connect;
337
358
connect.data = nullptr ;
338
359
uv_tcp_connect (&connect, &client_socket,
339
- reinterpret_cast <const sockaddr *>(&addr), on_connection);
360
+ reinterpret_cast <const sockaddr*>(&addr), on_connection);
340
361
uv_tcp_nodelay (&client_socket, 1 ); // The buffering messes up the test
341
362
SPIN_WHILE (!connect.data || !connected);
342
363
really_close (reinterpret_cast <uv_handle_t *>(&server));
343
364
}
344
365
345
366
virtual void TearDown () {
346
367
really_close (reinterpret_cast <uv_handle_t *>(&client_socket));
347
- really_close (reinterpret_cast <uv_handle_t *>(&timeout_timer));
348
368
EXPECT_TRUE (inspector.buffer .empty ());
349
369
expectations* expects = static_cast <expectations*>(inspector.data );
350
370
if (expects != nullptr ) {
@@ -753,7 +773,7 @@ TEST_F(InspectorSocketTest, WriteBeforeHandshake) {
753
773
754
774
static void CleanupSocketAfterEOF_close_cb (inspector_socket_t * inspector,
755
775
int status) {
756
- *(static_cast <bool *>(inspector->data )) = true ;
776
+ *(static_cast <bool *>(inspector->data )) = true ;
757
777
}
758
778
759
779
static void CleanupSocketAfterEOF_read_cb (uv_stream_t * stream, ssize_t nread,
0 commit comments