-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |