Skip to content

Instantly share code, notes, and snippets.

@joakimk
Created August 12, 2014 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joakimk/885aeb78e68eed0903c8 to your computer and use it in GitHub Desktop.
Save joakimk/885aeb78e68eed0903c8 to your computer and use it in GitHub Desktop.
Starting a redis server when running elixir tests
Code.require_file "support/test_redis.exs", __DIR__
ExUnit.start
TestRedis.start
defmodule TestRedis do
def start do
run("echo 'daemonize yes\npidfile #{pid_file_path}\nport #{port}' | redis-server -")
System.at_exit(&TestRedis.stop/1)
end
def stop(_exit_code) do
{:ok, pid} = File.read(pid_file_path)
run("kill -9 #{pid}")
end
defp run(cmd) do
cmd |> String.to_char_list |> :os.cmd
end
defp port do
# Get port from config here perhaps?
6395
end
defp pid_file_path do
"/tmp/redis_test.pid"
end
end
@henrik
Copy link

henrik commented Aug 13, 2014

Behövs -9?

@joakimk
Copy link
Author

joakimk commented Aug 13, 2014

"Is -9 needed?"

Not entirely sure -9 is needed, but it shouldn't have any bad side effects in a test env (e.g. we don't care about the data). It made writing this faster, otherwise I'd have to figure out if redis respected a normal kill in all situations, many processes don't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment