PageRenderTime 22ms CodeModel.GetById 16ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/test/test-idle.c

http://github.com/joyent/libuv
C | 82 lines | 42 code | 20 blank | 20 comment | 11 complexity | 2b8e4a29edd285f238719f2b6a490713 MD5 | raw file
 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
26static uv_timer_t timer_handle;
27static uv_idle_t idle_handle;
28
29static int idle_cb_called = 0;
30static int timer_cb_called = 0;
31static int close_cb_called = 0;
32
33
34static void close_cb(uv_handle_t* handle) {
35  close_cb_called++;
36}
37
38
39static void timer_cb(uv_timer_t* handle, int status) {
40  ASSERT(handle == &timer_handle);
41  ASSERT(status == 0);
42
43  uv_close((uv_handle_t*) &idle_handle, close_cb);
44  uv_close((uv_handle_t*) &timer_handle, close_cb);
45
46  timer_cb_called++;
47  LOGF("timer_cb %d\n", timer_cb_called);
48}
49
50
51static void idle_cb(uv_idle_t* handle, int status) {
52  ASSERT(handle == &idle_handle);
53  ASSERT(status == 0);
54
55  idle_cb_called++;
56  LOGF("idle_cb %d\n", idle_cb_called);
57}
58
59
60TEST_IMPL(idle_starvation) {
61  int r;
62
63  r = uv_idle_init(uv_default_loop(), &idle_handle);
64  ASSERT(r == 0);
65  r = uv_idle_start(&idle_handle, idle_cb);
66  ASSERT(r == 0);
67
68  r = uv_timer_init(uv_default_loop(), &timer_handle);
69  ASSERT(r == 0);
70  r = uv_timer_start(&timer_handle, timer_cb, 50, 0);
71  ASSERT(r == 0);
72
73  r = uv_run(uv_default_loop());
74  ASSERT(r == 0);
75
76  ASSERT(idle_cb_called > 0);
77  ASSERT(timer_cb_called == 1);
78  ASSERT(close_cb_called == 2);
79
80  MAKE_VALGRIND_HAPPY();
81  return 0;
82}