Tim Buchwaldt 7 vuotta sitten
commit
db2dfb96a0

+ 4 - 0
.formatter.exs

@@ -0,0 +1,4 @@
+# Used by "mix format"
+[
+  inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
+]

+ 24 - 0
.gitignore

@@ -0,0 +1,24 @@
+# The directory Mix will write compiled artifacts to.
+/_build/
+
+# If you run "mix test --cover", coverage assets end up here.
+/cover/
+
+# The directory Mix downloads your dependencies sources to.
+/deps/
+
+# Where 3rd-party dependencies like ExDoc output generated docs.
+/doc/
+
+# Ignore .fetch files in case you like to edit your project deps locally.
+/.fetch
+
+# If the VM crashes, it generates a dump, let's ignore it too.
+erl_crash.dump
+
+# Also ignore archive artifacts (built via "mix archive.build").
+*.ez
+
+# Ignore package tarball (built via "mix hex.build").
+gen_reset-*.tar
+

+ 21 - 0
README.md

@@ -0,0 +1,21 @@
+# GenReset
+
+**TODO: Add description**
+
+## Installation
+
+If [available in Hex](https://hex.pm/docs/publish), the package can be installed
+by adding `gen_reset` to your list of dependencies in `mix.exs`:
+
+```elixir
+def deps do
+  [
+    {:gen_reset, "~> 0.1.0"}
+  ]
+end
+```
+
+Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
+and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
+be found at [https://hexdocs.pm/gen_reset](https://hexdocs.pm/gen_reset).
+

+ 30 - 0
config/config.exs

@@ -0,0 +1,30 @@
+# This file is responsible for configuring your application
+# and its dependencies with the aid of the Mix.Config module.
+use Mix.Config
+
+# This configuration is loaded before any dependency and is restricted
+# to this project. If another project depends on this project, this
+# file won't be loaded nor affect the parent project. For this reason,
+# if you want to provide default values for your application for
+# 3rd-party users, it should be done in your "mix.exs" file.
+
+# You can configure your application as:
+#
+#     config :gen_reset, key: :value
+#
+# and access this configuration in your application as:
+#
+#     Application.get_env(:gen_reset, :key)
+#
+# You can also configure a 3rd-party app:
+#
+#     config :logger, level: :info
+#
+
+# It is also possible to import configuration files, relative to this
+# directory. For example, you can emulate configuration per environment
+# by uncommenting the line below and defining dev.exs, test.exs and such.
+# Configuration from the imported file will override the ones defined
+# here (which is why it is important to import them last).
+#
+#     import_config "#{Mix.env()}.exs"

+ 38 - 0
lib/gen_reset.ex

@@ -0,0 +1,38 @@
+defmodule GenReset do
+  @moduledoc """
+  Documentation for GenServerReset.
+
+  GenServerReset allows GenServers to be reset during testing.
+  """
+
+  defmacro __using__(_args) do
+    quote do
+      def handle_info(:"$gen_reset", state) do
+        case init(:erlang.get(:"$gen_reset_initial_args")) do
+          {:ok, state} ->
+            {:noreply, state}
+          {:ok, state, continue} ->
+            {:noreply, state, continue}
+        end
+      end
+
+      def register_gen_reset(args) do
+        :erlang.put(:"$gen_reset_initial_args", args)
+      end
+    end
+  end
+end
+
+
+
+defmodule Server do
+  use GenServer
+  use GenReset
+
+  def init(args) do
+    register_gen_reset(args)
+    {:ok, args}
+  end
+  def handle_call(:state, _from, state), do: {:reply, state, state}
+  def handle_cast({:set, value}, _state), do: {:noreply, value}
+end

+ 21 - 0
lib/gen_reset/application.ex

@@ -0,0 +1,21 @@
+defmodule GenReset.Application do
+  # See https://hexdocs.pm/elixir/Application.html
+  # for more information on OTP Applications
+  @moduledoc false
+
+  use Application
+
+  def start(_type, _args) do
+    # List all child processes to be supervised
+    children = [
+      # Starts a worker by calling: GenReset.Worker.start_link(arg)
+      # {GenReset.Worker, arg},
+      {GenReset.Tracker, []}
+    ]
+
+    # See https://hexdocs.pm/elixir/Supervisor.html
+    # for other strategies and supported options
+    opts = [strategy: :one_for_one, name: GenReset.Supervisor]
+    Supervisor.start_link(children, opts)
+  end
+end

+ 44 - 0
lib/gen_reset/gen_reset_tracker.ex

@@ -0,0 +1,44 @@
+defmodule GenReset.Tracker do
+	use GenServer
+	use GenReset
+
+	@initial_state []
+
+	def start_link(_args) do
+		GenServer.start_link(__MODULE__, nil, name: __MODULE__)
+	end
+
+	def init(_args) do
+    register_gen_reset(@initial_state)
+    {:ok, @initial_state}
+  end
+
+  def add(pid) do
+  	GenServer.call(__MODULE__, {:add, pid})
+  end
+
+  def remove(pid) do
+  	GenServer.call(__MODULE__, {:remove, pid})
+  end
+
+  def pids do
+  	GenServer.call(__MODULE__, :pids)
+  end
+
+  def handle_call({:add, pid}, _from, state) do
+  	Process.monitor(pid)
+  	{:reply, :ok, state ++ [pid]}
+  end
+
+  def handle_call({:remove, pid}, _from, state) do
+		{:reply, :ok, state -- [pid]}
+  end
+
+  def handle_info({:DOWN, _ref, :process, object, _reason}, state) do
+  	IO.puts "Received down message"
+  	IO.inspect(object)
+		{:noreply, state -- [object]}
+  end
+
+  def handle_call(:pids, _from, state), do: {:reply, state, state}
+end

+ 29 - 0
mix.exs

@@ -0,0 +1,29 @@
+defmodule GenReset.MixProject do
+  use Mix.Project
+
+  def project do
+    [
+      app: :gen_reset,
+      version: "0.1.0",
+      elixir: "~> 1.7-dev",
+      start_permanent: Mix.env() == :prod,
+      deps: deps()
+    ]
+  end
+
+  # Run "mix help compile.app" to learn about applications.
+  def application do
+    [
+      extra_applications: [:logger],
+      mod: {GenReset.Application, []}
+    ]
+  end
+
+  # Run "mix help deps" to learn about dependencies.
+  defp deps do
+    [
+      # {:dep_from_hexpm, "~> 0.3.0"},
+      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"},
+    ]
+  end
+end

+ 13 - 0
test/gen_reset_test.exs

@@ -0,0 +1,13 @@
+defmodule GenResetTest do
+  use ExUnit.Case
+
+  test "resets state manually" do
+  	{:ok, pid} = GenServer.start_link(Server, :fresh_state)
+  	assert GenServer.call(pid, :state) == :fresh_state
+  	GenServer.cast(pid, {:set, :foo})
+  	assert GenServer.call(pid, :state) == :foo
+
+  	send(pid, :"$gen_reset")
+  	assert GenServer.call(pid, :state) == :fresh_state
+  end
+end

+ 53 - 0
test/gen_reset_tracker_test.exs

@@ -0,0 +1,53 @@
+defmodule GenReset.TrackerTest do
+  use ExUnit.Case
+
+  setup do
+  	send(GenReset.Tracker, :"$gen_reset")
+  	:ok
+  end
+
+  test "adding pids" do
+		assert GenReset.Tracker.add(self()) == :ok
+		assert GenReset.Tracker.pids == [self()]
+  end
+
+  test "removing existing pid" do
+  	assert GenReset.Tracker.add(self()) == :ok
+		assert GenReset.Tracker.pids == [self()]
+  	assert GenReset.Tracker.remove(self()) == :ok
+		assert GenReset.Tracker.pids == []
+
+  end
+
+  test "dead pid is automatically removed" do
+  	pid = Process.spawn(fn ->
+  		receive do
+  			_ -> :ok
+  		end
+  	 end, [])
+  	GenReset.Tracker.add(pid)
+  	assert GenReset.Tracker.pids == [pid]
+  	Process.exit(pid, :kill)
+  	TimeHelper.wait_until(fn ->
+	  	assert GenReset.Tracker.pids == []
+	  end)
+   end
+end
+
+defmodule TimeHelper do
+
+  def wait_until(fun), do: wait_until(500, fun)
+
+  def wait_until(0, fun), do: fun.()
+
+  def wait_until(timeout, fun) do
+    try do
+      fun.()
+    rescue
+      ExUnit.AssertionError ->
+        :timer.sleep(10)
+        wait_until(max(0, timeout - 10), fun)
+    end
+  end
+
+end

+ 1 - 0
test/test_helper.exs

@@ -0,0 +1 @@
+ExUnit.start()