-
Notifications
You must be signed in to change notification settings - Fork 8
/
example_test.exs
87 lines (73 loc) · 2.3 KB
/
example_test.exs
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
78
79
80
81
82
83
84
85
86
87
defmodule Mutex.ExampleTest do
require Logger
use ExUnit.Case, async: true
@moduletag :capture_log
setup do
{:ok, _pid} = start_supervised({Mutex, name: MyApp.Mutex})
:ok
end
describe "basic usage" do
test "file count" do
path = "/tmp/counter-#{System.system_time(:microsecond)}"
File.write!(path, "0")
tasks =
for _ <- 1..10 do
Task.async(fn ->
Mutex.with_lock(MyApp.Mutex, :file_manager, fn ->
counter = String.to_integer(File.read!(path))
File.write!(path, Integer.to_string(counter + 1))
end)
end)
end
Enum.each(tasks, &Task.await/1)
# counter = String.to_integer(File.read!(path))
# IO.puts("Total count is: #{counter}")
end
end
describe "give away" do
defmodule Encoder do
def encode_video(video_path) do
send(:test_process, {:encoding, video_path})
:ok
end
def cleanup(video_path) do
send(:test_process, {:cleaning, video_path})
:ok
end
end
def handle_request(encoding_request) do
case Mutex.lock(MyApp.Mutex, :encoding_server) do
{:error, :busy} ->
"encoding not available"
{:ok, lock} ->
{:ok, task_pid} =
Task.Supervisor.start_child(
Encoding.Supervisor,
&encode_video/0
)
# Here we pass the video path as the "gift data" but in the real world
# that should be given as an argument to the task function.
:ok = Mutex.give_away(MyApp.Mutex, lock, task_pid, encoding_request.path)
"encoding started"
end
end
def encode_video do
receive do
{:"MUTEX-TRANSFER", _, lock, video_path} ->
Encoder.encode_video(video_path)
Mutex.release(MyApp.Mutex, lock)
Encoder.cleanup(video_path)
after
1000 -> exit(:no_lock_received)
end
end
test "readme example" do
Process.register(self(), :test_process)
{:ok, _} = Task.Supervisor.start_link(name: Encoding.Supervisor)
handle_request(%{path: "/some/path"})
assert_receive {:encoding, "/some/path"}
assert %Mutex.Lock{} = Mutex.await(MyApp.Mutex, :encoding_server)
assert_receive {:cleaning, "/some/path"}
end
end
end