Created
August 12, 2014 23:18
-
-
Save joakimk/885aeb78e68eed0903c8 to your computer and use it in GitHub Desktop.
Starting a redis server when running elixir tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Code.require_file "support/test_redis.exs", __DIR__ | |
ExUnit.start | |
TestRedis.start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
"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
Behövs -9?