diff options
author | Marc Pervaz Boocha <marcpervaz@qburst.com> | 2025-02-24 10:37:50 +0530 |
---|---|---|
committer | Marc Pervaz Boocha <marcpervaz@qburst.com> | 2025-02-27 13:37:32 +0530 |
commit | a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2 (patch) | |
tree | 5af6a2854644ac3dc44acdc9d604fa0eb576f120 /conn_test.go | |
parent | Added LTR Eviction (diff) | |
download | cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.tar cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.tar.gz cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.tar.bz2 cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.tar.lz cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.tar.xz cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.tar.zst cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.zip |
Added examples and documentation
Diffstat (limited to 'conn_test.go')
-rw-r--r-- | conn_test.go | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/conn_test.go b/conn_test.go new file mode 100644 index 0000000..d1581f3 --- /dev/null +++ b/conn_test.go @@ -0,0 +1,145 @@ +package cache + +import ( + "fmt" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func setupTestDB[K any, V any](t testing.TB) *DB[K, V] { + t.Helper() + + db, err := OpenMem[K, V]() + assert.NoError(t, err) + t.Cleanup(func() { + db.Close() + }) + return &db +} + +func TestDBGetSet(t *testing.T) { + t.Parallel() + + t.Run("Exists", func(t *testing.T) { + t.Parallel() + + db := setupTestDB[string, string](t) + + want := "Value" + err := db.Set("Key", want, 1*time.Hour) + assert.NoError(t, err) + + got, ttl, err := db.GetValue("Key") + assert.NoError(t, err) + assert.Equal(t, want, got) + + now := time.Now() + assert.WithinDuration(t, now.Add(ttl), now.Add(1*time.Hour), 1*time.Millisecond) + }) + + t.Run("Not Exists", func(t *testing.T) { + t.Parallel() + + db := setupTestDB[string, string](t) + + _, _, err := db.GetValue("Key") + assert.ErrorIs(t, err, ErrKeyNotFound) + }) + + t.Run("Update", func(t *testing.T) { + t.Parallel() + + db := setupTestDB[string, string](t) + + err := db.Set("Key", "Other", 0) + assert.NoError(t, err) + + want := "Value" + err = db.Set("Key", want, 0) + assert.NoError(t, err) + + got, _, err := db.GetValue("Key") + assert.NoError(t, err) + assert.Equal(t, want, got) + }) +} + +func TestDBDelete(t *testing.T) { + t.Parallel() + + t.Run("Exists", func(t *testing.T) { + t.Parallel() + + db := setupTestDB[string, string](t) + want := "Value" + err := db.Set("Key", want, 0) + assert.NoError(t, err) + + err = db.Delete("Key") + assert.NoError(t, err) + + _, _, err = db.GetValue("Key") + assert.ErrorIs(t, err, ErrKeyNotFound) + }) + + t.Run("Not Exists", func(t *testing.T) { + t.Parallel() + + db := setupTestDB[string, string](t) + + err := db.Delete("Key") + assert.ErrorIs(t, err, ErrKeyNotFound) + }) +} + +func BenchmarkDBGet(b *testing.B) { + for n := 1; n <= 10000; n *= 10 { + b.Run(fmt.Sprint(n), func(b *testing.B) { + db := setupTestDB[int, int](b) + for i := 0; i < n; i++ { + db.Set(i, i, 0) + } + b.ReportAllocs() + + b.ResetTimer() + for i := 0; i < b.N; i++ { + db.GetValue(n - 1) + } + }) + } +} + +func BenchmarkDBSet(b *testing.B) { + for n := 1; n <= 10000; n *= 10 { + b.Run(fmt.Sprint(n), func(b *testing.B) { + db := setupTestDB[int, int](b) + for i := 0; i < n-1; i++ { + db.Set(i, i, 0) + } + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + db.Set(n, n, 0) + } + }) + } +} + +func BenchmarkDBDelete(b *testing.B) { + for n := 1; n <= 10000; n *= 10 { + b.Run(fmt.Sprint(n), func(b *testing.B) { + db := setupTestDB[int, int](b) + for i := 0; i < n-1; i++ { + db.Set(i, i, 0) + } + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + db.Set(n, n, 0) + db.Delete(n) + } + }) + } +} |