Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@henrik henrik commented Aug 13, 2014

Behövs -9?

@joakimk

This comment has been minimized.

Copy link
Owner Author

@joakimk 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