Skip to content

Commit

Permalink
[#98] Fixed inconsistent listener behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
maypok86 committed Jul 29, 2024
1 parent e57f61b commit 7f72200
Show file tree
Hide file tree
Showing 17 changed files with 278 additions and 258 deletions.
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

0 comments on commit 7f72200

Please sign in to comment.