github.com/maypok86/otter@v1.2.1/builder_test.go (about) 1 // Copyright (c) 2023 Alexey Mayshev. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package otter 16 17 import ( 18 "errors" 19 "fmt" 20 "reflect" 21 "testing" 22 "time" 23 ) 24 25 func TestBuilder_MustFailed(t *testing.T) { 26 defer func() { 27 if r := recover(); r != nil { 28 fmt.Println("recover: ", r) 29 } 30 }() 31 MustBuilder[int, int](-1) 32 t.Fatal("no panic detected") 33 } 34 35 func TestBuilder_NewFailed(t *testing.T) { 36 _, err := NewBuilder[int, int](-63) 37 if err == nil || !errors.Is(err, ErrIllegalCapacity) { 38 t.Fatalf("should fail with an error %v, but got %v", ErrIllegalCapacity, err) 39 } 40 41 capacity := 100 42 // negative const ttl 43 _, err = MustBuilder[int, int](capacity).WithTTL(-1).Build() 44 if err == nil || !errors.Is(err, ErrIllegalTTL) { 45 t.Fatalf("should fail with an error %v, but got %v", ErrIllegalTTL, err) 46 } 47 48 // negative initial capacity 49 _, err = MustBuilder[int, int](capacity).InitialCapacity(-2).Build() 50 if err == nil || !errors.Is(err, ErrIllegalInitialCapacity) { 51 t.Fatalf("should fail with an error %v, but got %v", ErrIllegalInitialCapacity, err) 52 } 53 54 _, err = MustBuilder[int, int](capacity).WithTTL(time.Hour).InitialCapacity(0).Build() 55 if err == nil || !errors.Is(err, ErrIllegalInitialCapacity) { 56 t.Fatalf("should fail with an error %v, but got %v", ErrIllegalInitialCapacity, err) 57 } 58 59 _, err = MustBuilder[int, int](capacity).WithVariableTTL().InitialCapacity(-5).Build() 60 if err == nil || !errors.Is(err, ErrIllegalInitialCapacity) { 61 t.Fatalf("should fail with an error %v, but got %v", ErrIllegalInitialCapacity, err) 62 } 63 64 // nil cost func 65 _, err = MustBuilder[int, int](capacity).Cost(nil).Build() 66 if err == nil || !errors.Is(err, ErrNilCostFunc) { 67 t.Fatalf("should fail with an error %v, but got %v", ErrNilCostFunc, err) 68 } 69 } 70 71 func TestBuilder_BuildSuccess(t *testing.T) { 72 b := MustBuilder[int, int](10) 73 74 _, err := b.InitialCapacity(unsetCapacity).Build() 75 if err != nil { 76 t.Fatalf("builded cache with error: %v", err) 77 } 78 79 c, err := b. 80 CollectStats(). 81 InitialCapacity(10). 82 Cost(func(key int, value int) uint32 { 83 return 2 84 }).Build() 85 if err != nil { 86 t.Fatalf("builded cache with error: %v", err) 87 } 88 89 if !reflect.DeepEqual(reflect.TypeOf(Cache[int, int]{}), reflect.TypeOf(c)) { 90 t.Fatalf("builder returned a different type of cache: %v", err) 91 } 92 93 cc, err := b.WithTTL(time.Minute).CollectStats().Cost(func(key int, value int) uint32 { 94 return 2 95 }).DeletionListener(func(key int, value int, cause DeletionCause) { 96 fmt.Println("const ttl") 97 }).Build() 98 if err != nil { 99 t.Fatalf("builded cache with error: %v", err) 100 } 101 102 if !reflect.DeepEqual(reflect.TypeOf(Cache[int, int]{}), reflect.TypeOf(cc)) { 103 t.Fatalf("builder returned a different type of cache: %v", err) 104 } 105 106 cv, err := b.WithVariableTTL().CollectStats().Cost(func(key int, value int) uint32 { 107 return 2 108 }).DeletionListener(func(key int, value int, cause DeletionCause) { 109 fmt.Println("variable ttl") 110 }).Build() 111 if err != nil { 112 t.Fatalf("builded cache with error: %v", err) 113 } 114 115 if !reflect.DeepEqual(reflect.TypeOf(CacheWithVariableTTL[int, int]{}), reflect.TypeOf(cv)) { 116 t.Fatalf("builder returned a different type of cache: %v", err) 117 } 118 }