github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/framework/cache/write_through_test.go (about) 1 // The package is migrated from beego, you can get from following link: 2 // import( 3 // "github.com/beego/beego/v2/client/cache" 4 // ) 5 // Copyright 2023. All Rights Reserved. 6 // 7 // Licensed under the Apache License, Version 2.0 (the "License"); 8 // you may not use this file except in compliance with the License. 9 // You may obtain a copy of the License at 10 // 11 // http://www.apache.org/licenses/LICENSE-2.0 12 // 13 // Unless required by applicable law or agreed to in writing, software 14 // distributed under the License is distributed on an "AS IS" BASIS, 15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 // See the License for the specific language governing permissions and 17 // limitations under the License. 18 19 // nolint 20 package cache 21 22 import ( 23 "context" 24 "errors" 25 "fmt" 26 "testing" 27 "time" 28 29 "github.com/stretchr/testify/assert" 30 31 "github.com/mdaxf/iac/framework/berror" 32 ) 33 34 func TestWriteThoughCache_Set(t *testing.T) { 35 mockDbStore := make(map[string]any) 36 37 testCases := []struct { 38 name string 39 cache Cache 40 storeFunc func(ctx context.Context, key string, val any) error 41 key string 42 value any 43 wantErr error 44 }{ 45 { 46 name: "store key/value in db fail", 47 cache: NewMemoryCache(), 48 storeFunc: func(ctx context.Context, key string, val any) error { 49 return errors.New("failed") 50 }, 51 wantErr: berror.Wrap(errors.New("failed"), PersistCacheFailed, 52 fmt.Sprintf("key: %s, val: %v", "", nil)), 53 }, 54 { 55 name: "store key/value success", 56 cache: NewMemoryCache(), 57 storeFunc: func(ctx context.Context, key string, val any) error { 58 mockDbStore[key] = val 59 return nil 60 }, 61 key: "hello", 62 value: "world", 63 }, 64 } 65 for _, tt := range testCases { 66 t.Run(tt.name, func(t *testing.T) { 67 w, err := NewWriteThroughCache(tt.cache, tt.storeFunc) 68 if err != nil { 69 assert.EqualError(t, tt.wantErr, err.Error()) 70 return 71 } 72 73 err = w.Set(context.Background(), tt.key, tt.value, 60*time.Second) 74 if err != nil { 75 assert.EqualError(t, tt.wantErr, err.Error()) 76 return 77 } 78 79 val, err := w.Get(context.Background(), tt.key) 80 assert.Nil(t, err) 81 assert.Equal(t, tt.value, val) 82 83 vv, ok := mockDbStore[tt.key] 84 assert.True(t, ok) 85 assert.Equal(t, tt.value, vv) 86 }) 87 } 88 } 89 90 func TestNewWriteThoughCache(t *testing.T) { 91 underlyingCache := NewMemoryCache() 92 storeFunc := func(ctx context.Context, key string, val any) error { return nil } 93 94 type args struct { 95 cache Cache 96 fn func(ctx context.Context, key string, val any) error 97 } 98 tests := []struct { 99 name string 100 args args 101 wantRes *WriteThroughCache 102 wantErr error 103 }{ 104 { 105 name: "nil cache parameters", 106 args: args{ 107 cache: nil, 108 fn: storeFunc, 109 }, 110 wantErr: berror.Error(InvalidInitParameters, "cache or storeFunc can not be nil"), 111 }, 112 { 113 name: "nil storeFunc parameters", 114 args: args{ 115 cache: underlyingCache, 116 fn: nil, 117 }, 118 wantErr: berror.Error(InvalidInitParameters, "cache or storeFunc can not be nil"), 119 }, 120 { 121 name: "init write-though cache success", 122 args: args{ 123 cache: underlyingCache, 124 fn: storeFunc, 125 }, 126 wantRes: &WriteThroughCache{ 127 Cache: underlyingCache, 128 storeFunc: storeFunc, 129 }, 130 }, 131 } 132 for _, tt := range tests { 133 t.Run(tt.name, func(t *testing.T) { 134 _, err := NewWriteThroughCache(tt.args.cache, tt.args.fn) 135 assert.Equal(t, tt.wantErr, err) 136 if err != nil { 137 return 138 } 139 }) 140 } 141 } 142 143 func ExampleNewWriteThroughCache() { 144 c := NewMemoryCache() 145 wtc, err := NewWriteThroughCache(c, func(ctx context.Context, key string, val any) error { 146 fmt.Printf("write data to somewhere key %s, val %v \n", key, val) 147 return nil 148 }) 149 if err != nil { 150 panic(err) 151 } 152 err = wtc.Set(context.Background(), 153 "/biz/user/id=1", "I am user 1", time.Minute) 154 if err != nil { 155 panic(err) 156 } 157 // Output: 158 // write data to somewhere key /biz/user/id=1, val I am user 1 159 }