From 9ca41da8b63d9ab0fa7bd280935aaed9c2370be0 Mon Sep 17 00:00:00 2001 From: AnastasiaShemyakinskaya Date: Thu, 22 Aug 2024 11:23:20 +0200 Subject: [PATCH] GO-3675: add error for nil value Signed-off-by: AnastasiaShemyakinskaya --- app/ocache/ocache.go | 8 +++++--- app/ocache/ocache_test.go | 19 +++++-------------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/app/ocache/ocache.go b/app/ocache/ocache.go index 77c037fa..dfd99192 100644 --- a/app/ocache/ocache.go +++ b/app/ocache/ocache.go @@ -3,6 +3,7 @@ package ocache import ( "context" "errors" + "fmt" "sync" "time" @@ -167,6 +168,9 @@ func (c *oCache) load(ctx context.Context, id string, e *entry) { c.mu.Lock() defer c.mu.Unlock() + if value == nil { + err = fmt.Errorf("loaded value is nil, id: %s", id) + } if err != nil { e.loadErr = err delete(c.data, id) @@ -199,9 +203,7 @@ func (c *oCache) remove(ctx context.Context, e *entry) (ok bool, err error) { _, curState := e.setClosing(true) if curState == entryStateClosing { ok = true - if e.value != nil { - err = e.value.Close() - } + err = e.value.Close() c.mu.Lock() e.setClosed() delete(c.data, e.id) diff --git a/app/ocache/ocache_test.go b/app/ocache/ocache_test.go index 63731abe..2425a6e6 100644 --- a/app/ocache/ocache_test.go +++ b/app/ocache/ocache_test.go @@ -140,24 +140,15 @@ func TestOCache_Get(t *testing.T) { assert.Equal(t, context.Canceled, err) assert.NoError(t, c.Close()) }) - t.Run("parallel load and remove", func(t *testing.T) { - var waitCh = make(chan struct{}) + t.Run("value is nil", func(t *testing.T) { c := New(func(ctx context.Context, id string) (value Object, err error) { return nil, nil }) - id := "id" - go func() { - val, err := c.Get(context.TODO(), id) - close(waitCh) - require.NoError(t, err) - assert.Nil(t, val) - }() - - <-waitCh - ok, err := c.Remove(context.TODO(), id) - require.NoError(t, err) - assert.True(t, ok) + value, err := c.Get(ctx, "id") + assert.NotNil(t, err) + assert.Nil(t, value) + assert.NoError(t, c.Close()) }) }