File: 174-cq-close.lua

package info (click to toggle)
lua-cqueues 20161214-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,672 kB
  • ctags: 3,228
  • sloc: ansic: 20,232; sh: 2,959; makefile: 24
file content (65 lines) | stat: -rwxr-xr-x 1,364 bytes parent folder | download | duplicates (4)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/sh
_=[[
	. "${0%%/*}/regress.sh"
	exec runlua "$0" "$@"
]]
require"regress".export".*"


local mainloop = cqueues.new()
local cv = condition.new()
local cq = cqueues.new()

local closed = false
local polling, polled = false, false
local tests = 0
local okays = 0

local function test(...)
	tests = tests + 1
	mainloop:wrap(function (f, ...)
		f(...)
		okays = okays + 1
	end, ...)
end

test(function ()
	local function checkresult(ready)
		info"awoke on controller readiness"
		polling = false
		polled = true

		check(closed == true, "spurious wakeup")
		check(ready == cq, "cqueue not cancelled")
		check(cqueues.type(cq), "cqueue not closed")
	end

	cv:signal()
	polling = true
	info"polling controller"
	checkresult(cqueues.poll(cq, 3))
end)

test(function ()
	check(not polled, "polling thread unexpectedly finished")
	if not polling then
		info"waiting on polling thread"
		cv:wait(5)
		check(polling, "expected polling thread")
	end

	closed = true
	info"closing controller"
	cq:close()
	check(not pcall(cq.wrap, cq), "cqueue should not allow new threads when closed") -- previously would trigger a segfault, should now error

	info"closing controller again"
	cq:close()
	info("cq:close() was safely called a second time")
end)

ok, why = mainloop:loop()
check(ok, "%s", why)
check(tests == okays, "expected %d okays, got %d", tests, okays)

say"OK"