|
|
@ -1,27 +1,8 @@ |
|
|
|
defmodule EthereumJSONRPC.RollingWindowTest do |
|
|
|
defmodule EthereumJSONRPC.RollingWindowTest do |
|
|
|
use ExUnit.Case, |
|
|
|
use ExUnit.Case, async: true |
|
|
|
# The same named process is used for all tests and they use the same key in the table, so they would interfere |
|
|
|
|
|
|
|
async: false |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
alias EthereumJSONRPC.RollingWindow |
|
|
|
alias EthereumJSONRPC.RollingWindow |
|
|
|
|
|
|
|
|
|
|
|
@table :table |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setup do |
|
|
|
|
|
|
|
# We set `window_length` to a large time frame so that we can sweep manually to simulate |
|
|
|
|
|
|
|
# time passing |
|
|
|
|
|
|
|
{:ok, pid} = |
|
|
|
|
|
|
|
RollingWindow.start_link([table: @table, duration: :timer.minutes(120), window_count: 3], name: RollingWindow) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
on_exit(fn -> Process.exit(pid, :normal) end) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:ok |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defp sweep do |
|
|
|
|
|
|
|
GenServer.call(RollingWindow, :sweep) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test "start_link/2" do |
|
|
|
test "start_link/2" do |
|
|
|
assert {:ok, _} = RollingWindow.start_link(table: :test_table, duration: 5, window_count: 1) |
|
|
|
assert {:ok, _} = RollingWindow.start_link(table: :test_table, duration: 5, window_count: 1) |
|
|
|
end |
|
|
|
end |
|
|
@ -40,61 +21,85 @@ defmodule EthereumJSONRPC.RollingWindowTest do |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
test "when no increments have happened, inspect returns an empty list" do |
|
|
|
test "when no increments have happened, inspect returns an empty list" do |
|
|
|
assert RollingWindow.inspect(@table, :foobar) == [] |
|
|
|
table = :no_increments_have_happened |
|
|
|
|
|
|
|
start_rolling_window(table) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert RollingWindow.inspect(table, :foobar) == [] |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
test "when no increments have happened, count returns 0" do |
|
|
|
test "when no increments have happened, count returns 0" do |
|
|
|
assert RollingWindow.count(@table, :foobar) == 0 |
|
|
|
table = :no_increments_have_happened_empty_list |
|
|
|
|
|
|
|
start_rolling_window(table) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert RollingWindow.count(table, :foobar) == 0 |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
test "when an increment has happened, inspect returns the count for that window" do |
|
|
|
test "when an increment has happened, inspect returns the count for that window" do |
|
|
|
RollingWindow.inc(@table, :foobar) |
|
|
|
table = :no_increments_have_happened_count |
|
|
|
|
|
|
|
start_rolling_window(table) |
|
|
|
|
|
|
|
|
|
|
|
assert RollingWindow.inspect(@table, :foobar) == [1] |
|
|
|
RollingWindow.inc(table, :foobar) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert RollingWindow.inspect(table, :foobar) == [1] |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
test "when an increment has happened, count returns the count for that window" do |
|
|
|
test "when an increment has happened, count returns the count for that window" do |
|
|
|
RollingWindow.inc(@table, :foobar) |
|
|
|
table = :no_increments_have_happened_count1 |
|
|
|
|
|
|
|
start_rolling_window(table) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RollingWindow.inc(table, :foobar) |
|
|
|
|
|
|
|
|
|
|
|
assert RollingWindow.count(@table, :foobar) == 1 |
|
|
|
assert RollingWindow.count(table, :foobar) == 1 |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
test "when an increment has happened in multiple windows, inspect returns the count for both windows" do |
|
|
|
test "when an increment has happened in multiple windows, inspect returns the count for both windows" do |
|
|
|
RollingWindow.inc(@table, :foobar) |
|
|
|
table = :no_increments_have_happened_multiple_windows |
|
|
|
sweep() |
|
|
|
start_rolling_window(table) |
|
|
|
RollingWindow.inc(@table, :foobar) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert RollingWindow.inspect(@table, :foobar) == [1, 1] |
|
|
|
RollingWindow.inc(table, :foobar) |
|
|
|
|
|
|
|
sweep(table) |
|
|
|
|
|
|
|
RollingWindow.inc(table, :foobar) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert RollingWindow.inspect(table, :foobar) == [1, 1] |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
test "when an increment has happened in multiple windows, count returns the sum of both windows" do |
|
|
|
test "when an increment has happened in multiple windows, count returns the sum of both windows" do |
|
|
|
RollingWindow.inc(@table, :foobar) |
|
|
|
table = :no_increments_have_happened_multiple_windows1 |
|
|
|
sweep() |
|
|
|
start_rolling_window(table) |
|
|
|
RollingWindow.inc(@table, :foobar) |
|
|
|
|
|
|
|
|
|
|
|
RollingWindow.inc(table, :foobar) |
|
|
|
|
|
|
|
sweep(table) |
|
|
|
|
|
|
|
RollingWindow.inc(table, :foobar) |
|
|
|
|
|
|
|
|
|
|
|
assert RollingWindow.count(@table, :foobar) == 2 |
|
|
|
assert RollingWindow.count(table, :foobar) == 2 |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
test "when an increment has happened, but has been swept <window_count> times, it no longer appears in inspect" do |
|
|
|
test "when an increment has happened, but has been swept <window_count> times, it no longer appears in inspect" do |
|
|
|
RollingWindow.inc(@table, :foobar) |
|
|
|
table = :no_increments_have_happened_multiple_windows3 |
|
|
|
sweep() |
|
|
|
start_rolling_window(table) |
|
|
|
sweep() |
|
|
|
|
|
|
|
RollingWindow.inc(@table, :foobar) |
|
|
|
RollingWindow.inc(table, :foobar) |
|
|
|
sweep() |
|
|
|
sweep(table) |
|
|
|
RollingWindow.inc(@table, :foobar) |
|
|
|
sweep(table) |
|
|
|
|
|
|
|
RollingWindow.inc(table, :foobar) |
|
|
|
assert RollingWindow.inspect(@table, :foobar) == [1, 1, 0] |
|
|
|
sweep(table) |
|
|
|
|
|
|
|
RollingWindow.inc(table, :foobar) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert RollingWindow.inspect(table, :foobar) == [1, 1, 0] |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
test "when an increment has happened, but has been swept <window_count> times, it no longer is included in count" do |
|
|
|
test "when an increment has happened, but has been swept <window_count> times, it no longer is included in count" do |
|
|
|
RollingWindow.inc(@table, :foobar) |
|
|
|
table = :no_increments_have_happened_multiple_windows4 |
|
|
|
sweep() |
|
|
|
start_rolling_window(table) |
|
|
|
sweep() |
|
|
|
|
|
|
|
RollingWindow.inc(@table, :foobar) |
|
|
|
RollingWindow.inc(table, :foobar) |
|
|
|
sweep() |
|
|
|
sweep(table) |
|
|
|
RollingWindow.inc(@table, :foobar) |
|
|
|
sweep(table) |
|
|
|
|
|
|
|
RollingWindow.inc(table, :foobar) |
|
|
|
assert RollingWindow.count(@table, :foobar) == 2 |
|
|
|
sweep(table) |
|
|
|
|
|
|
|
RollingWindow.inc(table, :foobar) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert RollingWindow.count(table, :foobar) == 2 |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
test "sweeping schedules another sweep" do |
|
|
|
test "sweeping schedules another sweep" do |
|
|
@ -102,4 +107,13 @@ defmodule EthereumJSONRPC.RollingWindowTest do |
|
|
|
RollingWindow.handle_info(:sweep, state) |
|
|
|
RollingWindow.handle_info(:sweep, state) |
|
|
|
assert_receive(:sweep) |
|
|
|
assert_receive(:sweep) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defp start_rolling_window(table_name) do |
|
|
|
|
|
|
|
{:ok, _pid} = |
|
|
|
|
|
|
|
RollingWindow.start_link([table: table_name, duration: :timer.minutes(120), window_count: 3], name: table_name) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defp sweep(name) do |
|
|
|
|
|
|
|
GenServer.call(name, :sweep) |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|