github.com/klaytn/klaytn@v1.12.1/reward/staking_info_cache_test.go (about)

     1  // Copyright 2019 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package reward
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  // test cache limit of stakingInfoCache
    26  func TestStakingInfoCache_Add_Limit(t *testing.T) {
    27  	stakingInfoCache := newStakingInfoCache()
    28  
    29  	for i := 1; i <= 10; i++ {
    30  		testStakingInfo := newEmptyStakingInfo(uint64(i))
    31  		stakingInfoCache.add(testStakingInfo)
    32  
    33  		if len(stakingInfoCache.cells) > maxStakingCache {
    34  			t.Errorf("over the max limit of stakingCache. Current Len : %v, MaxStakingCache : %v", len(stakingInfoCache.cells), maxStakingCache)
    35  		}
    36  	}
    37  }
    38  
    39  func TestStakingInfoCache_Add_SameNumber(t *testing.T) {
    40  	stakingInfoCache := newStakingInfoCache()
    41  
    42  	testStakingInfo1 := newEmptyStakingInfo(uint64(1))
    43  	testStakingInfo2 := newEmptyStakingInfo(uint64(1))
    44  
    45  	stakingInfoCache.add(testStakingInfo1)
    46  	stakingInfoCache.add(testStakingInfo2)
    47  
    48  	assert.Equal(t, 1, len(stakingInfoCache.cells), "StakingInfo with Same block number is saved to the stakingCache")
    49  }
    50  
    51  func TestStakingInfoCache_Add_SmallNumber(t *testing.T) {
    52  	stakingInfoCache := newStakingInfoCache()
    53  
    54  	for i := uint64(10); i > 0; i-- {
    55  		testStakingInfo := newEmptyStakingInfo(i)
    56  		stakingInfoCache.add(testStakingInfo)
    57  		assert.Equal(t, i, stakingInfoCache.minBlockNum)
    58  	}
    59  }
    60  
    61  // stakingInfo with minBlockNum should be deleted if add more than limit
    62  func TestStakingInfoCache_Add_MinBlockNum(t *testing.T) {
    63  	stakingInfoCache := newStakingInfoCache()
    64  
    65  	for i := 1; i < 5; i++ {
    66  		testStakingInfo := newEmptyStakingInfo(uint64(i))
    67  		stakingInfoCache.add(testStakingInfo)
    68  	}
    69  
    70  	testStakingInfo := newEmptyStakingInfo(uint64(5))
    71  	stakingInfoCache.add(testStakingInfo) // blockNum 1 should be deleted
    72  	assert.Equal(t, uint64(2), stakingInfoCache.minBlockNum)
    73  
    74  	testStakingInfo = newEmptyStakingInfo(uint64(6))
    75  	stakingInfoCache.add(testStakingInfo) // blockNum 2 should be deleted
    76  	assert.Equal(t, uint64(3), stakingInfoCache.minBlockNum)
    77  }
    78  
    79  func TestStakingInfoCache_Add(t *testing.T) {
    80  	testCases := []struct {
    81  		blockNumber       uint64
    82  		expectedLen       int
    83  		expectedMinNumber uint64
    84  	}{
    85  		{1, 1, 1},
    86  		{5, 2, 1},
    87  		{10, 3, 1},
    88  		{7, 4, 1},
    89  		{15, 4, 5},
    90  		{30, 4, 7},
    91  		{20, 4, 10},
    92  		{3, 4, 3},
    93  	}
    94  	stakingInfoCache := newStakingInfoCache()
    95  	for i := 0; i < len(testCases); i++ {
    96  		testStakingInfo := newEmptyStakingInfo(testCases[i].blockNumber)
    97  		stakingInfoCache.add(testStakingInfo)
    98  		assert.Equal(t, testCases[i].expectedLen, len(stakingInfoCache.cells))
    99  		assert.Equal(t, testCases[i].expectedMinNumber, stakingInfoCache.minBlockNum)
   100  	}
   101  }
   102  
   103  func TestStakingInfoCache_Get(t *testing.T) {
   104  	stakingInfoCache := newStakingInfoCache()
   105  
   106  	for i := 1; i <= 4; i++ {
   107  		testStakingInfo := newEmptyStakingInfo(uint64(i))
   108  		stakingInfoCache.add(testStakingInfo)
   109  	}
   110  
   111  	// should find correct stakingInfo with a given block number
   112  	for i := uint64(1); i <= 4; i++ {
   113  		testStakingInfo := stakingInfoCache.get(i)
   114  		assert.Equal(t, i, testStakingInfo.BlockNum)
   115  	}
   116  
   117  	// nothing should be found as no matched block number is in the cache
   118  	for i := uint64(5); i < 10; i++ {
   119  		testStakingInfo := stakingInfoCache.get(i)
   120  		assert.Nil(t, testStakingInfo)
   121  	}
   122  }