Skip to content

Commit

Permalink
test: fix issues reported by Coverity
Browse files Browse the repository at this point in the history
Wrapped the timer into class to ensure it is cleaned up properly.

PR-URL: #8870
Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
Eugene Ostroukhov authored and jasnell committed Oct 14, 2016
1 parent 3735f22 commit 0ad0e6a
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions test/cctest/test_inspector_socket.cc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ static const int MAX_LOOP_ITERATIONS = 10000;


#define SPIN_WHILE(condition) \ #define SPIN_WHILE(condition) \
{ \ { \
bool timed_out = false; \ Timeout timeout(&loop); \
uv_timer_t* timer = start_timer(&timed_out); \ while ((condition) && !timeout.timed_out) { \
while (((condition)) && !timed_out) { \
uv_run(&loop, UV_RUN_NOWAIT); \ uv_run(&loop, UV_RUN_NOWAIT); \
} \ } \
ASSERT_FALSE((condition)); \ ASSERT_FALSE((condition)); \
cleanup_timer(timer); \
} }


static bool connected = false; static bool connected = false;
Expand Down Expand Up @@ -46,32 +44,36 @@ static const char HANDSHAKE_REQ[] = "GET /ws/path HTTP/1.1\r\n"
"Sec-WebSocket-Key: aaa==\r\n" "Sec-WebSocket-Key: aaa==\r\n"
"Sec-WebSocket-Version: 13\r\n\r\n"; "Sec-WebSocket-Version: 13\r\n\r\n";


static void dispose_handle(uv_handle_t* handle) { class Timeout {
*static_cast<bool*>(handle->data) = true; public:
} explicit Timeout(uv_loop_t* loop) : timed_out(false), done_(false) {

uv_timer_init(loop, &timer_);
static void set_timeout_flag(uv_timer_t* timer) { uv_timer_start(&timer_, Timeout::set_flag, 5000, 0);
*(static_cast<bool*>(timer->data)) = true; }
}


static uv_timer_t* start_timer(bool* flag) { ~Timeout() {
uv_timer_t* timer = new uv_timer_t(); uv_timer_stop(&timer_);
uv_timer_init(&loop, timer); uv_close(reinterpret_cast<uv_handle_t*>(&timer_), mark_done);
timer->data = flag; while (!done_) {
uv_timer_start(timer, set_timeout_flag, 5000, 0); uv_run(&loop, UV_RUN_NOWAIT);
return timer; }
} }
bool timed_out;
private:
static void set_flag(uv_timer_t* timer) {
Timeout* t = node::ContainerOf(&Timeout::timer_, timer);
t->timed_out = true;
}


static void cleanup_timer(uv_timer_t* timer) { static void mark_done(uv_handle_t* timer) {
bool done = false; Timeout* t = node::ContainerOf(&Timeout::timer_,
timer->data = &done; reinterpret_cast<uv_timer_t*>(timer));
uv_timer_stop(timer); t->done_ = true;
uv_close(reinterpret_cast<uv_handle_t*>(timer), dispose_handle);
while (!done) {
uv_run(&loop, UV_RUN_NOWAIT);
} }
delete timer;
} bool done_;
uv_timer_t timer_;
};


static void stop_if_stop_path(enum inspector_handshake_event state, static void stop_if_stop_path(enum inspector_handshake_event state,
const std::string& path, bool* cont) { const std::string& path, bool* cont) {
Expand Down

0 comments on commit 0ad0e6a

Please sign in to comment.