this repo has no description
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Seed random generators

garrison d1cf7532 457f2cab

+17 -10
-4
lib/trinity/scheduler.ex
··· 48 48 @spec get_sim :: Simulation.t 49 49 defp get_sim, do: Process.get(simulation_key()) 50 50 51 - def run_simulation(fun) do 52 - SimulationSupervisor.run_simulation(fun) 53 - end 54 - 55 51 @spec now :: integer 56 52 def now do 57 53 %{now: now} = get_sim()
+8 -3
lib/trinity/scheduler/simulation_supervisor.ex
··· 15 15 defstruct @enforce_keys 16 16 end 17 17 18 - @spec run_simulation(function) :: :ok 19 - def run_simulation(fun) do 18 + @spec run_simulation(function, %{seed: integer}) :: :ok 19 + def run_simulation(fun, %{seed: seed}) do 20 20 ref = make_ref() 21 21 GenServer.start_link(__MODULE__, %{ 22 + seed: seed, 22 23 fun: fun, 23 24 parent_pid: self(), 24 25 parent_ref: ref, ··· 37 38 GenServer.call(server, {:spawn_child, fun}) 38 39 end 39 40 40 - def init(%{fun: fun, parent_pid: parent_pid, parent_ref: parent_ref}) do 41 + def init(%{seed: seed, fun: fun, parent_pid: parent_pid, parent_ref: parent_ref}) do 41 42 Process.flag(:trap_exit, true) 43 + :rand.seed(:exsss, seed) 42 44 43 45 sim = %Simulation{ 44 46 queue: :ets.new(__MODULE__, [:ordered_set, :public]), ··· 148 150 end 149 151 150 152 defp spawn_sim_child(%Simulation{} = sim, fun) do 153 + # TODO: larger range? 154 + child_seed = Enum.random(1..1_000_000) 151 155 spawn_link(fn -> 156 + :rand.seed(:exsss, child_seed) 152 157 Process.put(simulation_key(), sim) 153 158 fun.() 154 159 end)
+6
lib/trinity/sim.ex
··· 5 5 @spec get_sim :: Simulation.t | nil 6 6 defp get_sim, do: Process.get(simulation_key()) 7 7 8 + @spec run_simulation(function, keyword) :: :ok 9 + def run_simulation(fun, opts \\ []) do 10 + seed = Keyword.get(opts, :seed, 100) 11 + Trinity.Scheduler.SimulationSupervisor.run_simulation(fun, %{seed: seed}) 12 + end 13 + 8 14 @spec simulated? :: boolean 9 15 def simulated? do 10 16 case get_sim() do
+3 -3
test/trinity_test.exs
··· 1 1 defmodule TrinityTest do 2 2 use ExUnit.Case 3 3 4 - alias Trinity.{SimProcess, SimLogger, Scheduler} 4 + alias Trinity.{Sim, SimProcess, SimLogger, Scheduler} 5 5 import Trinity.Scheduler, only: [receive_yield: 1] 6 6 require SimLogger 7 7 ··· 53 53 end 54 54 55 55 test "scheduler" do 56 - Scheduler.run_simulation(fn -> 56 + Sim.run_simulation(fn -> 57 57 nodes = [:n1, :n2, :n3] 58 58 pids = 59 59 Enum.map(nodes, fn node -> ··· 98 98 99 99 Scheduler.yield(1000) 100 100 #dbg Scheduler.dump(), limit: :infinity 101 - end) 101 + end, seed: 101) 102 102 end 103 103 end