fix: Fix WebSocketClient reconnect (#9937)

pull/9977/head
Qwerty5Uiop 7 months ago committed by GitHub
parent 23419e7180
commit 604491b7ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/web_socket/web_socket_client.ex
  2. 8
      apps/ethereum_jsonrpc/test/ethereum_jsonrpc/web_socket/web_socket_client_test.exs

@ -14,6 +14,8 @@ defmodule EthereumJSONRPC.WebSocket.WebSocketClient do
@behaviour :websocket_client
@behaviour WebSocket
@reconnect_interval :timer.minutes(1)
@enforce_keys ~w(url)a
defstruct connected: false,
request_id_to_registration: %{},
@ -139,7 +141,7 @@ defmodule EthereumJSONRPC.WebSocket.WebSocketClient do
def ondisconnect(_reason, %__MODULE__{request_id_to_registration: request_id_to_registration} = state) do
final_state = Enum.reduce(request_id_to_registration, state, &disconnect_request_id_registration/2)
{:reconnect, %__MODULE__{final_state | connected: false}}
{:reconnect, @reconnect_interval, %__MODULE__{final_state | connected: false}}
end
@impl :websocket_client

@ -10,7 +10,7 @@ defmodule EthereumJSONRPC.WebSocket.WebSocketClientTest do
setup :example_state
test "reconnects", %{state: state} do
assert {:reconnect, _} = WebSocketClient.ondisconnect({:closed, :remote}, state)
assert {:reconnect, _, _} = WebSocketClient.ondisconnect({:closed, :remote}, state)
end
test "treats in-progress unsubscribes as successful", %{state: state} do
@ -23,7 +23,7 @@ defmodule EthereumJSONRPC.WebSocket.WebSocketClientTest do
state = put_registration(state, registration)
assert {_, disconnected_state} = WebSocketClient.ondisconnect({:closed, :remote}, state)
assert {_, _, disconnected_state} = WebSocketClient.ondisconnect({:closed, :remote}, state)
assert Enum.empty?(disconnected_state.request_id_to_registration)
assert Enum.empty?(disconnected_state.subscription_id_to_subscription_reference)
@ -36,7 +36,7 @@ defmodule EthereumJSONRPC.WebSocket.WebSocketClientTest do
test "keeps :json_rpc requests for re-requesting on reconnect", %{state: state} do
state = put_registration(state, %{type: :json_rpc, method: "eth_getBlockByNumber", params: [1, true]})
assert {_, disconnected_state} = WebSocketClient.ondisconnect({:closed, :remote}, state)
assert {_, _, disconnected_state} = WebSocketClient.ondisconnect({:closed, :remote}, state)
assert Enum.count(disconnected_state.request_id_to_registration) == 1
end
@ -44,7 +44,7 @@ defmodule EthereumJSONRPC.WebSocket.WebSocketClientTest do
test "keeps :subscribe requests for re-requesting on reconnect", %{state: state} do
state = put_registration(state, %{type: :subscribe})
assert {_, disconnected_state} = WebSocketClient.ondisconnect({:closed, :remote}, state)
assert {_, _, disconnected_state} = WebSocketClient.ondisconnect({:closed, :remote}, state)
assert Enum.count(disconnected_state.request_id_to_registration) == 1
end

Loading…
Cancel
Save