diff options
author | Marc Pervaz Boocha <marcpervaz@qburst.com> | 2025-03-03 17:24:17 +0530 |
---|---|---|
committer | Marc Pervaz Boocha <marcpervaz@qburst.com> | 2025-03-03 17:24:17 +0530 |
commit | 7f9a513ea1803af327067b7bd84326c63b3d3844 (patch) | |
tree | d0fd50db0aa8d4d59a79c22fc9eb9b84e305a261 /conn_test.go | |
parent | Improved Concurency (diff) | |
download | cache-7f9a513ea1803af327067b7bd84326c63b3d3844.tar cache-7f9a513ea1803af327067b7bd84326c63b3d3844.tar.gz cache-7f9a513ea1803af327067b7bd84326c63b3d3844.tar.bz2 cache-7f9a513ea1803af327067b7bd84326c63b3d3844.tar.lz cache-7f9a513ea1803af327067b7bd84326c63b3d3844.tar.xz cache-7f9a513ea1803af327067b7bd84326c63b3d3844.tar.zst cache-7f9a513ea1803af327067b7bd84326c63b3d3844.zip |
Added documentation and tests
Diffstat (limited to '')
-rw-r--r-- | conn_test.go | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/conn_test.go b/conn_test.go index ee2c175..c80072a 100644 --- a/conn_test.go +++ b/conn_test.go @@ -131,6 +131,105 @@ func TestDBDelete(t *testing.T) { }) } +func TestDBUpdateInPlace(t *testing.T) { + t.Parallel() + + t.Run("Exists", func(t *testing.T) { + t.Parallel() + + store := setupTestDB[string, string](t) + + want := "Value" + store.Set("Key", "Initial", 1*time.Hour) + + processFunc := func(v string) (string, error) { + return want, nil + } + + if err := store.UpdateInPlace("Key", processFunc, 1*time.Hour); err != nil { + t.Fatalf("unexpected error: %v", err) + } + + got, _, err := store.GetValue("Key") + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if want != got { + t.Errorf("got %v, want %v", got, want) + } + }) + + t.Run("Not Exists", func(t *testing.T) { + t.Parallel() + + store := setupTestDB[string, string](t) + + want := "Value" + + processFunc := func(v string) (string, error) { + return want, nil + } + + if err := store.UpdateInPlace("Key", processFunc, 1*time.Hour); !errors.Is(err, ErrKeyNotFound) { + t.Fatalf("expected error: %v, got: %v", ErrKeyNotFound, err) + } + }) +} + +func TestDBMemoize(t *testing.T) { + t.Parallel() + + t.Run("Cache Miss", func(t *testing.T) { + t.Parallel() + + store := setupTestDB[string, string](t) + + want := "Value" + + factoryFunc := func() (string, error) { + return want, nil + } + + got, err := store.Memorize("Key", factoryFunc, 1*time.Hour) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if got != "Value" { + t.Fatalf("expected: %v, got: %v", "Value", got) + } + + got, _, err = store.GetValue("Key") + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if want != got { + t.Errorf("got %v, want %v", got, want) + } + }) + + t.Run("Cache Hit", func(t *testing.T) { + t.Parallel() + + store := setupTestDB[string, string](t) + + want := "NewValue" + + store.Set("Key", "Value", 1*time.Hour) + + factoryFunc := func() (string, error) { + return want, nil + } + + got, err := store.Memorize("Key", factoryFunc, 1*time.Hour) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if got != "Value" { + t.Fatalf("expected: %v, got: %v", "Value", got) + } + }) +} + func BenchmarkDBGet(b *testing.B) { for n := 1; n <= 10000; n *= 10 { b.Run(strconv.Itoa(n), func(b *testing.B) { |