-
Notifications
You must be signed in to change notification settings - Fork 108
/
instance.ex
153 lines (130 loc) · 4.41 KB
/
instance.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
defmodule Bypass.Instance do
use GenServer
import Bypass.Utils
def start_link do
case GenServer.start_link(__MODULE__, [self()]) do
{:ok, pid} ->
port = receive do
{:bypass_port, ^pid, port} -> port
end
{:ok, pid, port}
{:error, _} = err -> err
end
end
def call(pid, request) do
debug_log "call(#{inspect pid}, #{inspect request})"
result = GenServer.call(pid, request, :infinity)
debug_log "#{inspect pid} -> #{inspect result}"
result
end
def cast(pid, request) do
GenServer.cast(pid, request)
end
# GenServer callbacks
def init([parent]) do
debug_log "init([#{inspect parent}])"
# Get a free port from the OS
{:ok, socket} = :ranch_tcp.listen(port: 0)
{:ok, port} = :inet.port(socket)
:erlang.port_close(socket)
ref = make_ref()
socket = do_up(port, ref)
state = %{
expect_fun: nil,
port: port,
ref: ref,
request_result: :ok,
socket: socket,
retained_plug: nil,
caller_awaiting_down: nil,
}
send(parent, {:bypass_port, self(), port})
{:ok, state}
end
def handle_call(request, from, state) do
debug_log [inspect(self()), " called ", inspect(request), " with state ", inspect(state)]
do_handle_call(request, from, state)
end
def handle_cast({:retain_plug_process, caller_pid}, state) do
debug_log [
inspect(self()), " retain_plug_process ", inspect(caller_pid),
", retained_plug: ", inspect(state.retained_plug)
]
{:noreply, Map.update!(state, :retained_plug, fn nil -> caller_pid end)}
end
defp do_handle_call(:up, _from, %{port: port, ref: ref, socket: nil} = state) do
socket = do_up(port, ref)
{:reply, :ok, %{state | socket: socket}}
end
defp do_handle_call(:up, _from, state) do
{:reply, {:error, :already_up}, state}
end
defp do_handle_call(:down, _from, %{socket: nil} = state) do
{:reply, {:error, :already_down}, state}
end
defp do_handle_call(:down, from, %{socket: socket, ref: ref} = state) when not is_nil(socket) do
nil = state.caller_awaiting_down # assertion
if state.retained_plug != nil do
# wait for the plug to finish
{:noreply, %{state | caller_awaiting_down: from}}
else
do_down(ref, socket)
{:reply, :ok, %{state | socket: nil}}
end
end
defp do_handle_call({:expect, nil}, _from, state) do
{:reply, :ok, %{state | expect_fun: nil, request_result: :ok}}
end
defp do_handle_call({:expect, fun}, _from, state) do
{:reply, :ok, %{state | expect_fun: fun, request_result: {:error, :not_called}}}
end
defp do_handle_call(:get_expect_fun, _from, %{expect_fun: expect_fun} = state) do
{:reply, expect_fun, state}
end
defp do_handle_call({:put_expect_result, result}, _from, state) do
updated_state =
%{state | request_result: result}
|> Map.put(:retained_plug, nil)
|> dispatch_awaiting_caller()
{:reply, :ok, updated_state}
end
defp do_handle_call(:on_exit, _from, state) do
updated_state =
case state do
%{socket: nil} -> state
%{socket: socket, ref: ref} ->
do_down(ref, socket)
%{state | socket: nil}
end
{:stop, :normal, state.request_result, updated_state}
end
defp do_up(port, ref) do
plug_opts = [self()]
{:ok, socket} = :ranch_tcp.listen(port: port)
cowboy_opts = [ref: ref, acceptors: 5, port: port, socket: socket]
{:ok, _pid} = Plug.Adapters.Cowboy.http(Bypass.Plug, plug_opts, cowboy_opts)
socket
end
defp do_down(ref, socket) do
:ok = Plug.Adapters.Cowboy.shutdown(ref)
# `port_close` is synchronous, so after it has returned we _know_ that the socket has been
# closed. If we'd rely on ranch's supervisor shutting down the acceptor processes and thereby
# killing the socket we would run into race conditions where the socket port hasn't yet gotten
# the EXIT signal and would still be open, thereby breaking tests that rely on a closed socket.
case :erlang.port_info(socket, :name) do
:undefined -> :ok
_ -> :erlang.port_close(socket)
end
end
defp dispatch_awaiting_caller(
%{retained_plug: retained_plug, caller_awaiting_down: caller, socket: socket, ref: ref} = state)
do
if retained_plug == nil and caller != nil do
do_down(ref, socket)
GenServer.reply(caller, :ok)
%{state | socket: nil, caller_awaiting_down: nil}
else
state
end
end
end