inspector: fix tests on Windows

Should help with https://github.com/nodejs/node/pull/8034.

PR-URL: https://github.com/nodejs/node/pull/8528
Reviewed-By: bnoordhuis - Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
Eugene Ostroukhov 2016-09-13 20:15:51 -07:00 committed by Ali Ijaz Sheikh
parent a7fa72156a
commit 6f9157fbab
2 changed files with 46 additions and 20 deletions

View File

@ -450,14 +450,20 @@ static void close_and_report_handshake_failure(inspector_socket_t* inspector) {
}
}
static void then_close_and_report_failure(uv_write_t* req, int status) {
inspector_socket_t* inspector = WriteRequest::from_write_req(req)->inspector;
write_request_cleanup(req, status);
close_and_report_handshake_failure(inspector);
}
static void handshake_failed(inspector_socket_t* inspector) {
const char HANDSHAKE_FAILED_RESPONSE[] =
"HTTP/1.0 400 Bad Request\r\n"
"Content-Type: text/html; charset=UTF-8\r\n\r\n"
"WebSockets request was expected\r\n";
write_to_client(inspector, HANDSHAKE_FAILED_RESPONSE,
sizeof(HANDSHAKE_FAILED_RESPONSE) - 1);
close_and_report_handshake_failure(inspector);
sizeof(HANDSHAKE_FAILED_RESPONSE) - 1,
then_close_and_report_failure);
}
// init_handshake references message_complete_cb

View File

@ -9,16 +9,14 @@ static const int MAX_LOOP_ITERATIONS = 10000;
#define SPIN_WHILE(condition) \
{ \
bool timed_out = false; \
timeout_timer.data = &timed_out; \
uv_timer_start(&timeout_timer, set_timeout_flag, 5000, 0); \
uv_timer_t* timer = start_timer(&timed_out); \
while (((condition)) && !timed_out) { \
uv_run(&loop, UV_RUN_NOWAIT); \
} \
ASSERT_FALSE((condition)); \
uv_timer_stop(&timeout_timer); \
cleanup_timer(timer); \
}
static uv_timer_t timeout_timer;
static bool connected = false;
static bool inspector_ready = false;
static int handshake_events = 0;
@ -46,10 +44,33 @@ static const char HANDSHAKE_REQ[] = "GET /ws/path HTTP/1.1\r\n"
"Sec-WebSocket-Key: aaa==\r\n"
"Sec-WebSocket-Version: 13\r\n\r\n";
static void dispose_handle(uv_handle_t* handle) {
*static_cast<bool*>(handle->data) = true;
}
static void set_timeout_flag(uv_timer_t* timer) {
*(static_cast<bool*>(timer->data)) = true;
}
static uv_timer_t* start_timer(bool* flag) {
uv_timer_t* timer = new uv_timer_t();
uv_timer_init(&loop, timer);
timer->data = flag;
uv_timer_start(timer, set_timeout_flag, 5000, 0);
return timer;
}
static void cleanup_timer(uv_timer_t* timer) {
bool done = false;
timer->data = &done;
uv_timer_stop(timer);
uv_close(reinterpret_cast<uv_handle_t*>(timer), dispose_handle);
while (!done) {
uv_run(&loop, UV_RUN_NOWAIT);
}
delete timer;
}
static void stop_if_stop_path(enum inspector_handshake_event state,
const std::string& path, bool* cont) {
*cont = path.empty() || path != "/close";
@ -154,11 +175,12 @@ static void fail_callback(uv_stream_t* stream, ssize_t nread,
}
static void expect_nothing_on_client() {
int err = uv_read_start(reinterpret_cast<uv_stream_t *>(&client_socket),
buffer_alloc_cb, fail_callback);
uv_stream_t* stream = reinterpret_cast<uv_stream_t*>(&client_socket);
int err = uv_read_start(stream, buffer_alloc_cb, fail_callback);
GTEST_ASSERT_EQ(0, err);
for (int i = 0; i < MAX_LOOP_ITERATIONS; i++)
uv_run(&loop, UV_RUN_NOWAIT);
uv_read_stop(stream);
}
static void expect_on_client(const char* data, size_t len) {
@ -325,13 +347,12 @@ protected:
client_socket = uv_tcp_t();
server.data = &inspector;
sockaddr_in addr;
uv_timer_init(&loop, &timeout_timer);
uv_tcp_init(&loop, &server);
uv_tcp_init(&loop, &client_socket);
uv_ip4_addr("localhost", PORT, &addr);
uv_ip4_addr("127.0.0.1", PORT, &addr);
uv_tcp_bind(&server, reinterpret_cast<const struct sockaddr*>(&addr), 0);
int err = uv_listen(reinterpret_cast<uv_stream_t*>(&server),
0, on_new_connection);
1, on_new_connection);
GTEST_ASSERT_EQ(0, err);
uv_connect_t connect;
connect.data = nullptr;
@ -344,7 +365,6 @@ protected:
virtual void TearDown() {
really_close(reinterpret_cast<uv_handle_t*>(&client_socket));
really_close(reinterpret_cast<uv_handle_t*>(&timeout_timer));
EXPECT_TRUE(inspector.buffer.empty());
expectations* expects = static_cast<expectations*>(inspector.data);
if (expects != nullptr) {