[#30] adding a full test for Pending Collector
This commit is contained in:
parent
70747216e6
commit
efe5a71d50
|
@ -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
|
|
@ -6,30 +6,9 @@ defmodule POAAgent.Plugins.Collectors.Eth.LatestBlockTest do
|
||||||
|
|
||||||
import Mock
|
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
|
test "sending history to the transfer when Collector starts" do
|
||||||
echo_transfer = :echo_transfer
|
echo_transfer = :echo_transfer
|
||||||
start_echo_transfer(echo_transfer)
|
{:ok, _echo} = EchoTransfer.start(echo_transfer)
|
||||||
|
|
||||||
args = %{
|
args = %{
|
||||||
name: :eth_latest_block,
|
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
|
test "sending latest block to the transfer after Collector start" do
|
||||||
echo_transfer = :echo_transfer
|
echo_transfer = :echo_transfer
|
||||||
start_echo_transfer(echo_transfer)
|
{:ok, _echo} = EchoTransfer.start(echo_transfer)
|
||||||
|
|
||||||
args = %{
|
args = %{
|
||||||
name: :eth_latest_block,
|
name: :eth_latest_block,
|
||||||
|
@ -74,11 +53,6 @@ defmodule POAAgent.Plugins.Collectors.Eth.LatestBlockTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp start_echo_transfer(name) do
|
|
||||||
{:ok, pid} = EchoTransfer.start_link(%{name: name, args: self()})
|
|
||||||
pid
|
|
||||||
end
|
|
||||||
|
|
||||||
defp ethereumex_block() do
|
defp ethereumex_block() do
|
||||||
%{"author" => "0xdf9c9701e434c5c9f755ef8af18d6a4336550206",
|
%{"author" => "0xdf9c9701e434c5c9f755ef8af18d6a4336550206",
|
||||||
"difficulty" => "0xfffffffffffffffffffffffffffffffd",
|
"difficulty" => "0xfffffffffffffffffffffffffffffffd",
|
||||||
|
|
|
@ -1,20 +1,51 @@
|
||||||
defmodule POAAgent.Plugins.Collectors.Eth.PendingTest do
|
defmodule POAAgent.Plugins.Collectors.Eth.PendingTest do
|
||||||
use ExUnit.Case
|
use ExUnit.Case
|
||||||
|
|
||||||
|
alias POAAgent.Plugins.Collectors.Eth.Pending
|
||||||
|
|
||||||
import Mock
|
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, [
|
with_mock Ethereumex.HttpClient, [
|
||||||
eth_get_block_transaction_count_by_number: fn(_) -> {:ok, "0x0"} end
|
eth_get_block_transaction_count_by_number: fn(_) -> {:ok, "0x0"} end
|
||||||
] do
|
] do
|
||||||
|
|
||||||
{:notransfer, _} = POAAgent.Plugins.Collectors.Eth.Pending.collect(%{last_pending: 0})
|
{:ok, _pid} = Pending.start_link(args)
|
||||||
|
|
||||||
|
refute_receive {:my_metrics, _}
|
||||||
end
|
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, [
|
with_mock Ethereumex.HttpClient, [
|
||||||
eth_get_block_transaction_count_by_number: fn(_) -> {:ok, "0x3"} end
|
eth_get_block_transaction_count_by_number: fn(_) -> {:ok, "0x3"} end
|
||||||
] do
|
] do
|
||||||
|
|
||||||
{:transfer, pending, _} = POAAgent.Plugins.Collectors.Eth.Pending.collect(%{last_pending: 0})
|
{:ok, _pid} = Pending.start_link(args)
|
||||||
assert pending == expected_pending()
|
|
||||||
|
assert_receive {:my_metrics, %POAAgent.Entity.Ethereum.Pending{pending: 3}}, 20_000
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue