|
| 1 | +Code.require_file("../test_helper.exs", __DIR__) |
| 2 | + |
| 3 | +defmodule Mix.StateTest do |
| 4 | + use ExUnit.Case, async: true |
| 5 | + |
| 6 | + describe "lock" do |
| 7 | + test "executes functions" do |
| 8 | + assert Mix.State.lock(:key, fn -> :it_works! end) == :it_works! |
| 9 | + assert Mix.State.lock(:key, fn -> :still_works! end) == :still_works! |
| 10 | + end |
| 11 | + |
| 12 | + test "releases lock on error" do |
| 13 | + assert_raise RuntimeError, fn -> |
| 14 | + Mix.State.lock(:key, fn -> raise "oops" end) |
| 15 | + end |
| 16 | + |
| 17 | + assert Mix.State.lock(:key, fn -> :still_works! end) == :still_works! |
| 18 | + end |
| 19 | + |
| 20 | + test "releases lock on exit" do |
| 21 | + {_pid, ref} = |
| 22 | + spawn_monitor(fn -> |
| 23 | + Mix.State.lock(:key, fn -> Process.exit(self(), :kill) end) |
| 24 | + end) |
| 25 | + |
| 26 | + assert_receive {:DOWN, ^ref, _, _, _} |
| 27 | + assert Mix.State.lock(:key, fn -> :still_works! end) == :still_works! |
| 28 | + end |
| 29 | + |
| 30 | + test "blocks until released" do |
| 31 | + parent = self() |
| 32 | + |
| 33 | + task = |
| 34 | + Task.async(fn -> |
| 35 | + Mix.State.lock(:key, fn -> |
| 36 | + send(parent, :locked) |
| 37 | + assert_receive :will_lock |
| 38 | + :it_works! |
| 39 | + end) |
| 40 | + end) |
| 41 | + |
| 42 | + assert_receive :locked |
| 43 | + send(task.pid, :will_lock) |
| 44 | + assert Mix.State.lock(:key, fn -> :still_works! end) == :still_works! |
| 45 | + assert Task.await(task) == :it_works! |
| 46 | + end |
| 47 | + |
| 48 | + test "blocks until released on error" do |
| 49 | + parent = self() |
| 50 | + |
| 51 | + {pid, ref} = |
| 52 | + spawn_monitor(fn -> |
| 53 | + Mix.State.lock(:key, fn -> |
| 54 | + send(parent, :locked) |
| 55 | + assert_receive :will_lock |
| 56 | + raise "oops" |
| 57 | + end) |
| 58 | + end) |
| 59 | + |
| 60 | + assert_receive :locked |
| 61 | + send(pid, :will_lock) |
| 62 | + assert Mix.State.lock(:key, fn -> :still_works! end) == :still_works! |
| 63 | + assert_receive {:DOWN, ^ref, _, _, _} |
| 64 | + end |
| 65 | + |
| 66 | + test "blocks until released on exit" do |
| 67 | + parent = self() |
| 68 | + |
| 69 | + {pid, ref} = |
| 70 | + spawn_monitor(fn -> |
| 71 | + Mix.State.lock(:key, fn -> |
| 72 | + send(parent, :locked) |
| 73 | + assert_receive :will_not_lock |
| 74 | + end) |
| 75 | + end) |
| 76 | + |
| 77 | + assert_receive :locked |
| 78 | + Process.exit(pid, :kill) |
| 79 | + assert Mix.State.lock(:key, fn -> :still_works! end) == :still_works! |
| 80 | + assert_receive {:DOWN, ^ref, _, _, _} |
| 81 | + end |
| 82 | + |
| 83 | + test "scheduls and releases on exit" do |
| 84 | + assert Mix.State.lock(:key, fn -> |
| 85 | + {pid, ref} = |
| 86 | + spawn_monitor(fn -> |
| 87 | + Mix.State.lock(:key, fn -> |
| 88 | + raise "this will never be invoked" |
| 89 | + end) |
| 90 | + end) |
| 91 | + |
| 92 | + Process.exit(pid, :kill) |
| 93 | + assert_receive {:DOWN, ^ref, _, _, :killed} |
| 94 | + :it_works! |
| 95 | + end) == :it_works! |
| 96 | + |
| 97 | + assert Mix.State.lock(:key, fn -> :still_works! end) == :still_works! |
| 98 | + end |
| 99 | + end |
| 100 | +end |
0 commit comments