-
Notifications
You must be signed in to change notification settings - Fork 17
/
builder_test.go
61 lines (53 loc) · 1.96 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
53
54
55
56
57
58
59
60
61
package theine_test
import (
"context"
"reflect"
"testing"
"github.com/Yiling-J/theine-go"
"github.com/Yiling-J/theine-go/internal"
"github.com/stretchr/testify/require"
)
func TestBuilder(t *testing.T) {
// simple cache
_, err := theine.NewBuilder[int, int](-500).Build()
require.Error(t, err)
builder := theine.NewBuilder[int, int](100)
builder = builder.Cost(func(v int) int64 { return 1 })
builder = builder.Doorkeeper(false)
builder = builder.RemovalListener(func(key, value int, reason theine.RemoveReason) {})
cache, err := builder.Build()
require.Nil(t, err)
require.Equal(t, reflect.TypeOf(&theine.Cache[int, int]{}), reflect.TypeOf(cache))
// loading cache
_, err = builder.Loading(nil).Build()
require.Error(t, err)
builderL := builder.Loading(func(ctx context.Context, key int) (theine.Loaded[int], error) {
return theine.Loaded[int]{}, nil
})
cacheL, err := builderL.Build()
require.Nil(t, err)
require.Equal(t, reflect.TypeOf(&theine.LoadingCache[int, int]{}), reflect.TypeOf(cacheL))
// hybrid cache
_, err = builder.Hybrid(nil).Build()
require.Error(t, err)
secondary := internal.NewSimpleMapSecondary[int, int]()
_, err = builder.Hybrid(secondary).Workers(0).Build()
require.Error(t, err)
builderH := builder.Hybrid(secondary).Workers(1).AdmProbability(0.8)
cacheH, err := builderH.Build()
require.Nil(t, err)
require.Equal(t, reflect.TypeOf(&theine.HybridCache[int, int]{}), reflect.TypeOf(cacheH))
// loading + hybrid
builderLH := builderL.Hybrid(secondary)
cacheLH, err := builderLH.Build()
require.Nil(t, err)
require.Equal(t, reflect.TypeOf(&theine.HybridLoadingCache[int, int]{}), reflect.TypeOf(cacheLH))
// hybrid + loading
builderLH = builderH.Workers(8).Loading(
func(ctx context.Context, key int) (theine.Loaded[int], error) {
return theine.Loaded[int]{}, nil
})
cacheLH, err = builderLH.Build()
require.Nil(t, err)
require.Equal(t, reflect.TypeOf(&theine.HybridLoadingCache[int, int]{}), reflect.TypeOf(cacheLH))
}