Skip to content

Commit

Permalink
Fix handling of timeout error
Browse files Browse the repository at this point in the history
- Use mock instead of VCR cassette
- Simplify typespecs
  • Loading branch information
rupurt committed Feb 25, 2019
1 parent 29575d2 commit 4d9e82a
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 150 deletions.

This file was deleted.

24 changes: 0 additions & 24 deletions fixture/vcr_cassettes/rest/orders/amend_timeout.json

This file was deleted.

24 changes: 0 additions & 24 deletions fixture/vcr_cassettes/rest/orders/cancel_timeout.json

This file was deleted.

24 changes: 0 additions & 24 deletions fixture/vcr_cassettes/rest/orders/create_timeout.json

This file was deleted.

5 changes: 2 additions & 3 deletions lib/ex_bitmex/rest/http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ defmodule ExBitmex.Rest.HTTPClient do
{:error, reason, rate_limit}
end

defp parse_response({:error, %HTTPoison.Error{reason: "timeout"}, rate_limit}) do
{:error, :timeout, rate_limit}
end
defp parse_response({:error, %HTTPoison.Error{reason: :timeout}, nil}),
do: {:error, :timeout, nil}
end
17 changes: 4 additions & 13 deletions lib/ex_bitmex/rest/orders.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@ defmodule ExBitmex.Rest.Orders do
@type rate_limit :: ExBitmex.RateLimit.t()
@type auth_error_reason :: Rest.HTTPClient.auth_error_reason()
@type params :: map
@type error_msg :: String.t()
@type shared_error_reason :: :timeout | auth_error_reason | nonce_not_increasing_error_reason
@type insufficient_balance_error_reason :: {:insufficient_balance, error_msg}
@type nonce_not_increasing_error_reason :: {:nonce_not_increasing, error_msg}

@type create_error_reason ::
shared_error_reason
| insufficient_balance_error_reason
@type insufficient_balance_error_reason :: {:insufficient_balance, error_msg :: String.t()}
@type create_error_reason :: auth_error_reason | insufficient_balance_error_reason
@type amend_error_reason :: auth_error_reason | insufficient_balance_error_reason

@spec create(credentials, params) ::
{:ok, order, rate_limit} | {:error, create_error_reason, rate_limit | nil}
Expand All @@ -23,8 +18,6 @@ defmodule ExBitmex.Rest.Orders do
|> parse_response
end

@type amend_error_reason :: shared_error_reason | insufficient_balance_error_reason

@spec amend(credentials, params) ::
{:ok, order, rate_limit} | {:error, amend_error_reason, rate_limit | nil}
def amend(%ExBitmex.Credentials{} = credentials, params) when is_map(params) do
Expand All @@ -33,10 +26,8 @@ defmodule ExBitmex.Rest.Orders do
|> parse_response
end

@type cancel_error_reason :: shared_error_reason

@spec cancel(credentials, params) ::
{:ok, [order], rate_limit} | {:error, cancel_error_reason, rate_limit | nil}
{:ok, [order], rate_limit} | {:error, auth_error_reason, rate_limit | nil}
def cancel(%ExBitmex.Credentials{} = credentials, params) when is_map(params) do
"/order"
|> Rest.HTTPClient.auth_delete(credentials, params)
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ defmodule ExBitmex.Mixfile do
{:ex_doc, ">= 0.0.0", only: :dev},
{:dialyxir, "~> 1.0.0-rc.4", only: [:dev], runtime: false},
{:mix_test_watch, "~> 0.8", only: :dev, runtime: false},
{:mock, "~> 0.3.0", only: :test},
{:exvcr, "~> 0.10.0", only: [:dev, :test]},
{:ex_unit_notifier, "~> 0.1", only: :test},
{:excoveralls, "~> 0.10", only: :test}
Expand Down
3 changes: 2 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
"makeup_elixir": {:hex, :makeup_elixir, "0.13.0", "be7a477997dcac2e48a9d695ec730b2d22418292675c75aa2d34ba0909dcdeda", [:mix], [{:makeup, "~> 0.8", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
"mapail": {:hex, :mapail, "1.0.2", "45d0bc390b965f21b80c02e8fb2279f9bcc57cf5c78f964ef63113317d6944d1", [:mix], [{:maptu, "~> 1.0.0", [hex: :maptu, repo: "hexpm", optional: false]}], "hexpm"},
"maptu": {:hex, :maptu, "1.0.0", "3f1915d3241a733f46a39e935bed369faaf4ff69182b95ea58e9ad1b065c3b11", [:mix], [], "hexpm"},
"meck": {:hex, :meck, "0.8.12", "1f7b1a9f5d12c511848fec26bbefd09a21e1432eadb8982d9a8aceb9891a3cf2", [:rebar3], [], "hexpm"},
"meck": {:hex, :meck, "0.8.13", "ffedb39f99b0b99703b8601c6f17c7f76313ee12de6b646e671e3188401f7866", [:rebar3], [], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"},
"mix_test_watch": {:hex, :mix_test_watch, "0.9.0", "c72132a6071261893518fa08e121e911c9358713f62794a90c95db59042af375", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}], "hexpm"},
"mock": {:hex, :mock, "0.3.3", "42a433794b1291a9cf1525c6d26b38e039e0d3a360732b5e467bfc77ef26c914", [:mix], [{:meck, "~> 0.8.13", [hex: :meck, repo: "hexpm", optional: false]}], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.0", "90e2eca3d0266e5c53f8fbe0079694740b9c91b6747f2b7e3c5d21966bba8300", [:mix], [], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
Expand Down
15 changes: 8 additions & 7 deletions test/ex_bitmex/rest/http_client_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule ExBitmex.Rest.HTTPClientTest do
use ExUnit.Case, async: false
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
import Mock
doctest ExBitmex.Rest.HTTPClient

setup_all do
Expand All @@ -27,6 +28,13 @@ defmodule ExBitmex.Rest.HTTPClientTest do
end
end

test "returns an error tuple with no rate limits when the request times out" do
with_mock HTTPoison, request: fn _url -> {:error, %HTTPoison.Error{reason: :timeout}} end do
assert ExBitmex.Rest.HTTPClient.auth_request(:get, "/stats", @credentials, %{}) ==
{:error, :timeout, nil}
end
end

test "returns an error tuple when the params are invalid" do
use_cassette "rest/http_client/auth_request_error_bad_request" do
assert {:error, reason, _} =
Expand Down Expand Up @@ -57,13 +65,6 @@ defmodule ExBitmex.Rest.HTTPClientTest do
end
end

test "returns an error tuple with no rate limits when the request times out" do
use_cassette "rest/http_client/auth_request_error_timeout" do
assert {:error, :timeout, nil} =
ExBitmex.Rest.HTTPClient.auth_request(:get, "/stats", @credentials, %{})
end
end

test "returns an error tuple when the resource is not found" do
use_cassette "rest/http_client/auth_request_not_found" do
assert {:error, :not_found, _} =
Expand Down
20 changes: 10 additions & 10 deletions test/ex_bitmex/rest/orders/amend_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule ExBitmex.Rest.Orders.AmendTest do
use ExUnit.Case, async: false
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
import Mock

setup_all do
HTTPoison.start()
Expand Down Expand Up @@ -61,16 +62,15 @@ defmodule ExBitmex.Rest.Orders.AmendTest do
end
end

test ".amend returns an error tuple when there is a timeout" do
use_cassette "rest/orders/amend_timeout" do
assert {:error, :timeout, nil} =
ExBitmex.Rest.Orders.amend(
@credentials,
%{
orderID: "8d6f2649-7477-4db5-e32a-d8d5bf99dd9b",
leavesQty: 3
}
)
test ".amend bubbles other errors" do
with_mock HTTPoison, request: fn _url -> {:error, %HTTPoison.Error{reason: :timeout}} end do
assert ExBitmex.Rest.Orders.amend(
@credentials,
%{
orderID: "8d6f2649-7477-4db5-e32a-d8d5bf99dd9b",
leavesQty: 3
}
) == {:error, :timeout, nil}
end
end
end
15 changes: 7 additions & 8 deletions test/ex_bitmex/rest/orders/cancel_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ExBitmex.Rest.Orders.CancelTest do
use ExUnit.Case, async: false
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
doctest ExBitmex.Rest.Orders
import Mock

setup_all do
HTTPoison.start()
Expand Down Expand Up @@ -61,13 +61,12 @@ defmodule ExBitmex.Rest.Orders.CancelTest do
end
end

test ".cancel returns an error tuple when there is a timeout" do
use_cassette "rest/orders/cancel_timeout" do
assert {:error, timeout, nil} =
ExBitmex.Rest.Orders.cancel(
@credentials,
%{orderID: "8d6f2649-7477-4db5-e32a-d8d5bf99dd9b"}
)
test ".cancel bubbles other errors" do
with_mock HTTPoison, request: fn _url -> {:error, %HTTPoison.Error{reason: :timeout}} end do
assert ExBitmex.Rest.Orders.cancel(
@credentials,
%{orderID: "8d6f2649-7477-4db5-e32a-d8d5bf99dd9b"}
) == {:error, :timeout, nil}
end
end
end
24 changes: 12 additions & 12 deletions test/ex_bitmex/rest/orders/create_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule ExBitmex.Rest.Orders.CreateTest do
use ExUnit.Case, async: false
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
import Mock

setup_all do
HTTPoison.start()
Expand Down Expand Up @@ -80,18 +81,17 @@ defmodule ExBitmex.Rest.Orders.CreateTest do
end
end

test ".create returns an error tuple when there is a timeout" do
use_cassette "rest/orders/create_timeout" do
assert {:error, :timeout, nil} =
ExBitmex.Rest.Orders.create(
@credentials,
%{
symbol: "XBTUSD",
side: "Buy",
orderQty: 1_000_000,
price: 2000
}
)
test ".create bubbles other errors" do
with_mock HTTPoison, request: fn _url -> {:error, %HTTPoison.Error{reason: :timeout}} end do
assert ExBitmex.Rest.Orders.create(
@credentials,
%{
symbol: "XBTUSD",
side: "Buy",
orderQty: 1_000_000,
price: 2000
}
) == {:error, :timeout, nil}
end
end
end

0 comments on commit 4d9e82a

Please sign in to comment.