Merge pull request #34 from poanetwork/ferigis.30.coverage_stats_collector

[#30] improving test for Stats Collector
This commit is contained in:
Joseph Yiasemides 2018-05-18 09:11:30 +02:00 committed by GitHub
commit 4c2a40d8e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 82 additions and 4 deletions

View File

@ -1,8 +1,23 @@
defmodule POAAgent.Plugins.Collectors.Eth.StatsTest do
use ExUnit.Case
alias POAAgent.Plugins.Collectors.Eth.Stats
alias POAAgent.Entity.Ethereum.Statistics
import Mock
test "stats sent to the transfer" do
test "stats sent to the transfer when the collectors starts" do
echo_transfer = :echo_transfer
{:ok, _echo} = EchoTransfer.start(echo_transfer)
args = %{
name: :eth_pending,
transfers: [echo_transfer],
frequency: 1000,
label: :my_metrics,
args: [url: "http://localhost:8545"]
}
with_mock Ethereumex.HttpClient, [
net_peer_count: fn() -> {:ok, "0x3"} end,
eth_mining: fn() -> {:ok, false} end,
@ -10,9 +25,72 @@ defmodule POAAgent.Plugins.Collectors.Eth.StatsTest do
eth_syncing: fn() -> {:ok, syncing()} end,
eth_gas_price: fn() -> {:ok, "0x0"} end
] do
{:ok, _pid} = Stats.start_link(args)
expected_stats = expected_stats()
{:transfer, stats, _} = POAAgent.Plugins.Collectors.Eth.Stats.collect(%{last_stats: nil, tries: 0, down: 0})
assert stats == expected_stats()
# received only when starts
assert_receive {:my_metrics, ^expected_stats}, 20_000
refute_receive {:my_metrics, _}, 20_000
end
end
test "stats sent to the transfer when the last stats are different" do
echo_transfer = :echo_transfer
{:ok, _echo} = EchoTransfer.start(echo_transfer)
args = %{
name: :eth_pending,
transfers: [echo_transfer],
frequency: 1000,
label: :my_metrics,
args: [url: "http://localhost:8545"]
}
{:ok, _pid} = Stats.start_link(args)
with_mock Ethereumex.HttpClient, [
net_peer_count: fn() -> {:ok, "0x3"} end,
eth_mining: fn() -> {:ok, false} end,
eth_hashrate: fn() -> {:ok, "0x0"} end,
eth_syncing: fn() -> {:ok, syncing()} end,
eth_gas_price: fn() -> {:ok, "0x0"} end
] do
expected_stats = expected_stats()
assert_receive {:my_metrics, ^expected_stats}, 20_000
end
end
test "stats sent to the transfer when the node is not active" do
echo_transfer = :echo_transfer
{:ok, _echo} = EchoTransfer.start(echo_transfer)
args = %{
name: :eth_pending,
transfers: [echo_transfer],
frequency: 1000,
label: :my_metrics,
args: [url: "http://localhost:8545"]
}
with_mock Ethereumex.HttpClient, [
net_peer_count: fn() -> {:ok, "0x0"} end,
eth_mining: fn() -> {:ok, false} end,
eth_hashrate: fn() -> {:ok, "0x0"} end,
eth_syncing: fn() -> {:ok, syncing()} end,
eth_gas_price: fn() -> {:ok, "0x0"} end
] do
{:ok, _pid} = Stats.start_link(args)
expected_stats =
%Statistics{expected_stats() |
active?: false,
peers: 0,
uptime: 0.0
}
assert_receive {:my_metrics, ^expected_stats}, 20_000
end
end
@ -27,7 +105,7 @@ defmodule POAAgent.Plugins.Collectors.Eth.StatsTest do
end
def expected_stats() do
%POAAgent.Entity.Ethereum.Statistics{
%Statistics{
active?: true,
gas_price: 0,
hashrate: 0,