1local uvVersionGEQ = require('lib/utils').uvVersionGEQ
2
3return require('lib/tap')(function (test)
4
5  -- This tests using timers for a simple timeout.
6  -- It also tests the handle close callback and
7  test("simple timeout", function (print, p, expect, uv)
8    local timer = uv.new_timer()
9    local function onclose()
10      p("closed", timer)
11    end
12    local function ontimeout()
13      p("timeout", timer)
14      uv.close(timer, expect(onclose))
15    end
16    uv.timer_start(timer, 10, 0, expect(ontimeout))
17  end)
18
19  -- This is like the previous test, but using repeat.
20  test("simple interval", function (print, p, expect, uv)
21    local timer = uv.new_timer()
22    local count = 3
23    local onclose = expect(function ()
24      p("closed", timer)
25    end)
26    local function oninterval()
27      p("interval", timer)
28      count = count - 1
29      if count == 0 then
30        uv.close(timer, onclose)
31      end
32    end
33    uv.timer_start(timer, 10, 10, oninterval)
34  end)
35
36  -- Test two concurrent timers
37  -- There is a small race condition, but there are 100ms of wiggle room.
38  -- 400ms is halfway between 100+200ms and 100+400ms
39  test("timeout with interval", function (print, p, expect, uv)
40    local a = uv.new_timer()
41    local b = uv.new_timer()
42    uv.timer_start(a, 400, 0, expect(function ()
43      p("timeout", a)
44      uv.timer_stop(b)
45      uv.close(a)
46      uv.close(b)
47    end))
48    uv.timer_start(b, 100, 200, expect(function ()
49      p("interval", b)
50    end, 2))
51  end)
52
53  -- This advanced test uses the rest of the uv_timer_t functions
54  -- to create an interval that shrinks over time.
55  test("shrinking interval", function (print, p, expect, uv)
56    local timer = uv.new_timer()
57    uv.timer_start(timer, 10, 0, expect(function ()
58      local r = uv.timer_get_repeat(timer)
59      p("interval", timer, r)
60      if r == 0 then
61        uv.timer_set_repeat(timer, 8)
62        uv.timer_again(timer)
63      elseif r == 2 then
64        uv.timer_stop(timer)
65        uv.close(timer)
66      else
67        uv.timer_set_repeat(timer, r / 2)
68      end
69    end, 4))
70  end)
71
72  test("shrinking interval using methods", function (print, p, expect, uv)
73    local timer = uv.new_timer()
74    timer:start(10, 0, expect(function ()
75      local r = timer:get_repeat()
76      p("interval", timer, r)
77      if r == 0 then
78        timer:set_repeat(8)
79        timer:again()
80      elseif r == 2 then
81        timer:stop()
82        timer:close()
83      else
84        timer:set_repeat(r / 2)
85      end
86    end, 4))
87  end)
88
89  test("timer init", function(print, p, expect, uv)
90    local timer = uv.new_timer()
91    assert(timer:get_repeat()==0)
92    if uvVersionGEQ("1.40.0") then
93      assert(uv.timer_get_due_in)
94      -- avoid https://github.com/libuv/libuv/issues/3020
95      --assert(timer:get_due_in()==0)
96    end
97    assert(timer:is_active()==false)
98    uv.close(timer)
99  end)
100
101  test("timer huge timeout", function(print, p, expect, uv)
102    local tiny_timer = uv.new_timer()
103    local huge_timer = uv.new_timer()
104
105    local function timer_cb()
106      uv.close(tiny_timer)
107      uv.close(huge_timer)
108    end
109
110    uv.timer_start(tiny_timer, 1, 0, expect(timer_cb))
111    uv.timer_start(huge_timer, 0xffff, 0, timer_cb)
112    --(Lua_Integer)0xffffffff is not 0xffffffff on x86
113    assert(tiny_timer:get_due_in()==1)
114    assert(huge_timer:get_due_in()==0xffff)
115  end, "1.40.0")
116
117end)
118