Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for stream session ID in MainSocket.start_link #26

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Wait for stream session ID in MainSocket.start_link
  • Loading branch information
dsienkiewicz committed Jan 19, 2024
commit 95e2f66c2a51a210da7d13eb3037e8bdda81188d
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
Loading