@@ -9,6 +9,17 @@ import (
99 "google.golang.org/protobuf/proto"
1010)
1111
12+ func unmarshaler (t * testing.T ) func (b []byte , inCache bool ) (* types.ValueWithMetadata , bool ) {
13+ return func (b []byte , inCache bool ) (* types.ValueWithMetadata , bool ) {
14+ if ! inCache {
15+ return nil , inCache
16+ }
17+ value := & types.ValueWithMetadata {}
18+ require .NoError (t , proto .Unmarshal (b , value ))
19+ return value , inCache
20+ }
21+ }
22+
1223func TestCache (t * testing.T ) {
1324 cache := newCache (10 )
1425
@@ -34,8 +45,8 @@ func TestCache(t *testing.T) {
3445
3546 t .Run ("check basic storage and retrieval" , func (t * testing.T ) {
3647 // db1, key1 does not exist
37- v , err := cache .getState ("db1" , "key1" )
38- require .NoError (t , err )
48+ v , inCache := cache .getState ("db1" , "key1" )
49+ require .False (t , inCache )
3950 require .Nil (t , v )
4051
4152 s := & fastcache.Stats {}
@@ -54,11 +65,12 @@ func TestCache(t *testing.T) {
5465 }
5566 valBytes , err := proto .Marshal (db1Key1Value1 )
5667 require .NoError (t , err )
57- cache .putState ("db1" , "key1" , valBytes )
68+ require . NoError ( t , cache .putState ("db1" , "key1" , valBytes ) )
5869
59- // db1, key1 should exists
60- actualKey1Value1 , err := cache .getState ("db1" , "key1" )
61- require .NoError (t , err )
70+ // db1, key1 should exist
71+ actualKey1Value1 , inCache := unmarshaler (t )(cache .getState ("db1" , "key1" ))
72+ require .True (t , inCache )
73+ require .NotNil (t , actualKey1Value1 )
6274 require .True (t , proto .Equal (db1Key1Value1 , actualKey1Value1 ))
6375
6476 cache .dataCache .UpdateStats (s )
@@ -69,11 +81,12 @@ func TestCache(t *testing.T) {
6981 // update key1's value
7082 valBytes , err := proto .Marshal (db1Key1Value2 )
7183 require .NoError (t , err )
72- cache .putState ("db1" , "key1" , valBytes )
84+ require . NoError ( t , cache .putState ("db1" , "key1" , valBytes ) )
7385
7486 // db1, key1 should have the updated value
75- actualKey1Value2 , err := cache .getState ("db1" , "key1" )
76- require .NoError (t , err )
87+ actualKey1Value2 , inCache := unmarshaler (t )(cache .getState ("db1" , "key1" ))
88+ require .True (t , inCache )
89+ require .NotNil (t , actualKey1Value2 )
7790 require .True (t , proto .Equal (db1Key1Value2 , actualKey1Value2 ))
7891
7992 s := & fastcache.Stats {}
@@ -87,8 +100,8 @@ func TestCache(t *testing.T) {
87100 require .NoError (t , err )
88101 cache .putStateIfExist ("db2" , "key1" , valBytes )
89102
90- v , err := cache .getState ("db2" , "key1" )
91- require .NoError (t , err )
103+ v , inCache := unmarshaler ( t )( cache .getState ("db2" , "key1" ) )
104+ require .False (t , inCache )
92105 require .Nil (t , v )
93106
94107 s := & fastcache.Stats {}
@@ -99,15 +112,17 @@ func TestCache(t *testing.T) {
99112 t .Run ("store same key in two different databases" , func (t * testing.T ) {
100113 valBytes , err := proto .Marshal (db2Key1Value1 )
101114 require .NoError (t , err )
102- cache .putState ("db2" , "key1" , valBytes )
115+ require . NoError ( t , cache .putState ("db2" , "key1" , valBytes ) )
103116
104117 // both db1, key1 and db2, key1 should exist
105- actualDB1Key1Value2 , err := cache .getState ("db1" , "key1" )
106- require .NoError (t , err )
118+ actualDB1Key1Value2 , inCache := unmarshaler (t )(cache .getState ("db1" , "key1" ))
119+ require .True (t , inCache )
120+ require .NotNil (t , actualDB1Key1Value2 )
107121 require .True (t , proto .Equal (db1Key1Value2 , actualDB1Key1Value2 ))
108122
109- actualDB2Key1Value1 , err := cache .getState ("db2" , "key1" )
110- require .NoError (t , err )
123+ actualDB2Key1Value1 , inCache := unmarshaler (t )(cache .getState ("db2" , "key1" ))
124+ require .True (t , inCache )
125+ require .NotNil (t , actualDB2Key1Value1 )
111126 require .True (t , proto .Equal (db2Key1Value1 , actualDB2Key1Value1 ))
112127
113128 s := & fastcache.Stats {}
0 commit comments