Skip to content

Commit b6c332c

Browse files
authored
Merge pull request #106 from thiamsantos/ts-raise-open
Raise on Bypass.open/1 errors
2 parents 464cab0 + c1b8e8d commit b6c332c

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

lib/bypass.ex

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,38 @@ defmodule Bypass do
3939
```
4040
4141
"""
42-
@spec open(Keyword.t()) :: Bypass.t() | DynamicSupervisor.on_start_child()
42+
@spec open(Keyword.t()) :: Bypass.t()
4343
def open(opts \\ []) do
44+
pid = start_instance(opts)
45+
port = Bypass.Instance.call(pid, :port)
46+
debug_log("Did open connection #{inspect(pid)} on port #{inspect(port)}")
47+
bypass = %Bypass{pid: pid, port: port}
48+
setup_framework_integration(test_framework(), bypass)
49+
bypass
50+
end
51+
52+
defp start_instance(opts) do
4453
case DynamicSupervisor.start_child(Bypass.Supervisor, Bypass.Instance.child_spec(opts)) do
4554
{:ok, pid} ->
46-
port = Bypass.Instance.call(pid, :port)
47-
debug_log("Did open connection #{inspect(pid)} on port #{inspect(port)}")
48-
bypass = %Bypass{pid: pid, port: port}
49-
setup_framework_integration(test_framework(), bypass)
50-
bypass
51-
52-
other ->
53-
other
55+
pid
56+
57+
{:ok, pid, _info} ->
58+
pid
59+
60+
{:error, reason} ->
61+
raise "Failed to start bypass instance.\n" <>
62+
"Reason: #{start_supervised_error(reason)}"
5463
end
5564
end
5665

66+
defp start_supervised_error({{:EXIT, reason}, info}) when is_tuple(info),
67+
do: Exception.format_exit(reason)
68+
69+
defp start_supervised_error({reason, info}) when is_tuple(info),
70+
do: Exception.format_exit(reason)
71+
72+
defp start_supervised_error(reason), do: Exception.format_exit({:start_spec, reason})
73+
5774
defp setup_framework_integration(:ex_unit, bypass = %{pid: pid}) do
5875
ExUnit.Callbacks.on_exit({Bypass, pid}, fn ->
5976
do_verify_expectations(bypass.pid, ExUnit.AssertionError)

test/bypass_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,4 +581,10 @@ defmodule BypassTest do
581581

582582
Mix.Config.persist(bypass: [test_framework: :ex_unit])
583583
end
584+
585+
test "Bypass.open/1 raises when cannot start child" do
586+
assert_raise RuntimeError, ~r/Failed to start bypass instance/, fn ->
587+
Bypass.open(:error)
588+
end
589+
end
584590
end

0 commit comments

Comments
 (0)