github.com/codingfuture/orig-energi3@v0.8.4/energi/common/cache_test.go (about) 1 // Copyright 2019 The Energi Core Authors 2 // This file is part of the Energi Core library. 3 // 4 // The Energi Core 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 Energi Core 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 Energi Core library. If not, see <http://www.gnu.org/licenses/>. 16 17 package common 18 19 import ( 20 "math/big" 21 "reflect" 22 "testing" 23 "time" 24 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/core/types" 27 ) 28 29 var dummyBlockHash = common.BytesToHash([]byte{1, 3, 4, 5}) 30 31 type fakeChain struct{} 32 33 func (f *fakeChain) CurrentBlock() *types.Block { 34 return types.NewBlock(&types.Header{ 35 ParentHash: dummyBlockHash, 36 }, nil, nil, nil) 37 } 38 39 // TestDataCache tests the cache's setter and getter methods. 40 func TestDataCache(t *testing.T) { 41 chain := new(fakeChain) 42 cacheInstance := NewCacheStorage() 43 44 var newData interface{} 45 cacheQueryfunc := func(num *big.Int) (interface{}, error) { 46 return newData, nil 47 } 48 delayedQueryfunc := func(num *big.Int) (interface{}, error) { 49 time.Sleep(time.Second) 50 return cacheQueryfunc(num) 51 } 52 53 t.Run("Test_adding_new_data_nil_data", func(t *testing.T) { 54 data, err := cacheInstance.Get(chain, cacheQueryfunc) 55 if err != ErrInvalidData { 56 t.Fatalf("expected error (%v) but found (%v)", ErrInvalidData, err) 57 } 58 59 if !reflect.DeepEqual(data, newData) { 60 t.Fatalf("expected the returned data to match but it didn't") 61 } 62 }) 63 64 t.Run("Test_adding_cache_data", func(t *testing.T) { 65 newData = []int{1, 2, 3, 5, 6, 6} 66 data, err := cacheInstance.Get(chain, cacheQueryfunc) 67 if err != nil { 68 t.Fatalf("expected no error but found %v", err) 69 } 70 71 if !reflect.DeepEqual(data, newData) { 72 t.Fatalf("expected the returned data to match but it didn't") 73 } 74 }) 75 76 t.Run("Test_adding_new_cache_data_but_with_old_hash", func(t *testing.T) { 77 newData = "This is some random new data" 78 data, err := cacheInstance.Get(chain, cacheQueryfunc) 79 if err != nil { 80 t.Fatalf("expected no error but found %v", err) 81 } 82 83 if reflect.DeepEqual(data, newData) { 84 t.Fatalf("expected the returned data not to match but it did") 85 } 86 }) 87 88 t.Run("Test_adding_new_cache_data_but_with_new_hash", func(t *testing.T) { 89 dummyBlockHash = common.BytesToHash([]byte{120, 23, 90, 5}) 90 newData = map[string]int{ 91 "rsc": 3711, 92 "r": 2138, 93 "gri": 1908, 94 "adg": 912, 95 } 96 97 data, err := cacheInstance.Get(chain, cacheQueryfunc) 98 if err != nil { 99 t.Fatalf("expected no error but found %v", err) 100 } 101 102 if reflect.DeepEqual(data, newData) { 103 t.Fatalf("expected the returned data to be old on the first call") 104 } 105 106 time.Sleep(100 * time.Millisecond) 107 108 data, err = cacheInstance.Get(chain, cacheQueryfunc) 109 if err != nil { 110 t.Fatalf("expected no error but found %v", err) 111 } 112 113 if !reflect.DeepEqual(data, newData) { 114 t.Fatalf("expected the returned data to match it didn't") 115 } 116 }) 117 118 t.Run("Test_stress", func(t *testing.T) { 119 dummyBlockHash = common.BytesToHash([]byte{120, 23, 90, 7}) 120 newData = map[string]int{ 121 "rsc": 3711, 122 } 123 124 tf := func() { 125 data, err := cacheInstance.Get(chain, delayedQueryfunc) 126 if err != nil { 127 t.Fatalf("expected no error but found %v", err) 128 } 129 130 if reflect.DeepEqual(data, newData) { 131 t.Fatalf("expected the returned data to be old on the first call") 132 } 133 } 134 135 for i := 100; i > 0; i-- { 136 tf() 137 } 138 139 time.Sleep(1100 * time.Millisecond) 140 141 data, err := cacheInstance.Get(chain, cacheQueryfunc) 142 if err != nil { 143 t.Fatalf("expected no error but found %v", err) 144 } 145 146 if !reflect.DeepEqual(data, newData) { 147 t.Fatalf("expected the returned data to match it didn't") 148 } 149 }) 150 151 }