1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 * 3 * Permission is hereby granted, free of charge, to any person obtaining a copy 4 * of this software and associated documentation files (the "Software"), to 5 * deal in the Software without restriction, including without limitation the 6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 * sell copies of the Software, and to permit persons to whom the Software is 8 * furnished to do so, subject to the following conditions: 9 * 10 * The above copyright notice and this permission notice shall be included in 11 * all copies or substantial portions of the Software. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 * IN THE SOFTWARE. 20 */ 21 22 #include "uv.h" 23 #include "task.h" 24 25 static uv_prepare_t prepare_handle; 26 static uv_check_t check_handle; 27 static uv_timer_t timer_handle; 28 29 static int prepare_cb_called; 30 static int check_cb_called; 31 static int timer_cb_called; 32 33 34 static void prepare_cb(uv_prepare_t* handle) { 35 ASSERT(0 == uv_prepare_stop(&prepare_handle)); 36 ASSERT(0 == prepare_cb_called); 37 ASSERT(1 == check_cb_called); 38 ASSERT(0 == timer_cb_called); 39 prepare_cb_called++; 40 } 41 42 43 static void timer_cb(uv_timer_t* handle) { 44 ASSERT(0 == uv_timer_stop(&timer_handle)); 45 ASSERT(1 == prepare_cb_called); 46 ASSERT(1 == check_cb_called); 47 ASSERT(0 == timer_cb_called); 48 timer_cb_called++; 49 } 50 51 52 static void check_cb(uv_check_t* handle) { 53 ASSERT(0 == uv_check_stop(&check_handle)); 54 ASSERT(0 == uv_timer_stop(&timer_handle)); /* Runs before timer_cb. */ 55 ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 50, 0)); 56 ASSERT(0 == uv_prepare_start(&prepare_handle, prepare_cb)); 57 ASSERT(0 == prepare_cb_called); 58 ASSERT(0 == check_cb_called); 59 ASSERT(0 == timer_cb_called); 60 check_cb_called++; 61 } 62 63 64 TEST_IMPL(timer_from_check) { 65 ASSERT(0 == uv_prepare_init(uv_default_loop(), &prepare_handle)); 66 ASSERT(0 == uv_check_init(uv_default_loop(), &check_handle)); 67 ASSERT(0 == uv_check_start(&check_handle, check_cb)); 68 ASSERT(0 == uv_timer_init(uv_default_loop(), &timer_handle)); 69 ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 50, 0)); 70 ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT)); 71 ASSERT(1 == prepare_cb_called); 72 ASSERT(1 == check_cb_called); 73 ASSERT(1 == timer_cb_called); 74 uv_close((uv_handle_t*) &prepare_handle, NULL); 75 uv_close((uv_handle_t*) &check_handle, NULL); 76 uv_close((uv_handle_t*) &timer_handle, NULL); 77 ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_ONCE)); 78 MAKE_VALGRIND_HAPPY(); 79 return 0; 80 } 81