1 // Copyright (c) 2016 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
13 // TestDynamicBanScoreDecay tests the exponential decay implemented in
15 func TestDynamicBanScoreDecay(t *testing.T) {
16 var bs DynamicBanScore
19 r := bs.increase(100, 50, base)
21 t.Errorf("Unexpected result %d after ban score increase.", r)
24 r = bs.int(base.Add(time.Minute))
26 t.Errorf("Halflife check failed - %d instead of 125", r)
29 r = bs.int(base.Add(7 * time.Minute))
31 t.Errorf("Decay after 7m - %d instead of 100", r)
35 // TestDynamicBanScoreLifetime tests that DynamicBanScore properly yields zero
36 // once the maximum age is reached.
37 func TestDynamicBanScoreLifetime(t *testing.T) {
38 var bs DynamicBanScore
41 r := bs.increase(0, math.MaxUint32, base)
42 r = bs.int(base.Add(Lifetime * time.Second))
43 if r != 3 { // 3, not 4 due to precision loss and truncating 3.999...
44 t.Errorf("Pre max age check with MaxUint32 failed - %d", r)
46 r = bs.int(base.Add((Lifetime + 1) * time.Second))
48 t.Errorf("Zero after max age check failed - %d instead of 0", r)
52 // TestDynamicBanScore tests exported functions of DynamicBanScore. Exponential
53 // decay or other time based behavior is tested by other functions.
54 func TestDynamicBanScoreReset(t *testing.T) {
55 var bs DynamicBanScore
57 t.Errorf("Initial state is not zero.")
62 t.Errorf("Unexpected result %d after ban score increase.", r)
66 t.Errorf("Failed to reset ban score.")