package bounty import ( "net/netip" "testing" "time" ) func TestTracker(t *testing.T) { tr := New(3, time.Minute) p := netip.MustParsePrefix("203.0.113.9/32") t0 := time.Now() if n, r := tr.Add(p, t0); n != 1 || r { t.Errorf("first: %d %v", n, r) } // Two minutes later the first is out of the window. tr.Add(p, t0.Add(2*time.Minute)) if n, r := tr.Add(p, t0.Add(2*time.Minute+time.Second)); n != 2 || r { t.Errorf("window: %d %v", n, r) } if top := tr.Top(5, t0.Add(2*time.Minute+time.Second)); len(top) != 1 || top[0].Bounty != 2 || top[0].Address != "203.0.113.9" { t.Errorf("top %+v", top) } if n, r := tr.Add(p, t0.Add(2*time.Minute+2*time.Second)); n != 3 || !r { t.Errorf("reached: %d %v", n, r) } if top := tr.Top(5, t0.Add(3*time.Minute)); len(top) != 0 { t.Errorf("after the ban the bounty starts again: %+v", top) } }