Created
March 21, 2017 17:22
-
-
Save Arkham/a91694349eecebb5854e845d3b4762fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MutexTest do | |
use ExUnit.Case, async: false | |
doctest Mutex | |
setup do | |
Mutex.start | |
on_exit fn -> | |
Mutex.stop | |
end | |
end | |
test "acquiring mutex" do | |
assert Mutex.wait() == :ok | |
Mutex.signal() | |
assert Mutex.wait() == :ok | |
end | |
test "two processes test" do | |
Mutex.wait() | |
current = self() | |
spawn(fn -> | |
Process.sleep(1) | |
value = Mutex.wait() | |
send current, {:value_received, value} | |
end) | |
Mutex.signal() | |
assert_receive {:value_received, :ok} | |
end | |
test "two processes test waiting for mutex" do | |
Mutex.wait() | |
current = self() | |
spawn(fn -> | |
value = Mutex.wait() | |
send current, {:value_received, value} | |
end) | |
Process.sleep(1) | |
Mutex.signal() | |
assert_receive {:value_received, :ok} | |
end | |
test "other two processes test" do | |
Mutex.wait() | |
current = self() | |
spawn(fn -> | |
IO.inspect current | |
IO.inspect self() | |
case Mutex.signal() do | |
:ok -> send current, {:value_received, :ok} | |
_ -> :ok | |
end | |
end) | |
Process.sleep(1) | |
Mutex.signal() | |
refute_receive {:value_received, :ok} | |
end | |
test "call wait twice in same process" do | |
Mutex.wait() | |
assert :ok = Mutex.wait() | |
end | |
test "call signal twice, should error" do | |
Mutex.signal() | |
assert :error = Mutex.signal() | |
end | |
test "when other process crashes" do | |
spawn(fn -> | |
:ok = Mutex.wait() | |
exit(:funny_reason) | |
end) | |
Process.sleep(1) | |
assert :ok = Mutex.wait() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment