Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update per elixir 1.4 deprecations #126

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: elixir

elixir:
- 1.3
- 1.4

otp_release:
- 18.3
Expand Down
43 changes: 22 additions & 21 deletions lib/rethinkdb/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ defmodule RethinkDB.Connection do
defmacro __using__(_opts) do
quote location: :keep do
def start_link(opts \\ []) do
if Dict.has_key?(opts, :name) && opts[:name] != __MODULE__ do
if Keyword.has_key?(opts, :name) && opts[:name] != __MODULE__ do
# The whole point of this macro is to provide an implicit process
# name, so subverting it is considered an error.
raise ArgumentError.exception(
"Process name #{inspect opts[:name]} conflicts with implicit name #{inspect __MODULE__} provided by `use RethinkDB.Connection`"
)
end
RethinkDB.Connection.start_link(Dict.put_new(opts, :name, __MODULE__))
RethinkDB.Connection.start_link(Keyword.put_new(opts, :name, __MODULE__))
end

def run(query, opts \\ []) do
Expand Down Expand Up @@ -98,12 +98,13 @@ defmodule RethinkDB.Connection do
* `profile` - whether or not to return a profile of the query’s execution (default: false).
"""
def run(query, conn, opts \\ []) do
timeout = Dict.get(opts, :timeout, 5000)
conn_opts = Dict.drop(opts, [:timeout])
noreply = Dict.get(opts, :noreply, false)
timeout = Keyword.get(opts, :timeout, 5000)
conn_opts = Keyword.drop(opts, [:timeout])
noreply = Keyword.get(opts, :noreply, false)
conn_opts = Connection.call(conn, :conn_opts)
|> Dict.take([:db])
|> Dict.merge(conn_opts)
|> Map.to_list()
|> Keyword.take([:db])
|> Keyword.merge(conn_opts)
query = prepare_and_encode(query, conn_opts)
msg = case noreply do
true -> {:query_noreply, query}
Expand Down Expand Up @@ -170,7 +171,7 @@ defmodule RethinkDB.Connection do
@doc """
Start connection as a linked process

Accepts a `Dict` of options. Supported options:
Accepts a `Keyword.t` of options. Supported options:

* `:host` - hostname to use to connect to database. Defaults to `'localhost'`.
* `:port` - port on which to connect to database. Defaults to `28015`.
Expand All @@ -182,26 +183,26 @@ defmodule RethinkDB.Connection do
* `:ca_certs` - a list of file paths to cacerts.
"""
def start_link(opts \\ []) do
args = Dict.take(opts, [:host, :port, :auth_key, :db, :sync_connect, :ssl, :max_pending])
args = Keyword.take(opts, [:host, :port, :auth_key, :db, :sync_connect, :ssl, :max_pending])
Connection.start_link(__MODULE__, args, opts)
end

def init(opts) do
host = case Dict.get(opts, :host, 'localhost') do
host = case Keyword.get(opts, :host, 'localhost') do
x when is_binary(x) -> String.to_char_list x
x -> x
end
sync_connect = Dict.get(opts, :sync_connect, false)
ssl = Dict.get(opts, :ssl)
opts = Dict.put(opts, :host, host)
|> Dict.put_new(:port, 28015)
|> Dict.put_new(:auth_key, "")
|> Dict.put_new(:max_pending, 10000)
|> Dict.drop([:sync_connect])
sync_connect = Keyword.get(opts, :sync_connect, false)
ssl = Keyword.get(opts, :ssl)
opts = Keyword.put(opts, :host, host)
|> Keyword.put_new(:port, 28015)
|> Keyword.put_new(:auth_key, "")
|> Keyword.put_new(:max_pending, 10000)
|> Keyword.drop([:sync_connect])
|> Enum.into(%{})
{transport, transport_opts} = case ssl do
nil -> {%Transport.TCP{}, []}
x -> {%Transport.SSL{}, Enum.map(Dict.fetch!(x, :ca_certs), &({:cacertfile, &1})) ++ [verify: :verify_peer]}
x -> {%Transport.SSL{}, Enum.map(Keyword.fetch!(x, :ca_certs), &({:cacertfile, &1})) ++ [verify: :verify_peer]}
end
state = %{
pending: %{},
Expand All @@ -228,11 +229,11 @@ defmodule RethinkDB.Connection do
:ok ->
:ok = Transport.setopts(socket, [active: :once])
# TODO: investigate timeout vs hibernate
{:ok, Dict.put(state, :socket, socket)}
{:ok, Map.put(state, :socket, socket)}
end
{:error, :econnrefused} ->
backoff = min(Dict.get(state, :timeout, 1000), 64000)
{:backoff, backoff, Dict.put(state, :timeout, backoff*2)}
backoff = min(Map.get(state, :timeout, 1000), 64000)
{:backoff, backoff, Map.put(state, :timeout, backoff*2)}
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/rethinkdb/connection/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule RethinkDB.Connection.Request do
def make_request(query, token, from, state = %{pending: pending, socket: socket}) do
new_pending = case from do
:noreply -> pending
_ -> Dict.put_new(pending, token, from)
_ -> Map.put_new(pending, token, from)
end
bsize = :erlang.size(query)
payload = token <> << bsize :: little-size(32) >> <> query
Expand Down Expand Up @@ -41,7 +41,7 @@ defmodule RethinkDB.Connection.Request do
case leftover <> data do
<< response :: binary-size(length), leftover :: binary >> ->
Connection.reply(pending[token], {response, token})
handle_recv("", %{state | current: {:start, leftover}, pending: Dict.delete(pending, token)})
handle_recv("", %{state | current: {:start, leftover}, pending: Map.delete(pending, token)})
new_data ->
{:noreply, %{state | current: {:length, length, token, new_data}}}
end
Expand Down
8 changes: 4 additions & 4 deletions lib/rethinkdb/prepare.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ defmodule RethinkDB.Prepare do
{Enum.into(map, %{}), state}
end
defp prepare(ref, state = {max, map}) when is_reference(ref) do
case Dict.get(map,ref) do
nil -> {max + 1, {max + 1, Dict.put_new(map, ref, max + 1)}}
case Map.get(map, ref) do
nil -> {max + 1, {max + 1, Map.put_new(map, ref, max + 1)}}
x -> {x, state}
end
end
defp prepare({k,v}, state) do
defp prepare({k, v}, state) do
{k, state} = prepare(k, state)
{v, state} = prepare(v, state)
{[k,v], state}
{[k, v], state}
end
defp prepare(el, state) do
{el, state}
Expand Down
2 changes: 1 addition & 1 deletion lib/rethinkdb/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1860,7 +1860,7 @@ defmodule RethinkDB.Query do

args = case arity do
0 -> []
_ -> Enum.map(1..arity, fn _ -> make_ref end)
_ -> Enum.map(1..arity, fn _ -> make_ref() end)
end
params = Enum.map(args, &var/1)

Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ defmodule RethinkDB.Mixfile do use Mix.Project
version: "0.4.0",
elixir: "~> 1.0",
description: "RethinkDB driver for Elixir",
package: package,
deps: deps,
package: package(),
deps: deps(),
test_coverage: [tool: ExCoveralls]]
end

Expand Down
10 changes: 5 additions & 5 deletions test/changes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ defmodule ChangesTest do

@table_name "changes_test_table_1"
setup_all do
start_link
start_link()
table_create(@table_name) |> run
on_exit fn ->
start_link
start_link()
table_drop(@table_name) |> run
end
:ok
Expand Down Expand Up @@ -43,18 +43,18 @@ defmodule ChangesTest do
q = table(@table_name) |> insert(data)
{:ok, res} = run(q)
expected = res.data["id"]
{:ok, changes} = Task.await(t)
{:ok, changes} = Task.await(t)
^expected = changes.data |> hd |> Map.get("id")

# test Enumerable
t = Task.async fn ->
changes |> Enum.take(5)
changes |> Enum.take(5)
end
1..6 |> Enum.each(fn _ ->
q = table(@table_name) |> insert(data)
run(q)
end)
data = Task.await(t)
data = Task.await(t)
5 = Enum.count(data)
end

Expand Down
18 changes: 9 additions & 9 deletions test/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ defmodule ConnectionTest do
test "reconnects if initial connect fails" do
{:ok, c} = start_link(port: 28014)
Process.unlink(c)
%RethinkDB.Exception.ConnectionClosed{} = table_list |> run
%RethinkDB.Exception.ConnectionClosed{} = table_list() |> run
conn = FlakyConnection.start('localhost', 28015, [local_port: 28014])
:timer.sleep(1000)
{:ok, %RethinkDB.Record{}} = RethinkDB.Query.table_list |> run
{:ok, %RethinkDB.Record{}} = RethinkDB.Query.table_list() |> run
ref = Process.monitor(c)
FlakyConnection.stop(conn)
receive do
Expand All @@ -47,7 +47,7 @@ defmodule ConnectionTest do
table = "foo_flaky_test"
RethinkDB.Query.table_create(table)|> run
on_exit fn ->
start_link
start_link()
:timer.sleep(100)
RethinkDB.Query.table_drop(table) |> run
GenServer.cast(__MODULE__, :stop)
Expand Down Expand Up @@ -84,15 +84,15 @@ defmodule ConnectionTest do
{:ok, c} = RethinkDB.Connection.start_link(db: "new_test")
db_create("new_test") |> RethinkDB.run(c)
db("new_test") |> table_create("new_test_table") |> RethinkDB.run(c)
{:ok, %{data: data}} = table_list |> RethinkDB.run(c)
{:ok, %{data: data}} = table_list() |> RethinkDB.run(c)
assert data == ["new_test_table"]
end

test "connection accepts max_pending" do
{:ok, c} = RethinkDB.Connection.start_link(max_pending: 1)
res = Enum.map(1..100, fn (_) ->
Task.async fn ->
now |> RethinkDB.run(c)
now() |> RethinkDB.run(c)
end
end) |> Enum.map(&Task.await/1)
assert Enum.any?(res, &(&1 == %RethinkDB.Exception.TooManyRequests{}))
Expand All @@ -109,7 +109,7 @@ defmodule ConnectionTest do
test "ssl connection" do
conn = FlakyConnection.start('localhost', 28015, [ssl: [keyfile: "./test/cert/host.key", certfile: "./test/cert/host.crt"]])
{:ok, c} = RethinkDB.Connection.start_link(port: conn.port, ssl: [ca_certs: ["./test/cert/rootCA.pem"]], sync_connect: true)
{:ok, %{data: _}} = table_list |> RethinkDB.run(c)
{:ok, %{data: _}} = table_list() |> RethinkDB.run(c)
end
end

Expand All @@ -119,15 +119,15 @@ defmodule ConnectionRunTest do
import RethinkDB.Query

setup_all do
start_link
start_link()
:ok
end

test "run(conn, opts) with :db option" do
db_create("db_option_test") |> run
table_create("db_option_test_table") |> run(db: "db_option_test")

{:ok, %{data: data}} = db("db_option_test") |> table_list |> run
{:ok, %{data: data}} = db("db_option_test") |> table_list() |> run

db_drop("db_option_test") |> run

Expand All @@ -149,7 +149,7 @@ defmodule ConnectionRunTest do

test "run with :noreply option" do
:ok = make_array([1,2,3]) |> run(noreply: true)
noreply_wait
noreply_wait()
end

test "run with :profile options" do
Expand Down
8 changes: 4 additions & 4 deletions test/prepare_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ defmodule PrepareTest do

test "single elements" do
assert prepare(1) == 1
assert prepare(make_ref) == 1
assert prepare(make_ref()) == 1
end

test "list" do
assert prepare([1,2,3]) == [1,2,3]
assert prepare([1,2,make_ref,make_ref]) == [1,2,1,2]
assert prepare([1,2,make_ref(),make_ref()]) == [1,2,1,2]
end

test "nested list" do
list = [1, [1,2], [1, [1, 2]]]
assert prepare(list) == list
list = [1, [make_ref, make_ref], make_ref, [1, 2]]
list = [1, [make_ref(), make_ref()], make_ref(), [1, 2]]
assert prepare(list) == [1, [1, 2], 3, [1, 2]]
end

test "map" do
map = %{a: 1, b: 2}
assert prepare(map) == map
map = %{a: 1, b: make_ref, c: make_ref}
map = %{a: 1, b: make_ref(), c: make_ref()}
assert prepare(map) == %{a: 1, b: 1, c: 2}
end
end
4 changes: 2 additions & 2 deletions test/query/administration_query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule AdministrationQueryTest do
import RethinkDB.Query

setup_all do
start_link
start_link()
:ok
end

Expand All @@ -16,7 +16,7 @@ defmodule AdministrationQueryTest do
end
:ok
end

test "config" do
{:ok, r} = table(@table_name) |> config |> run
assert %RethinkDB.Record{data: %{"db" => "test"}} = r
Expand Down
2 changes: 1 addition & 1 deletion test/query/aggregation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule AggregationTest do
import RethinkDB.Lambda

setup_all do
start_link
start_link()
:ok
end

Expand Down
4 changes: 2 additions & 2 deletions test/query/control_structures_adv_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ defmodule ControlStructuresAdvTest do

@table_name "control_test_table_1"
setup_all do
start_link
start_link()
q = table_create(@table_name)
run(q)
on_exit fn ->
start_link
start_link()
table_drop(@table_name) |> run
end
:ok
Expand Down
8 changes: 4 additions & 4 deletions test/query/control_structures_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule ControlStructuresTest do
alias RethinkDB.Response

setup_all do
start_link
start_link()
:ok
end

Expand Down Expand Up @@ -39,10 +39,10 @@ defmodule ControlStructuresTest do
test "branch" do
q = branch(true, 1, 2)
{:ok, %Record{data: data}} = run q
assert data == 1
assert data == 1
q = branch(false, 1, 2)
{:ok, %Record{data: data}} = run q
assert data == 2
assert data == 2
end

test "error" do
Expand Down Expand Up @@ -105,7 +105,7 @@ defmodule ControlStructuresTest do
end

test "uuid" do
q = uuid
q = uuid()
{:ok, %Record{data: data}} = run q
assert String.length(String.replace(data, "-", "")) == 32
end
Expand Down
Loading