Skip to content

Commit 6158785

Browse files
committed
Improve comments
Signed-off-by: Mario Uher <[email protected]>
1 parent a07de1b commit 6158785

File tree

5 files changed

+32
-40
lines changed

5 files changed

+32
-40
lines changed

lib/bypass.ex

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
defmodule Bypass do
22
@moduledoc """
3-
Bypass provides a quick way to create a custom Plug that can be put
4-
in place instead of an actual HTTP server to return prebaked responses
5-
to client requests.
3+
Bypass provides a quick way to create a custom Plug that can be put in place
4+
instead of an actual HTTP server to return prebaked responses to client
5+
requests.
66
77
This module is the main interface to the library.
88
"""
99

1010
defstruct pid: nil, port: nil
1111

1212
@typedoc """
13-
Represents a Bypass server process
13+
Represents a Bypass server process.
1414
"""
1515
@type t :: %__MODULE__{pid: pid, port: non_neg_integer}
1616

1717
import Bypass.Utils
1818
require Logger
1919

2020
@doc """
21-
Starts an Elixir process running a minimal Plug app. The process
22-
is a HTTP handler and listens to requests on a TCP port on localhost.
21+
Starts an Elixir process running a minimal Plug app. The process is a HTTP
22+
handler and listens to requests on a TCP port on localhost.
2323
24-
Use the other functions in this module to declare which requests are
25-
handled and set expectations on the calls.
24+
Use the other functions in this module to declare which requests are handled
25+
and set expectations on the calls.
2626
"""
2727
def open(opts \\ []) do
2828
case DynamicSupervisor.start_child(Bypass.Supervisor, Bypass.Instance.child_spec(opts)) do
@@ -38,21 +38,18 @@ defmodule Bypass do
3838
end
3939
end
4040

41-
# Raise an error if called with an unknown framework
42-
#
4341
defp setup_framework_integration(:ex_unit, bypass = %{pid: pid}) do
4442
ExUnit.Callbacks.on_exit({Bypass, pid}, fn ->
4543
do_verify_expectations(bypass.pid, ExUnit.AssertionError)
4644
end)
4745
end
4846

4947
defp setup_framework_integration(:espec, _bypass) do
50-
# Entry point for more advanced ESpec configurations
5148
end
5249

5350
@doc """
54-
Can be called to immediately verify if the declared request
55-
expectations have been met.
51+
Can be called to immediately verify if the declared request expectations have
52+
been met.
5653
5754
Returns `:ok` on success and raises an error on failure.
5855
"""
@@ -104,16 +101,15 @@ defmodule Bypass do
104101
end
105102

106103
@doc """
107-
Re-opens the TCP socket on the same port.
108-
Blocks until the operation is complete.
104+
Re-opens the TCP socket on the same port. Blocks until the operation is
105+
complete.
109106
"""
110107
@spec up(Bypass.t()) :: :ok | {:error, :already_up}
111108
def up(%Bypass{pid: pid}),
112109
do: Bypass.Instance.call(pid, :up)
113110

114111
@doc """
115-
Closes the TCP socket.
116-
Blocks until the operation is complete.
112+
Closes the TCP socket. Blocks until the operation is complete.
117113
"""
118114
@spec down(Bypass.t()) :: :ok | {:error, :already_down}
119115
def down(%Bypass{pid: pid}),

lib/bypass/application.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
defmodule Bypass.Application do
2+
@moduledoc false
3+
24
use Application
35

46
def start(_type, _args) do

lib/bypass/instance.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
defmodule Bypass.Instance do
2+
@moduledoc false
3+
24
use GenServer, restart: :transient
35

46
import Bypass.Utils

lib/bypass/plug.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
defmodule Bypass.Plug do
2+
@moduledoc false
3+
24
def init([pid]), do: pid
35

46
def call(%{method: method, request_path: request_path} = conn, pid) do

test/bypass_test.exs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ defmodule BypassTest do
3737
defp specify_port(port, expect_fun) do
3838
bypass = Bypass.open(port: port)
3939

40-
# one of Bypass.expect or Bypass.expect_once
4140
apply(Bypass, expect_fun, [
4241
bypass,
4342
fn conn ->
@@ -62,7 +61,6 @@ defmodule BypassTest do
6261
defp down_socket(expect_fun) do
6362
bypass = Bypass.open()
6463

65-
# one of Bypass.expect or Bypass.expect_once
6664
apply(Bypass, expect_fun, [
6765
bypass,
6866
fn conn -> Plug.Conn.send_resp(conn, 200, "") end
@@ -101,13 +99,12 @@ defmodule BypassTest do
10199
defp not_called(expect_fun) do
102100
bypass = Bypass.open()
103101

104-
# one of Bypass.expect or Bypass.expect_once
105102
apply(Bypass, expect_fun, [
106103
bypass,
107104
fn _conn -> assert false end
108105
])
109106

110-
# Override Bypass' on_exit handler
107+
# Override Bypass' on_exit handler.
111108
ExUnit.Callbacks.on_exit({Bypass, bypass.pid}, fn ->
112109
exit_result = Bypass.Instance.call(bypass.pid, :on_exit)
113110
assert {:error, :not_called, {:any, :any}} = exit_result
@@ -125,7 +122,6 @@ defmodule BypassTest do
125122
defp pass(expect_fun) do
126123
bypass = Bypass.open()
127124

128-
# one of Bypass.expect or Bypass.expect_once
129125
apply(Bypass, expect_fun, [
130126
bypass,
131127
fn _conn ->
@@ -152,11 +148,10 @@ defmodule BypassTest do
152148
defp closing_in_flight(expect_fun) do
153149
bypass = Bypass.open()
154150

155-
# one of Bypass.expect or Bypass.expect_once
156151
apply(Bypass, expect_fun, [
157152
bypass,
158153
fn _conn ->
159-
# mark the request as arrived, since we're shutting it down now
154+
# Mark the request as arrived, since we're shutting it down now.
160155
Bypass.pass(bypass)
161156
Bypass.down(bypass)
162157
end
@@ -179,7 +174,6 @@ defmodule BypassTest do
179174
ref = make_ref()
180175
bypass = Bypass.open()
181176

182-
# one of Bypass.expect or Bypass.expect_once
183177
apply(Bypass, expect_fun, [
184178
bypass,
185179
fn conn ->
@@ -192,8 +186,8 @@ defmodule BypassTest do
192186

193187
assert {:ok, 200, ""} = request(bypass.port)
194188

195-
# Here we make sure that Bypass.down waits until the plug process finishes its work
196-
# before shutting down
189+
# Here we make sure that Bypass.down waits until the plug process finishes
190+
# its work before shutting down.
197191
refute_received ^ref
198192
Bypass.down(bypass)
199193
assert_received ^ref
@@ -236,8 +230,8 @@ defmodule BypassTest do
236230
end)
237231
end)
238232

239-
# Here we make sure that Bypass.down waits until the plug process finishes its work
240-
# before shutting down
233+
# Here we make sure that Bypass.down waits until the plug process finishes
234+
# its work before shutting down.
241235
refute_received ^ref
242236
:timer.sleep(200)
243237
Bypass.down(bypass)
@@ -256,7 +250,7 @@ defmodule BypassTest do
256250
assert {:ok, 500, ""} = request(bypass.port)
257251
end)
258252

259-
# Override Bypass' on_exit handler
253+
# Override Bypass' on_exit handler.
260254
ExUnit.Callbacks.on_exit({Bypass, bypass.pid}, fn ->
261255
exit_result = Bypass.Instance.call(bypass.pid, :on_exit)
262256
assert {:error, :unexpected_request, {:any, :any}} = exit_result
@@ -282,7 +276,7 @@ defmodule BypassTest do
282276
assert_receive :request_received
283277
end)
284278

285-
# Override Bypass' on_exit handler
279+
# Override Bypass' on_exit handler.
286280
ExUnit.Callbacks.on_exit({Bypass, bypass.pid}, fn ->
287281
:ok == Bypass.Instance.call(bypass.pid, :on_exit)
288282
end)
@@ -303,7 +297,7 @@ defmodule BypassTest do
303297
assert_receive :request_received
304298
refute_receive :request_received
305299

306-
# Override Bypass' on_exit handler
300+
# Override Bypass' on_exit handler.
307301
ExUnit.Callbacks.on_exit({Bypass, bypass.pid}, fn ->
308302
exit_result = Bypass.Instance.call(bypass.pid, :on_exit)
309303
assert {:error, :too_many_requests, {:any, :any}} = exit_result
@@ -347,7 +341,6 @@ defmodule BypassTest do
347341
method = "POST"
348342
path = "/this"
349343

350-
# one of Bypass.expect or Bypass.expect_once
351344
apply(Bypass, expect_fun, [
352345
bypass,
353346
method,
@@ -382,7 +375,6 @@ defmodule BypassTest do
382375
pattern = "/this/:resource/get/:id"
383376
path = "/this/my_resource/get/1234"
384377

385-
# one of Bypass.expect or Bypass.expect_once
386378
apply(Bypass, expect_fun, [
387379
bypass,
388380
method,
@@ -421,7 +413,6 @@ defmodule BypassTest do
421413
paths = ["/this", "/that"]
422414

423415
Enum.each(paths, fn path ->
424-
# one of Bypass.expect or Bypass.expect_once
425416
apply(Bypass, expect_fun, [
426417
bypass,
427418
method,
@@ -448,7 +439,7 @@ defmodule BypassTest do
448439
@doc ~S"""
449440
Open a new HTTP connection and perform the request. We don't want to use httpc, hackney or another
450441
"high-level" HTTP client, since they do connection pooling and we will sometimes get a connection
451-
closed error and not a failed to connect error, when we test Bypass.down
442+
closed error and not a failed to connect error, when we test Bypass.down.
452443
"""
453444
def request(port, path \\ "/example_path", method \\ "POST") do
454445
with {:ok, conn} <- Mint.HTTP.connect(:http, "127.0.0.1", port),
@@ -490,11 +481,11 @@ defmodule BypassTest do
490481
end
491482
end
492483

493-
test "Bypass.expect/4 can be used to define a specific route and then redefined" do
484+
test "Bypass.expect/4 can be used to define a specific route and then redefine it later" do
494485
:expect |> specific_route_redefined
495486
end
496487

497-
test "Bypass.expect_once/4 can be used to define a specific route and then redefined" do
488+
test "Bypass.expect_once/4 can be used to define a specific route and then redefine it later" do
498489
:expect_once |> specific_route_redefined
499490
end
500491

@@ -503,7 +494,6 @@ defmodule BypassTest do
503494
method = "POST"
504495
path = "/this"
505496

506-
# one of Bypass.expect or Bypass.expect_once
507497
apply(Bypass, expect_fun, [
508498
bypass,
509499
method,
@@ -519,7 +509,7 @@ defmodule BypassTest do
519509
assert {:ok, 200, ""} = request(bypass.port, path)
520510
end)
521511

522-
# redefining the expect
512+
# Redfine the expect
523513
apply(Bypass, expect_fun, [
524514
bypass,
525515
method,

0 commit comments

Comments
 (0)