diff --git a/test/ancillary/echo_transfer.ex b/test/ancillary/echo_transfer.ex new file mode 100644 index 0000000..e6046bc --- /dev/null +++ b/test/ancillary/echo_transfer.ex @@ -0,0 +1,24 @@ +defmodule EchoTransfer do + use POAAgent.Plugins.Transfer + + def start(name) do + EchoTransfer.start_link(%{name: name, args: self()}) + end + + def init_transfer(caller) do + {:ok, caller} + end + + def data_received(label, data, caller) do + send(caller, {label, data}) + {:ok, caller} + end + + def handle_message(_, state) do + {:ok, state} + end + + def terminate(_state) do + :ok + end +end \ No newline at end of file diff --git a/test/poa_agent/plugins/collectors/eth/latest_block_test.exs b/test/poa_agent/plugins/collectors/eth/latest_block_test.exs index 54f64ba..a9574dd 100644 --- a/test/poa_agent/plugins/collectors/eth/latest_block_test.exs +++ b/test/poa_agent/plugins/collectors/eth/latest_block_test.exs @@ -6,30 +6,9 @@ defmodule POAAgent.Plugins.Collectors.Eth.LatestBlockTest do import Mock - defmodule EchoTransfer do - use POAAgent.Plugins.Transfer - - def init_transfer(caller) do - {:ok, caller} - end - - def data_received(label, data, caller) do - send(caller, {label, data}) - {:ok, caller} - end - - def handle_message(_, state) do - {:ok, state} - end - - def terminate(_state) do - :ok - end - end - test "sending history to the transfer when Collector starts" do echo_transfer = :echo_transfer - start_echo_transfer(echo_transfer) + {:ok, _echo} = EchoTransfer.start(echo_transfer) args = %{ name: :eth_latest_block, @@ -52,7 +31,7 @@ defmodule POAAgent.Plugins.Collectors.Eth.LatestBlockTest do test "sending latest block to the transfer after Collector start" do echo_transfer = :echo_transfer - start_echo_transfer(echo_transfer) + {:ok, _echo} = EchoTransfer.start(echo_transfer) args = %{ name: :eth_latest_block, @@ -74,11 +53,6 @@ defmodule POAAgent.Plugins.Collectors.Eth.LatestBlockTest do end end - defp start_echo_transfer(name) do - {:ok, pid} = EchoTransfer.start_link(%{name: name, args: self()}) - pid - end - defp ethereumex_block() do %{"author" => "0xdf9c9701e434c5c9f755ef8af18d6a4336550206", "difficulty" => "0xfffffffffffffffffffffffffffffffd", diff --git a/test/poa_agent/plugins/collectors/eth/pending_test.exs b/test/poa_agent/plugins/collectors/eth/pending_test.exs index da6bca8..41337ec 100644 --- a/test/poa_agent/plugins/collectors/eth/pending_test.exs +++ b/test/poa_agent/plugins/collectors/eth/pending_test.exs @@ -1,20 +1,51 @@ defmodule POAAgent.Plugins.Collectors.Eth.PendingTest do use ExUnit.Case + + alias POAAgent.Plugins.Collectors.Eth.Pending + import Mock - test "pending data sent to the transfer" do + test "pending data is not sent if it is the same as last pending" do + echo_transfer = :echo_transfer + {:ok, _echo} = EchoTransfer.start(echo_transfer) + + args = %{ + name: :eth_pending, + transfers: [echo_transfer], + frequency: 500, + label: :my_metrics, + args: [url: "http://localhost:8545"] + } + with_mock Ethereumex.HttpClient, [ eth_get_block_transaction_count_by_number: fn(_) -> {:ok, "0x0"} end ] do - {:notransfer, _} = POAAgent.Plugins.Collectors.Eth.Pending.collect(%{last_pending: 0}) + {:ok, _pid} = Pending.start_link(args) + + refute_receive {:my_metrics, _} end + end + + test "pending data is sent if it is different than last pending" do + echo_transfer = :echo_transfer + {:ok, _echo} = EchoTransfer.start(echo_transfer) + + args = %{ + name: :eth_pending, + transfers: [echo_transfer], + frequency: 500, + label: :my_metrics, + args: [url: "http://localhost:8545"] + } + with_mock Ethereumex.HttpClient, [ eth_get_block_transaction_count_by_number: fn(_) -> {:ok, "0x3"} end ] do - {:transfer, pending, _} = POAAgent.Plugins.Collectors.Eth.Pending.collect(%{last_pending: 0}) - assert pending == expected_pending() + {:ok, _pid} = Pending.start_link(args) + + assert_receive {:my_metrics, %POAAgent.Entity.Ethereum.Pending{pending: 3}}, 20_000 end end