File: test-coroutines.lua

package info (click to toggle)
lua-luv 1.48.0-2-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,036 kB
  • sloc: ansic: 6,565; makefile: 72; sh: 33
file content (32 lines) | stat: -rw-r--r-- 668 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
return require('lib/tap')(function (test)

  test("coroutines", function (print, p, expect, uv)
    local touch = 0
    local function wait(ms)
      local this = coroutine.running()
      assert(this)
      local timer = uv.new_timer()
      timer:start(ms, 0, function ()
        timer:close()
        touch = touch + 1
        coroutine.resume(this)
        touch = touch + 1
      end)
      coroutine.yield()
      touch = touch + 1
      return touch
    end

    coroutine.wrap(function()
      print("begin wait")
      local touched = wait(1000)
      assert(touched==touch)
      print("end wait")
    end)()

    uv.run()

    assert(touch==3)
  end)

end)