mirror of
https://github.com/pesde-pkg/scripts.git
synced 2024-12-12 15:00:37 +00:00
chore(lune): add time consume per test statistic
This commit is contained in:
parent
add460c3cc
commit
392a8f2c9a
2 changed files with 94 additions and 29 deletions
48
.lune/tests/channel.luau
Normal file
48
.lune/tests/channel.luau
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
--- An MPSC synchronization primitive powered by Lua upvalues which retains only
|
||||||
|
--- one value at a time.
|
||||||
|
|
||||||
|
--- ## Usage
|
||||||
|
--- ```luau
|
||||||
|
--- local send, recv = watch((nil :: any) :: string)
|
||||||
|
--- task.delay(5, send, "hello, world!")
|
||||||
|
--- task.spawn(function()
|
||||||
|
--- local value = recv()
|
||||||
|
--- print("recevied value:", value)
|
||||||
|
--- end)
|
||||||
|
--- ```
|
||||||
|
type Watch<T> = {
|
||||||
|
value: T?,
|
||||||
|
receivers: { thread },
|
||||||
|
}
|
||||||
|
|
||||||
|
--- Crates a new `Watch` channel, returning its send and receive handles.
|
||||||
|
local function chan<T>(_phantom: T): ((T) -> (), () -> T?)
|
||||||
|
local watch: Watch<T> = {
|
||||||
|
value = nil,
|
||||||
|
receivers = {},
|
||||||
|
}
|
||||||
|
|
||||||
|
local function send(value: T)
|
||||||
|
watch.value = value
|
||||||
|
|
||||||
|
for _, receiver in watch.receivers do
|
||||||
|
coroutine.resume(receiver, value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function recv(): T
|
||||||
|
local value = watch.value
|
||||||
|
watch.value = nil
|
||||||
|
|
||||||
|
if value == nil then
|
||||||
|
table.insert(watch.receivers, coroutine.running())
|
||||||
|
return coroutine.yield()
|
||||||
|
end
|
||||||
|
|
||||||
|
return value :: T
|
||||||
|
end
|
||||||
|
|
||||||
|
return send, recv
|
||||||
|
end
|
||||||
|
|
||||||
|
return chan
|
|
@ -3,12 +3,14 @@ local stdio = require("@lune/stdio")
|
||||||
local frktest = require("../../lune_packages/frktest")
|
local frktest = require("../../lune_packages/frktest")
|
||||||
local Reporter = frktest._reporters.lune_console_reporter
|
local Reporter = frktest._reporters.lune_console_reporter
|
||||||
|
|
||||||
|
local watch = require("./channel")
|
||||||
|
|
||||||
local STYLE = table.freeze({
|
local STYLE = table.freeze({
|
||||||
suite = function(name: string)
|
suite = function(name: string)
|
||||||
return `{stdio.style("bold")}{stdio.color("purple")}SUITE{stdio.style("reset")} {name}`
|
return `{stdio.style("bold")}{stdio.color("purple")}SUITE{stdio.style("reset")} {name}`
|
||||||
end,
|
end,
|
||||||
|
|
||||||
report = function(name: string, state: "run" | "success" | "error" | "skip")
|
report = function(name: string, state: "run" | "success" | "error" | "skip", elapsed: number)
|
||||||
local state_color: stdio.Color = if state == "run"
|
local state_color: stdio.Color = if state == "run"
|
||||||
then "white"
|
then "white"
|
||||||
elseif state == "success" then "green"
|
elseif state == "success" then "green"
|
||||||
|
@ -17,7 +19,7 @@ local STYLE = table.freeze({
|
||||||
else error("Invalid test state")
|
else error("Invalid test state")
|
||||||
return ` {stdio.style("bold")}{stdio.color(state_color)}{if state == "skip" then "SKIP" else "TEST"}{stdio.style(
|
return ` {stdio.style("bold")}{stdio.color(state_color)}{if state == "skip" then "SKIP" else "TEST"}{stdio.style(
|
||||||
"reset"
|
"reset"
|
||||||
)} {name}`
|
)} {name} [{stdio.style("dim")}{string.format("%.2fms", elapsed)}{stdio.style("reset")}]`
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -31,8 +33,23 @@ function ReporterExt.init()
|
||||||
stdio.write("\n")
|
stdio.write("\n")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
local send_ts, recv_ts = watch((nil :: any) :: number)
|
||||||
|
|
||||||
|
frktest.test.on_test_enter(function()
|
||||||
|
-- Send over some high precision timestamp when the test starts
|
||||||
|
return send_ts(os.clock())
|
||||||
|
end)
|
||||||
|
|
||||||
frktest.test.on_test_leave(function(test)
|
frktest.test.on_test_leave(function(test)
|
||||||
print(STYLE.report(test.name, if test.failed then "error" else "success"))
|
print(
|
||||||
|
STYLE.report(
|
||||||
|
test.name,
|
||||||
|
if test.failed then "error" else "success",
|
||||||
|
|
||||||
|
-- Await receival of the timestamp and convert the difference to ms
|
||||||
|
(os.clock() - recv_ts()) * 1000
|
||||||
|
)
|
||||||
|
)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
frktest.test.on_test_skipped(function(test)
|
frktest.test.on_test_skipped(function(test)
|
||||||
|
|
Loading…
Reference in a new issue