我有一个启动套接字连接并订阅和加入主题的测试。然后我将消息发送到通道,该通道执行一些工作并保留一些数据。然后我在套接字上使用ChannelTest.close / 2来模拟关闭连接的客户端。然后,我想修改被操纵的数据并创建另一个套接字连接来测试某些行为。
我的问题是ChannelTest.close / 2导致我的测试失败并返回** (EXIT from #PID<0.558.0>) shutdown: :closed
。我怎样才能防止这种情况发生?
这是一些相关的代码
defmodule PlexServer.EmpireChannelTest do
use PlexServer.ChannelCase
alias PlexServer.EmpireChannel
alias PlexServer.EmpireTemplate
alias PlexServer.EmpireInstance
setup do
...
{:ok, _, socket} =
socket("user_id", %{})
|> subscribe_and_join(EmpireChannel,
"empire:#{empire.id}",
%{"guardian_token" => jwt})
{:ok, socket: socket, empire: empire, jwt: jwt}
end
test "research infrastructure", %{socket: socket, empire: empire, jwt: jwt} do
...
ref = push socket, "command", request
assert_reply ref, :ok, _, 1000
close(socket) #fails here
...
end
end
defmodule PlexServer.UserSocket do
use Phoenix.Socket
use Guardian.Phoenix.Socket
## Channels
# channel "rooms:*", PlexServer.RoomChannel
channel "empire:*", PlexServer.EmpireChannel
channel "user:*", PlexServer.UserChannel
## Transports
transport :websocket, Phoenix.Transports.WebSocket
def connect(_params,_) do
:error
end
def id(_socket), do: nil
end
defmodule PlexServer.EmpireChannel do
use PlexServer.Web, :channel
use Guardian.Channel
alias PlexServer.EmpireInstance
alias PlexServer.EmpireServer
...
def join("empire:" <> empire_id, %{claims: _claims, resource: user}, socket) do
...
#Starts a process through a supervisor here
end
def join("empire:" <> _, _, _socket) do
{:error, data_error(:auth, "not authorized, did you pass your jwt?")}
end
# Channels can be used in a request/response fashion
# by sending replies to requests from the client
def handle_in("ping", payload, socket) do
{:reply, {:ok, payload}, socket}
end
def handle_in("show", _, socket) do
...
end
def handle_in("command", request, socket) do
...
end
def handle_in("remove", _, socket) do
...
end
def handle_in("ship_templates", _, socket) do
...
end
end
根据评论中的链接,我添加了一些代码:
Process.flag(:trap_exit, true)
monitor_ref = Process.monitor(socket.channel_pid)
close(socket)
现在我收到一条不同的错误消息:** (exit) exited in: GenServer.call(PlexServer.EmpireSupervisor, {:terminate_child, :empire4}, :infinity)
** (EXIT) no process