Skip to content

Commit

Permalink
Fix unstable behaviour in TestContractValidatorStore_CacheChange (0xP…
Browse files Browse the repository at this point in the history
…olygon#810)

* Fix unstable behaviour in TestContractValidatorStore_CacheChange

* Fix lint error
  • Loading branch information
Kourin1996 authored Nov 15, 2022
1 parent 4ab595c commit 62d5981
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions validators/store/contract/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,41 +473,48 @@ func TestContractValidatorStore_CacheChange(t *testing.T) {
)
)

testCache := func(t *testing.T, expectedCache map[uint64]validators.Validators) {
type testCase struct {
height uint64
expected validators.Validators
}

testCache := func(t *testing.T, testCases ...testCase) {
t.Helper()

assert.Equal(t, len(expectedCache), store.validatorSetCache.Len())
assert.Equal(t, len(testCases), store.validatorSetCache.Len())

for height, expected := range expectedCache {
cache, ok := store.validatorSetCache.Get(height)
for _, testCase := range testCases {
cache, ok := store.validatorSetCache.Get(testCase.height)

assert.Truef(t, ok, "validators for %d must exist, but not found")
assert.Equal(t, expected, cache)
assert.Truef(t, ok, "validators at %d must exist, but not found", testCase.height)
assert.Equal(t, testCase.expected, cache)
}
}

// initial cache is empty
testCache(t, nil)
testCache(t)

// overflow doesn't occur
assert.False(
t,
store.saveToValidatorSetCache(0, ecdsaValidators1),
)

testCache(t, map[uint64]validators.Validators{
0: ecdsaValidators1,
})
testCache(
t,
testCase{height: 0, expected: ecdsaValidators1},
)

assert.False(
t,
store.saveToValidatorSetCache(1, ecdsaValidators2),
)

testCache(t, map[uint64]validators.Validators{
0: ecdsaValidators1,
1: ecdsaValidators2,
})
testCache(
t,
testCase{height: 0, expected: ecdsaValidators1},
testCase{height: 1, expected: ecdsaValidators2},
)

// make sure ecdsaValidators2 is loaded at the end for LRU cache
store.validatorSetCache.Get(1)
Expand All @@ -518,10 +525,11 @@ func TestContractValidatorStore_CacheChange(t *testing.T) {
store.saveToValidatorSetCache(2, blsValidators),
)

testCache(t, map[uint64]validators.Validators{
1: ecdsaValidators2,
2: blsValidators,
})
testCache(
t,
testCase{height: 1, expected: ecdsaValidators2},
testCase{height: 2, expected: blsValidators},
)
}

func TestContractValidatorStore_NoCache(t *testing.T) {
Expand Down

0 comments on commit 62d5981

Please sign in to comment.