1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-03 19:00:06 +08:00
SpaceVim/bundle/plenary.nvim/tests/plenary/async/condvar_spec.lua
2022-05-16 22:20:10 +08:00

129 lines
2.1 KiB
Lua

require("plenary.async").tests.add_to_env()
local Condvar = a.control.Condvar
local eq = assert.are.same
local join, run_all = a.util.join, a.util.run_all
describe("condvar", function()
a.it("should allow blocking", function()
local var = false
local condvar = Condvar.new()
a.run(function()
condvar:wait()
var = true
end)
eq(var, false)
condvar:notify_one()
eq(var, true)
end)
a.it("should be able to notify one when running", function()
local counter = 0
local condvar = Condvar.new()
local first = function()
condvar:wait()
counter = counter + 1
end
local second = function()
condvar:wait()
counter = counter + 1
end
local third = function()
condvar:wait()
counter = counter + 1
end
a.run(function()
join { first, second, third }
end)
eq(0, counter)
condvar:notify_one()
eq(1, counter)
condvar:notify_one()
eq(counter, 2)
condvar:notify_one()
eq(counter, 3)
end)
a.it("should allow notify_one to work when using await_all", function()
local counter = 0
local condvar = Condvar.new()
local first = function()
condvar:wait()
counter = counter + 1
end
local second = function()
condvar:wait()
counter = counter + 1
end
local third = function()
condvar:wait()
counter = counter + 1
end
run_all { first, second, third }
eq(0, counter)
condvar:notify_one()
eq(1, counter)
condvar:notify_one()
eq(counter, 2)
condvar:notify_one()
eq(counter, 3)
end)
a.it("should notify_all", function()
local counter = 0
local condvar = Condvar.new()
local first = function()
condvar:wait()
counter = counter + 1
end
local second = function()
condvar:wait()
counter = counter + 1
end
local third = function()
condvar:wait()
counter = counter + 1
end
run_all { first, second, third }
eq(0, counter)
condvar:notify_all()
eq(3, counter)
end)
end)