Repository navigation
Expand file tree
/
Copy pathprofiler_test.exs
More file actions
77 lines (60 loc) · 2.04 KB
/
Copy pathprofiler_test.exs
File metadata and controls
77 lines (60 loc) · 2.04 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
defmodule ProfilerTest do
use ExUnit.Case, async: true
describe "warn_if_stuck/2 custom fun callback" do
test "arity-1 fun receives the monitored pid, not the monitor process pid" do
parent = self()
target = spawn(fn -> Process.sleep(1_000) end)
Profiler.warn_if_stuck(target,
timeout: 50,
fun: fn pid -> send(parent, {:reported, pid, self()}) end
)
assert_receive {:reported, reported_pid, monitor_pid}, 1_000
assert reported_pid == target
assert monitor_pid != target
assert monitor_pid != parent
end
test "arity-1 fun allows formatting the stacktrace of the monitored process" do
parent = self()
target =
spawn(fn ->
receive do
:stop -> :ok
end
end)
Profiler.warn_if_stuck(target,
timeout: 50,
fun: fn pid -> send(parent, {:trace, Profiler.format_stacktrace(pid)}) end
)
assert_receive {:trace, trace}, 1_000
# The report must be about the target process, not the monitor process
assert trace =~ inspect(target)
send(target, :stop)
end
test "arity-0 fun is still supported for backwards compatibility" do
parent = self()
target = spawn(fn -> Process.sleep(1_000) end)
Profiler.warn_if_stuck(target, timeout: 50, fun: fn -> send(parent, :reported) end)
assert_receive :reported, 1_000
end
test "no report is produced when the monitored process finishes in time" do
parent = self()
target = spawn(fn -> :ok end)
Profiler.warn_if_stuck(target,
timeout: 50,
fun: fn pid -> send(parent, {:reported, pid}) end
)
refute_receive {:reported, _}, 300
end
test "function form auto-cancels and returns the value" do
parent = self()
result =
Profiler.warn_if_stuck(
fn -> :done end,
timeout: 50,
fun: fn pid -> send(parent, {:reported, pid}) end
)
assert result == :done
refute_receive {:reported, _}, 300
end
end
end