Skip to content

Commit

Permalink
[Chore] Add tests for cache builder
Browse files Browse the repository at this point in the history
  • Loading branch information
maypok86 committed Oct 22, 2023
1 parent ec25b28 commit 6625706
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
3 changes: 2 additions & 1 deletion builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type options[K comparable, V any] struct {
}

func (o *options[K, V]) validate() error {
if o.shardCount == illegalShardCount {
// shard count should be power of two.
if o.shardCount <= 0 || (o.shardCount&(o.shardCount-1)) != 0 {
return ErrIllegalShardCount
}

Expand Down
47 changes: 47 additions & 0 deletions builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package otter

import (
"errors"
"reflect"
"testing"
)

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, err := NewBuilder[int, int](10)
if err != nil {
t.Fatalf("builder creates with error %v", err)
}

_, 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, err := NewBuilder[int, int](10)
if err != nil {
t.Fatalf("builder creates with error %v", err)
}

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)
}
}

0 comments on commit 6625706

Please sign in to comment.