github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/p2p/trust/banscore_test.go (about)

     1  package trust
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestInt(t *testing.T) {
    10  	var banScoreIntTests = []struct {
    11  		bs        DynamicBanScore
    12  		timeLapse int64
    13  		wantValue uint32
    14  	}{
    15  		{bs: DynamicBanScore{lastUnix: 0, transient: 50, persistent: 50}, timeLapse: 1, wantValue: 99},
    16  		{bs: DynamicBanScore{lastUnix: 0, transient: 50, persistent: 50}, timeLapse: Lifetime, wantValue: 50},
    17  		{bs: DynamicBanScore{lastUnix: 0, transient: 50, persistent: 50}, timeLapse: Lifetime + 1, wantValue: 50},
    18  		{bs: DynamicBanScore{lastUnix: 0, transient: 50, persistent: 50}, timeLapse: -1, wantValue: 50},
    19  		{bs: DynamicBanScore{lastUnix: 0, transient: 0, persistent: 0}, timeLapse: Lifetime + 1, wantValue: 0},
    20  		{bs: DynamicBanScore{lastUnix: 0, transient: 0, persistent: math.MaxUint32}, timeLapse: 0, wantValue: math.MaxUint32},
    21  		{bs: DynamicBanScore{lastUnix: 0, transient: math.MaxUint32, persistent: 0}, timeLapse: Lifetime + 1, wantValue: 0},
    22  		{bs: DynamicBanScore{lastUnix: 0, transient: math.MaxUint32, persistent: 0}, timeLapse: 60, wantValue: math.MaxUint32 / 2},
    23  		{bs: DynamicBanScore{lastUnix: 0, transient: math.MaxUint32, persistent: math.MaxUint32}, timeLapse: 0, wantValue: math.MaxUint32 - 1},
    24  	}
    25  
    26  	Init()
    27  	for i, intTest := range banScoreIntTests {
    28  		rst := intTest.bs.int(time.Unix(intTest.timeLapse, 0))
    29  		if rst != intTest.wantValue {
    30  			t.Fatal("test ban score int err.", "num:", i, "want:", intTest.wantValue, "got:", rst)
    31  		}
    32  	}
    33  }
    34  
    35  func TestIncrease(t *testing.T) {
    36  	var banScoreIncreaseTests = []struct {
    37  		bs            DynamicBanScore
    38  		transientAdd  uint32
    39  		persistentAdd uint32
    40  		timeLapse     int64
    41  		wantValue     uint32
    42  	}{
    43  		{bs: DynamicBanScore{lastUnix: 0, transient: 50, persistent: 50}, transientAdd: 50, persistentAdd: 50, timeLapse: 1, wantValue: 199},
    44  		{bs: DynamicBanScore{lastUnix: 0, transient: 50, persistent: 50}, transientAdd: 50, persistentAdd: 50, timeLapse: Lifetime, wantValue: 150},
    45  		{bs: DynamicBanScore{lastUnix: 0, transient: 50, persistent: 50}, transientAdd: 50, persistentAdd: 50, timeLapse: Lifetime + 1, wantValue: 150},
    46  		{bs: DynamicBanScore{lastUnix: 0, transient: 50, persistent: 50}, transientAdd: 50, persistentAdd: 50, timeLapse: -1, wantValue: 200},
    47  		{bs: DynamicBanScore{lastUnix: 0, transient: 0, persistent: 0}, transientAdd: math.MaxUint32, persistentAdd: 0, timeLapse: 60, wantValue: math.MaxUint32},
    48  		{bs: DynamicBanScore{lastUnix: 0, transient: 0, persistent: 0}, transientAdd: 0, persistentAdd: math.MaxUint32, timeLapse: 60, wantValue: math.MaxUint32},
    49  		{bs: DynamicBanScore{lastUnix: 0, transient: 0, persistent: 0}, transientAdd: 0, persistentAdd: math.MaxUint32, timeLapse: Lifetime + 1, wantValue: math.MaxUint32},
    50  		{bs: DynamicBanScore{lastUnix: 0, transient: 0, persistent: 0}, transientAdd: math.MaxUint32, persistentAdd: 0, timeLapse: Lifetime + 1, wantValue: math.MaxUint32},
    51  		{bs: DynamicBanScore{lastUnix: 0, transient: math.MaxUint32, persistent: 0}, transientAdd: math.MaxUint32, persistentAdd: 0, timeLapse: Lifetime + 1, wantValue: math.MaxUint32},
    52  		{bs: DynamicBanScore{lastUnix: 0, transient: math.MaxUint32, persistent: 0}, transientAdd: math.MaxUint32, persistentAdd: 0, timeLapse: 0, wantValue: math.MaxUint32 - 1},
    53  		{bs: DynamicBanScore{lastUnix: 0, transient: 0, persistent: math.MaxUint32}, transientAdd: math.MaxUint32, persistentAdd: 0, timeLapse: Lifetime + 1, wantValue: math.MaxUint32 - 1},
    54  	}
    55  
    56  	Init()
    57  	for i, incTest := range banScoreIncreaseTests {
    58  		rst := incTest.bs.increase(incTest.persistentAdd, incTest.transientAdd, time.Unix(incTest.timeLapse, 0))
    59  		if rst != incTest.wantValue {
    60  			t.Fatal("test ban score int err.", "num:", i, "want:", incTest.wantValue, "got:", rst)
    61  		}
    62  	}
    63  }
    64  
    65  func TestReset(t *testing.T) {
    66  	var bs DynamicBanScore
    67  	if bs.Int() != 0 {
    68  		t.Errorf("Initial state is not zero.")
    69  	}
    70  	bs.Increase(100, 0)
    71  	r := bs.Int()
    72  	if r != 100 {
    73  		t.Errorf("Unexpected result %d after ban score increase.", r)
    74  	}
    75  	bs.Reset()
    76  	if bs.Int() != 0 {
    77  		t.Errorf("Failed to reset ban score.")
    78  	}
    79  }
    80  
    81  func TestString(t *testing.T) {
    82  	want := "persistent 100 + transient 0 at 0 = 100 as of now"
    83  	var bs DynamicBanScore
    84  	if bs.Int() != 0 {
    85  		t.Errorf("Initial state is not zero.")
    86  	}
    87  
    88  	bs.Increase(100, 0)
    89  	if bs.String() != want {
    90  		t.Fatal("DynamicBanScore String test error.")
    91  	}
    92  }