Skip to content

Commit

Permalink
Wait for stream session ID in MainSocket.start_link (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsienkiewicz authored Jan 19, 2024
1 parent 73b8f10 commit 6fe11a3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
23 changes: 22 additions & 1 deletion lib/xtb_client/main_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,27 @@ defmodule XtbClient.MainSocket do
rate_limit: RateLimit.new(200)
}

WebSockex.start_link(url, __MODULE__, state, opts)
case WebSockex.start_link(url, __MODULE__, state, opts) do
{:ok, pid} = result ->
_ = poll_stream_session_id(pid)

result

other ->
other
end
end

defp poll_stream_session_id(server) do
case stream_session_id(server) do
{:ok, nil} ->
Process.sleep(10)

poll_stream_session_id(server)

{:ok, _session_id} = result ->
result
end
end

@impl WebSockex
Expand All @@ -136,6 +156,7 @@ defmodule XtbClient.MainSocket do

@impl WebSockex
def handle_disconnect(_connection_status_map, state) do
Logger.warning("Socket reconnecting")
{:reconnect, state}
end

Expand Down
11 changes: 0 additions & 11 deletions test/support/fixtures/main_socket_e2e_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,6 @@ defmodule XtbClient.MainSocket.E2EFixtures do
alias XtbClient.MainSocket
alias XtbClient.Messages.{Trades, TradeTransaction}

def poll_stream_session_id(server) do
case MainSocket.stream_session_id(server) do
{:ok, nil} ->
Process.sleep(100)
poll_stream_session_id(server)

{:ok, _session_id} = result ->
result
end
end

def open_trade(pid, buy_args) do
buy = TradeTransaction.Command.new(buy_args)
MainSocket.trade_transaction(pid, buy)
Expand Down
4 changes: 0 additions & 4 deletions test/xtb_client/main_socket_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ defmodule XtbClient.MainSocketTest do
setup :setup_main_socket

test "stream_session_id is present", %{pid: pid} do
# needed to wait for socket to connect
# during that time stream_session_id should be available
Process.sleep(100)

{:ok, stream_session_id} = MainSocket.stream_session_id(pid)
assert is_binary(stream_session_id)
end
Expand Down
3 changes: 1 addition & 2 deletions test/xtb_client/streaming_socket_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule XtbClient.StreamingSocketTest do
]

{:ok, pid} = start_supervised({MainSocket, params})
{:ok, stream_session_id} = poll_stream_session_id(pid)
{:ok, stream_session_id} = MainSocket.stream_session_id(pid)

{:ok,
%{
Expand Down Expand Up @@ -91,7 +91,6 @@ defmodule XtbClient.StreamingSocketTest do
describe "public API" do
setup context do
{:ok, pid} = start_supervised({StreamingSocket, context.params})

{:ok, _store} = start_supervised(StreamingTestStoreMock)

parent_pid = self()
Expand Down

0 comments on commit 6fe11a3

Please sign in to comment.