Skip to content

Commit

Permalink
Add stream terminate event
Browse files Browse the repository at this point in the history
- Send from Deribit connection
  • Loading branch information
rupurt committed Mar 1, 2020
1 parent acd8030 commit 0586442
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
22 changes: 22 additions & 0 deletions apps/tai/lib/tai/events/stream_terminate.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule Tai.Events.StreamTerminate do
alias __MODULE__

@type venue :: Tai.Venue.id()
@type t :: %StreamTerminate{venue: venue, reason: term}

@enforce_keys ~w(venue reason)a
defstruct ~w(venue reason)a
end

defimpl TaiEvents.LogEvent, for: Tai.Events.StreamTerminate do
def to_data(event) do
keys =
event
|> Map.keys()
|> Enum.filter(&(&1 != :__struct__))

event
|> Map.take(keys)
|> Map.put(:reason, event.reason |> inspect)
end
end
4 changes: 4 additions & 0 deletions apps/tai/lib/tai/venue_adapters/deribit/stream/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ defmodule Tai.VenueAdapters.Deribit.Stream.Connection do
@spec to_name(venue) :: atom
def to_name(venue), do: :"#{__MODULE__}_#{venue}"

def terminate(close_reason, state) do
TaiEvents.error(%Tai.Events.StreamTerminate{venue: state.venue, reason: close_reason})
end

def handle_connect(_conn, state) do
TaiEvents.info(%Tai.Events.StreamConnect{venue: state.venue})
send(self(), :init_subscriptions)
Expand Down
16 changes: 16 additions & 0 deletions apps/tai/test/tai/events/stream_terminate_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Tai.Events.StreamTerminateTest do
use ExUnit.Case, async: true

@base_attrs %{
venue: :venue_a,
reason: {:remote, :normal}
}

test ".to_data/1 transforms reason to a string" do
event = struct!(Tai.Events.StreamTerminate, @base_attrs)

assert %{} = json = TaiEvents.LogEvent.to_data(event)
assert json.venue == :venue_a
assert json.reason == "{:remote, :normal}"
end
end

0 comments on commit 0586442

Please sign in to comment.