github.com/oam-dev/kubevela@v1.9.11/pkg/utils/cache_test.go (about) 1 /* 2 Copyright 2021 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package utils 18 19 import ( 20 "context" 21 "fmt" 22 "testing" 23 "time" 24 25 . "github.com/onsi/ginkgo/v2" 26 . "github.com/onsi/gomega" 27 ) 28 29 var _ = Describe("Test cache utils", func() { 30 It("should return false for IsExpired()", func() { 31 c := newMemoryCache("test", 10*time.Hour) 32 Expect(c.IsExpired()).Should(BeFalse()) 33 }) 34 35 It("test cache store", func() { 36 store := NewMemoryCacheStore(context.TODO()) 37 store.Put("test", "test data", time.Second*2) 38 store.Put("test2", "test data", 0) 39 store.Put("test3", "test data", -1) 40 time.Sleep(3 * time.Second) 41 Expect(store.Get("test")).Should(BeNil()) 42 Expect(store.Get("test2")).Should(Equal("test data")) 43 Expect(store.Get("test3")).Should(Equal("test data")) 44 }) 45 46 It("test cache store delete key", func() { 47 store := NewMemoryCacheStore(context.TODO()) 48 store.Put("test", "test data", time.Minute*2) 49 store.Delete("test") 50 Expect(store.Get("test")).Should(BeNil()) 51 }) 52 }) 53 54 var store *MemoryCacheStore 55 56 // BenchmarkWrite 57 func BenchmarkWrite(b *testing.B) { 58 store = NewMemoryCacheStore(context.TODO()) 59 60 for i := 0; i < b.N; i++ { 61 store.Put(fmt.Sprintf("%d", i), i, 0) 62 } 63 } 64 65 // BenchmarkRead 66 func BenchmarkRead(b *testing.B) { 67 for i := 0; i < b.N; i++ { 68 store.Get(fmt.Sprintf("%d", i)) 69 } 70 } 71 72 // BenchmarkRW 73 func BenchmarkRW(b *testing.B) { 74 for i := 0; i < b.N; i++ { 75 store.Put(fmt.Sprintf("%d", i), i, 1) 76 store.Get(fmt.Sprintf("%d", i)) 77 } 78 }