Skip to content

Commit

Permalink
Add socket_opts from config
Browse files Browse the repository at this point in the history
Reorganize & refactor code; redis_opts returns only redis options and the rest are returned from connection_opts
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
On branch ssl - Thu 31 Aug 2017 23:11:57 PDT by Geoff Lee <geoff.lee@lendesk.com>

Fix tests

Add test for Exq.Support.Opts.connection_opts
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
On branch ssl - Tue  5 Sep 2017 15:14:25 PDT by Geoff Lee <geoff.lee@lendesk.com>
Amended commit - Tue  5 Sep 2017 15:21:13 PDT by Geoff Lee <geoff.lee@lendesk.com>
  • Loading branch information
geoff-lee-lendesk committed Sep 25, 2017
1 parent e315e80 commit 9f78775
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
21 changes: 14 additions & 7 deletions lib/exq/support/opts.ex
Expand Up @@ -18,13 +18,20 @@ defmodule Exq.Support.Opts do
mode = opts[:mode] || Config.get(:mode)
redis = redis_client_name(opts[:name])
opts = [{:redis, redis}|opts]
{redis_opts, connection_opts} = redis_opts(opts)

redis_opts = redis_opts(opts)
connection_opts = connection_opts(opts)
server_opts = server_opts(mode, opts)
{redis_opts, connection_opts, server_opts}
end

def redis_client_name(name) do
name = name || Config.get(:name)
"#{name}.Redis.Client" |> String.to_atom
end

def redis_opts(opts \\ []) do
redis_opts = if url = opts[:url] || Config.get(:url) do
if url = opts[:url] || Config.get(:url) do
url
else
host = opts[:host] || Config.get(:host)
Expand All @@ -33,14 +40,14 @@ defmodule Exq.Support.Opts do
password = opts[:password] || Config.get(:password)
[host: host, port: port, database: database, password: password]
end
end

def connection_opts(opts \\ []) do
reconnect_on_sleep = opts[:reconnect_on_sleep] || Config.get(:reconnect_on_sleep)
timeout = opts[:redis_timeout] || Config.get(:redis_timeout)
{redis_opts, [backoff: reconnect_on_sleep, timeout: timeout, name: opts[:redis]]}
end
socket_opts = opts[:socket_opts] || Config.get(:socket_opts) || []

def redis_client_name(name) do
name = name || Config.get(:name)
"#{name}.Redis.Client" |> String.to_atom
[backoff: reconnect_on_sleep, timeout: timeout, name: opts[:redis], socket_opts: socket_opts]
end

defp server_opts(:default, opts) do
Expand Down
35 changes: 20 additions & 15 deletions test/config_test.exs
Expand Up @@ -37,12 +37,12 @@ defmodule Exq.ConfigTest do
]
])

{[
[
host: host,
port: port,
database: database,
password: password
], _} = Exq.Support.Opts.redis_opts
] = Exq.Support.Opts.redis_opts

assert host == "127.0.0.1"
assert port == 6379
Expand All @@ -51,14 +51,14 @@ defmodule Exq.ConfigTest do

System.put_env("REDIS_URL", "redis_url")
Mix.Config.persist([exq: [url: {:system, "REDIS_URL"}]])
{redis_opts, _} = Exq.Support.Opts.redis_opts
redis_opts = Exq.Support.Opts.redis_opts
assert redis_opts == "redis_url"
end

test "redis_opts from runtime with defaults" do
Mix.Config.persist([exq: [url: {:system, "REDIS_URL", "default_redis_url"}]])

{redis_opts, _} = Exq.Support.Opts.redis_opts
redis_opts = Exq.Support.Opts.redis_opts
assert redis_opts == "default_redis_url"
end

Expand All @@ -72,36 +72,41 @@ defmodule Exq.ConfigTest do
end

test "redis_opts" do
Mix.Config.persist([exq: [host: "127.0.0.1", port: 6379, password: '', database: 0, reconnect_on_sleep: 100, redis_timeout: 5000]])
{[host: host, port: port, database: database, password: password],
[backoff: reconnect_on_sleep, timeout: timeout, name: client_name]}
= Exq.Support.Opts.redis_opts
Mix.Config.persist([exq: [host: "127.0.0.1", port: 6379, password: '', database: 0]])
[host: host, port: port, database: database, password: password] = Exq.Support.Opts.redis_opts

assert host == "127.0.0.1"
assert port == 6379
assert password == ''
assert database == 0
assert reconnect_on_sleep == 100
assert timeout == 5000
assert client_name == nil

Mix.Config.persist([exq: [host: '127.1.1.1', password: 'password']])
{redis_opts, _} = Exq.Support.Opts.redis_opts
redis_opts = Exq.Support.Opts.redis_opts
assert redis_opts[:host] == '127.1.1.1'
assert redis_opts[:password] == 'password'

Mix.Config.persist([exq: [password: "binary_password"]])
{redis_opts, _} = Exq.Support.Opts.redis_opts
redis_opts = Exq.Support.Opts.redis_opts
assert redis_opts[:password] == "binary_password"

Mix.Config.persist([exq: [password: nil]])
{redis_opts, _} = Exq.Support.Opts.redis_opts
redis_opts = Exq.Support.Opts.redis_opts
assert redis_opts[:password] == nil

Mix.Config.persist([exq: [url: "redis_url"]])
{redis_opts, _} = Exq.Support.Opts.redis_opts
redis_opts = Exq.Support.Opts.redis_opts
assert redis_opts == "redis_url"
end

test "connection_opts" do
Mix.Config.persist([exq: [reconnect_on_sleep: 100, redis_timeout: 5000]])
[backoff: reconnect_on_sleep, timeout: timeout, name: client_name, socket_opts: []] = Exq.Support.Opts.connection_opts

assert reconnect_on_sleep == 100
assert timeout == 5000
assert client_name == nil
end

test "default conform_opts" do
Mix.Config.persist([
exq: [
Expand Down

0 comments on commit 9f78775

Please sign in to comment.