138032Speter/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
238032Speter *
3261363Sgshapiro * Permission is hereby granted, free of charge, to any person obtaining a copy
464562Sgshapiro * of this software and associated documentation files (the "Software"), to
538032Speter * deal in the Software without restriction, including without limitation the
638032Speter * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
738032Speter * sell copies of the Software, and to permit persons to whom the Software is
838032Speter * furnished to do so, subject to the following conditions:
938032Speter *
1038032Speter * The above copyright notice and this permission notice shall be included in
1138032Speter * all copies or substantial portions of the Software.
1238032Speter *
1338032Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1438032Speter * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1538032Speter * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1638032Speter * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1764562Sgshapiro * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1838032Speter * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1938032Speter * IN THE SOFTWARE.
2038032Speter */
2190792Sgshapiro
2238032Speter#include "uv.h"
2338032Speter#include "task.h"
2438032Speter
2538032Speter#include <stdio.h>
2638032Speter#include <stdlib.h>
27266692Sgshapiro
2838032Speter
2938032Speterstatic int close_cb_called = 0;
3038032Speter
3138032Speter
3290792Sgshapirostatic void close_cb(uv_handle_t* handle) {
3338032Speter  ASSERT_NOT_NULL(handle);
3438032Speter  close_cb_called++;
3538032Speter}
3638032Speter
3738032Speter
3838032Speterstatic void timer_cb(uv_timer_t* handle) {
3938032Speter  ASSERT(0 && "timer_cb should not have been called");
4038032Speter}
4138032Speter
4238032Speter
4338032SpeterTEST_IMPL(active) {
4438032Speter  int r;
4538032Speter  uv_timer_t timer;
4638032Speter
4738032Speter  r = uv_timer_init(uv_default_loop(), &timer);
4838032Speter  ASSERT(r == 0);
4938032Speter
5038032Speter  /* uv_is_active() and uv_is_closing() should always return either 0 or 1. */
5138032Speter  ASSERT(0 == uv_is_active((uv_handle_t*) &timer));
5238032Speter  ASSERT(0 == uv_is_closing((uv_handle_t*) &timer));
5338032Speter
5490792Sgshapiro  r = uv_timer_start(&timer, timer_cb, 1000, 0);
5538032Speter  ASSERT(r == 0);
5638032Speter
5738032Speter  ASSERT(1 == uv_is_active((uv_handle_t*) &timer));
5838032Speter  ASSERT(0 == uv_is_closing((uv_handle_t*) &timer));
5938032Speter
6038032Speter  r = uv_timer_stop(&timer);
6138032Speter  ASSERT(r == 0);
6238032Speter
6338032Speter  ASSERT(0 == uv_is_active((uv_handle_t*) &timer));
6438032Speter  ASSERT(0 == uv_is_closing((uv_handle_t*) &timer));
6538032Speter
6638032Speter  r = uv_timer_start(&timer, timer_cb, 1000, 0);
6738032Speter  ASSERT(r == 0);
6838032Speter
6990792Sgshapiro  ASSERT(1 == uv_is_active((uv_handle_t*) &timer));
7038032Speter  ASSERT(0 == uv_is_closing((uv_handle_t*) &timer));
7138032Speter
7238032Speter  uv_close((uv_handle_t*) &timer, close_cb);
7338032Speter
7438032Speter  ASSERT(0 == uv_is_active((uv_handle_t*) &timer));
7538032Speter  ASSERT(1 == uv_is_closing((uv_handle_t*) &timer));
7638032Speter
7738032Speter  r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
7838032Speter  ASSERT(r == 0);
7938032Speter
8038032Speter  ASSERT(close_cb_called == 1);
8138032Speter
8238032Speter  MAKE_VALGRIND_HAPPY();
8338032Speter  return 0;
8438032Speter}
8538032Speter