This repository has been archived on 2024-01-29. You can view files and clone it, but cannot push or open issues or pull requests.
distributed_system_coursework/lib/mix/tasks/test_paxos.ex
2023-11-28 21:42:16 +00:00

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