summaryrefslogtreecommitdiffstats
path: root/store_test.go
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--store_test.go101
1 files changed, 66 insertions, 35 deletions
diff --git a/store_test.go b/store_test.go
index 9b802bf..89f4de6 100644
--- a/store_test.go
+++ b/store_test.go
@@ -1,19 +1,19 @@
package cache
import (
+ "bytes"
"encoding/binary"
- "fmt"
+ "strconv"
"testing"
"time"
-
- "github.com/stretchr/testify/assert"
)
-func setupTestStore(t testing.TB) *store {
- t.Helper()
+func setupTestStore(tb testing.TB) *store {
+ tb.Helper()
store := &store{}
store.Init()
+
return store
}
@@ -28,11 +28,16 @@ func TestStoreGetSet(t *testing.T) {
want := []byte("Value")
store.Set([]byte("Key"), want, 1*time.Hour)
got, ttl, ok := store.Get([]byte("Key"))
- assert.Equal(t, want, got)
+ if !ok {
+ t.Errorf("expected key to exist")
+ }
+ if !bytes.Equal(want, got) {
+ t.Errorf("got %v, want %v", got, want)
+ }
+ if ttl.Round(time.Second) != 1*time.Hour {
+ t.Errorf("ttl same: got %v expected %v", ttl.Round(time.Second), 1*time.Hour)
+ }
- now := time.Now()
- assert.WithinDuration(t, now.Add(ttl), now.Add(1*time.Hour), 1*time.Millisecond)
- assert.True(t, ok)
})
t.Run("Exists TTL", func(t *testing.T) {
@@ -42,17 +47,18 @@ func TestStoreGetSet(t *testing.T) {
want := []byte("Value")
store.Set([]byte("Key"), want, time.Nanosecond)
- _, _, ok := store.Get([]byte("Key"))
- assert.False(t, ok)
+ if _, _, ok := store.Get([]byte("Key")); ok {
+ t.Errorf("expected key to not exist")
+ }
})
t.Run("Not Exists", func(t *testing.T) {
t.Parallel()
store := setupTestStore(t)
-
- _, _, ok := store.Get([]byte("Key"))
- assert.False(t, ok)
+ if _, _, ok := store.Get([]byte("Key")); ok {
+ t.Errorf("expected key to not exist")
+ }
})
t.Run("Update", func(t *testing.T) {
@@ -61,11 +67,16 @@ func TestStoreGetSet(t *testing.T) {
store := setupTestStore(t)
store.Set([]byte("Key"), []byte("Other"), 0)
+
want := []byte("Value")
store.Set([]byte("Key"), want, 0)
got, _, ok := store.Get([]byte("Key"))
- assert.Equal(t, want, got)
- assert.True(t, ok)
+ if !bytes.Equal(want, got) {
+ t.Errorf("got %v, want %v", got, want)
+ }
+ if !ok {
+ t.Errorf("expected key to exist")
+ }
})
t.Run("Resize", func(t *testing.T) {
@@ -80,16 +91,20 @@ func TestStoreGetSet(t *testing.T) {
for i := range store.Length {
key := binary.LittleEndian.AppendUint64(nil, i)
- _, _, ok := store.Get(key)
- assert.True(t, ok, i)
+ if _, _, ok := store.Get(key); !ok {
+ t.Errorf("expected key %v to exist", i)
+ }
}
- assert.Len(t, store.Bucket, int(initialBucketSize)*2)
+ if len(store.Bucket) != int(initialBucketSize)*2 {
+ t.Errorf("expected bucket size to be %v, got %v", initialBucketSize*2, len(store.Bucket))
+ }
for i := range store.Length {
key := binary.LittleEndian.AppendUint64(nil, i)
- _, _, ok := store.Get(key)
- assert.True(t, ok, i)
+ if _, _, ok := store.Get(key); !ok {
+ t.Errorf("expected key %d to exist", i)
+ }
}
})
}
@@ -104,10 +119,13 @@ func TestStoreDelete(t *testing.T) {
want := []byte("Value")
store.Set([]byte("Key"), want, 0)
- ok := store.Delete([]byte("Key"))
- assert.True(t, ok)
- _, _, ok = store.Get([]byte("Key"))
- assert.False(t, ok)
+
+ if !store.Delete([]byte("Key")) {
+ t.Errorf("expected key to be deleted")
+ }
+ if _, _, ok := store.Get([]byte("Key")); ok {
+ t.Errorf("expected key to not exist")
+ }
})
t.Run("Not Exists", func(t *testing.T) {
@@ -115,8 +133,9 @@ func TestStoreDelete(t *testing.T) {
store := setupTestStore(t)
- ok := store.Delete([]byte("Key"))
- assert.False(t, ok)
+ if store.Delete([]byte("Key")) {
+ t.Errorf("expected key to not exist")
+ }
})
}
@@ -128,25 +147,29 @@ func TestStoreClear(t *testing.T) {
want := []byte("Value")
store.Set([]byte("Key"), want, 0)
store.Clear()
- _, _, ok := store.Get([]byte("Key"))
- assert.False(t, ok)
+ if _, _, ok := store.Get([]byte("Key")); ok {
+ t.Errorf("expected key to not exist")
+ }
}
func BenchmarkStoreGet(b *testing.B) {
for n := 1; n <= 10000; n *= 10 {
- b.Run(fmt.Sprint(n), func(b *testing.B) {
+ b.Run(strconv.Itoa(n), func(b *testing.B) {
want := setupTestStore(b)
+
for i := range n - 1 {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(i))
want.Set(buf, buf, 0)
}
+
key := []byte("Key")
want.Set(key, []byte("Store"), 0)
b.ReportAllocs()
b.ResetTimer()
- for i := 0; i < b.N; i++ {
+
+ for b.Loop() {
want.Get(key)
}
})
@@ -155,18 +178,22 @@ func BenchmarkStoreGet(b *testing.B) {
func BenchmarkStoreSet(b *testing.B) {
for n := 1; n <= 10000; n *= 10 {
- b.Run(fmt.Sprint(n), func(b *testing.B) {
+ b.Run(strconv.Itoa(n), func(b *testing.B) {
want := setupTestStore(b)
+
for i := range n - 1 {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(i))
want.Set(buf, buf, 0)
}
+
key := []byte("Key")
store := []byte("Store")
+
b.ReportAllocs()
b.ResetTimer()
- for i := 0; i < b.N; i++ {
+
+ for b.Loop() {
want.Set(key, store, 0)
}
})
@@ -175,18 +202,22 @@ func BenchmarkStoreSet(b *testing.B) {
func BenchmarkStoreDelete(b *testing.B) {
for n := 1; n <= 10000; n *= 10 {
- b.Run(fmt.Sprint(n), func(b *testing.B) {
+ b.Run(strconv.Itoa(n), func(b *testing.B) {
want := setupTestStore(b)
+
for i := range n - 1 {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(i))
want.Set(buf, buf, 0)
}
+
key := []byte("Key")
store := []byte("Store")
+
b.ReportAllocs()
b.ResetTimer()
- for i := 0; i < b.N; i++ {
+
+ for b.Loop() {
want.Set(key, store, 0)
want.Delete(key)
}