forked from maypok86/otter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder_test.go
52 lines (44 loc) · 1.13 KB
/
builder_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package otter
import (
"errors"
"fmt"
"reflect"
"testing"
)
func TestBuilder_MustFailed(t *testing.T) {
defer func() {
if r := recover(); r != nil {
fmt.Println("recover: ", r)
}
}()
MustBuilder[int, int](-1)
t.Fatal("no panic detected")
}
func TestBuilder_NewFailed(t *testing.T) {
_, err := NewBuilder[int, int](-63)
if err == nil || !errors.Is(err, ErrIllegalCapacity) {
t.Fatalf("should fail with an error %v, but got %v", ErrIllegalCapacity, err)
}
}
func TestBuilder_BuildFailed(t *testing.T) {
b := MustBuilder[int, int](10)
_, err := b.ShardCount(129).Build()
if err == nil || !errors.Is(err, ErrIllegalShardCount) {
t.Fatalf("should fail with an error %v, but got %v", ErrIllegalShardCount, err)
}
}
func TestBuilder_BuildSuccess(t *testing.T) {
b := MustBuilder[int, int](10)
c, err := b.
ShardCount(256).
StatsEnabled(true).
Cost(func(key int, value int) uint32 {
return 2
}).Build()
if err != nil {
t.Fatalf("builded cache with error: %v", err)
}
if !reflect.DeepEqual(reflect.TypeOf(&Cache[int, int]{}), reflect.TypeOf(c)) {
t.Fatalf("builder returned a different type of cache: %v", err)
}
}