Skip to content

Commit

Permalink
Add mempool tests
Browse files Browse the repository at this point in the history
  • Loading branch information
caike committed Jun 14, 2024
1 parent ae20384 commit cc1fc63
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/xogmios/mempool.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ defmodule Xogmios.Mempool do

require Logger

@doc """
Invoked when a new snapshot is acquired.
Receives snapshot information as argument.
Returning `{:ok, :next_transaction, new_state}` will request the next transaction
once it's made available.
Returning `{:ok, new_state}` wil not request anymore transactions.
Returning `{:close, new_state}` will close the connection to the server.
"""
@callback handle_acquired(snapshop :: map(), state) ::
{:ok, :next_transaction, new_state}
| {:ok, new_state}
| {:close, new_state}
when state: term(), new_state: term()

@doc """
Invoked when a new transaction is made available in the mempool.
Expand All @@ -17,7 +35,7 @@ defmodule Xogmios.Mempool do
Returning `{:ok, new_state}` wil not request anymore transactions.
Returning `{:close, new_state}` will close the connection to the server
Returning `{:close, new_state}` will close the connection to the server.
"""
@callback handle_transaction(transaction :: map(), state) ::
{:ok, :next_transaction, new_state}
Expand Down Expand Up @@ -175,7 +193,7 @@ defmodule Xogmios.Mempool do

def handle_connect(state), do: {:ok, state}
def handle_disconnect(_reason, state), do: {:ok, state}
def handle_acquired(_slot, state), do: {:ok, :next_transaction, state}
def handle_acquired(_snapshot, state), do: {:ok, :next_transaction, state}
defoverridable handle_connect: 1, handle_disconnect: 2, handle_acquired: 2

def handle_message(
Expand Down
47 changes: 47 additions & 0 deletions test/mempool_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule Xogmios.MempoolTest do
use ExUnit.Case

@ws_url TestServer.get_url()

setup_all do
{:ok, _server} = TestServer.start(handler: Mempool.TestHandler)

on_exit(fn ->
TestServer.shutdown()
end)

:ok
end

defmodule DummyClient do
use Xogmios, :mempool

def start_link(opts) do
Xogmios.start_mempool_link(__MODULE__, opts)
end

@impl true
def handle_acquired(_snapshot, state) do
send(state.test_handler, :handle_acquired)
{:ok, :next_transaction, state}
end

@impl true
def handle_transaction(_block, state) do
send(state.test_handler, :handle_transaction)
{:close, state}
end
end

test "receives callbacks and closes connection" do
pid = start_supervised!({DummyClient, url: @ws_url, test_handler: self()})
assert is_pid(pid)

assert_receive :handle_acquired
assert_receive :handle_transaction

Process.sleep(500)
refute Process.alive?(pid)
assert GenServer.whereis(DummyClient) == nil
end
end
54 changes: 54 additions & 0 deletions test/support/mempool/test_handler.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
defmodule Mempool.TestHandler do
@moduledoc false

@behaviour :cowboy_websocket

@impl true
def init(request, state) do
{:cowboy_websocket, request, state}
end

@impl true
def websocket_init(state) do
{:ok, state}
end

@impl true
def websocket_handle({:text, payload}, state) do
case Jason.decode(payload) do
{:ok, %{"method" => "acquireMempool"}} ->
payload =
Jason.encode!(%{
"method" => "acquireMempool",
"result" => %{"acquired" => "mempool", "slot" => 123}
})

{:reply, {:text, payload}, state}

{:ok, %{"method" => "nextTransaction"}} ->
payload =
Jason.encode!(%{
"method" => "nextTransaction",
"result" => %{
"transaction" => %{"id" => 456}
}
})

{:reply, {:text, payload}, state}
end
end

@impl true
def terminate(_arg1, _arg2, _arg3) do
:ok
end

def websocket_info(:stop, state) do
{:stop, state}
end

@impl true
def websocket_info(info, state) do
{:reply, {:text, info}, state}
end
end

0 comments on commit cc1fc63

Please sign in to comment.