Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inconsistent deletion listener behavior #101

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ linters:
- gosimple
- govet
- ineffassign
- interfacer
- misspell
- nakedret
- nilerr
Expand Down
29 changes: 21 additions & 8 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,21 @@ import (
"github.com/maypok86/otter/internal/xruntime"
)

func getRandomSize(t *testing.T) int {
t.Helper()

const (
minSize = 10
maxSize = 1000
)

r := rand.New(rand.NewSource(time.Now().UnixNano()))

return r.Intn(maxSize-minSize) + minSize
}

func TestCache_Set(t *testing.T) {
const size = 256
size := getRandomSize(t)
var mutex sync.Mutex
m := make(map[DeletionCause]int)
c, err := MustBuilder[int, int](size).
Expand Down Expand Up @@ -60,7 +73,7 @@ func TestCache_Set(t *testing.T) {

r := rand.New(rand.NewSource(time.Now().UnixNano()))
for a := 0; a < 10000; a++ {
k := r.Int() % 100
k := r.Int() % size
val, ok := c.Get(k)
if !ok {
err = fmt.Errorf("expected %d but got nil", k)
Expand Down Expand Up @@ -91,7 +104,7 @@ func TestCache_Set(t *testing.T) {
}

func TestCache_SetIfAbsent(t *testing.T) {
const size = 100
size := getRandomSize(t)
c, err := MustBuilder[int, int](size).WithTTL(time.Minute).CollectStats().Build()
if err != nil {
t.Fatalf("can not create cache: %v", err)
Expand Down Expand Up @@ -140,15 +153,15 @@ func TestCache_SetIfAbsent(t *testing.T) {
}
}

if hits := cc.Stats().Hits(); hits != size {
if hits := cc.Stats().Hits(); hits != int64(size) {
t.Fatalf("hit ratio should be 100%%. Hits: %d", hits)
}

cc.Close()
}

func TestCache_SetWithTTL(t *testing.T) {
size := 256
size := getRandomSize(t)
var mutex sync.Mutex
m := make(map[DeletionCause]int)
c, err := MustBuilder[int, int](size).
Expand Down Expand Up @@ -230,7 +243,7 @@ func TestCache_SetWithTTL(t *testing.T) {
}

func TestCache_Delete(t *testing.T) {
size := 256
size := getRandomSize(t)
var mutex sync.Mutex
m := make(map[DeletionCause]int)
c, err := MustBuilder[int, int](size).
Expand Down Expand Up @@ -276,7 +289,7 @@ func TestCache_Delete(t *testing.T) {
}

func TestCache_DeleteByFunc(t *testing.T) {
size := 256
size := getRandomSize(t)
var mutex sync.Mutex
m := make(map[DeletionCause]int)
c, err := MustBuilder[int, int](size).
Expand Down Expand Up @@ -318,7 +331,7 @@ func TestCache_DeleteByFunc(t *testing.T) {
}

func TestCache_Advanced(t *testing.T) {
size := 256
size := getRandomSize(t)
defaultTTL := time.Hour
c, err := MustBuilder[int, int](size).
WithTTL(defaultTTL).
Expand Down
Loading
Loading