48 lines
854 B
Elixir
48 lines
854 B
Elixir
defmodule Mix.Tasks.TestPaxos do
|
|
use Mix.Task
|
|
|
|
def run(_) do
|
|
IO.puts("Testing Paxos")
|
|
|
|
procs = Enum.map(0..3, fn i -> String.to_atom("p#{i}") end)
|
|
_pids = Enum.map(procs, fn p -> start(p, procs) end)
|
|
|
|
IO.puts("spawned")
|
|
|
|
Process.send_after(self(), {:end}, 50000)
|
|
|
|
receive do
|
|
{:end} ->
|
|
IO.puts("test ended")
|
|
end
|
|
end
|
|
|
|
def start(name, procs) do
|
|
pid = spawn(Mix.Tasks.TestPaxos, :init, [name, procs])
|
|
|
|
Utils.register_name(name, pid)
|
|
end
|
|
|
|
def init(name, procs) do
|
|
IO.puts("#{name}: init")
|
|
Paxos.start(name, procs)
|
|
|
|
Process.sleep(200)
|
|
|
|
if name == :p0 do
|
|
Paxos.propose(name, "Hi I was decided")
|
|
end
|
|
|
|
run_test(name)
|
|
end
|
|
|
|
def run_test(name) do
|
|
receive do
|
|
{:decide, value} ->
|
|
IO.puts("#{name}: decide #{value}")
|
|
end
|
|
|
|
run_test(name)
|
|
end
|
|
end
|