1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-08 14:07:02 +09:00

GO-3675: add error for nil value

Signed-off-by: AnastasiaShemyakinskaya <shem98a@mail.ru>
This commit is contained in:
AnastasiaShemyakinskaya 2024-08-22 11:23:20 +02:00
parent 03ccc4dd29
commit 9ca41da8b6
No known key found for this signature in database
GPG key ID: CCD60ED83B103281
2 changed files with 10 additions and 17 deletions

View file

@ -3,6 +3,7 @@ package ocache
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"sync" "sync"
"time" "time"
@ -167,6 +168,9 @@ func (c *oCache) load(ctx context.Context, id string, e *entry) {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
if value == nil {
err = fmt.Errorf("loaded value is nil, id: %s", id)
}
if err != nil { if err != nil {
e.loadErr = err e.loadErr = err
delete(c.data, id) 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) _, curState := e.setClosing(true)
if curState == entryStateClosing { if curState == entryStateClosing {
ok = true ok = true
if e.value != nil {
err = e.value.Close() err = e.value.Close()
}
c.mu.Lock() c.mu.Lock()
e.setClosed() e.setClosed()
delete(c.data, e.id) delete(c.data, e.id)

View file

@ -140,24 +140,15 @@ func TestOCache_Get(t *testing.T) {
assert.Equal(t, context.Canceled, err) assert.Equal(t, context.Canceled, err)
assert.NoError(t, c.Close()) assert.NoError(t, c.Close())
}) })
t.Run("parallel load and remove", func(t *testing.T) { t.Run("value is nil", func(t *testing.T) {
var waitCh = make(chan struct{})
c := New(func(ctx context.Context, id string) (value Object, err error) { c := New(func(ctx context.Context, id string) (value Object, err error) {
return nil, nil return nil, nil
}) })
id := "id" value, err := c.Get(ctx, "id")
go func() { assert.NotNil(t, err)
val, err := c.Get(context.TODO(), id) assert.Nil(t, value)
close(waitCh) assert.NoError(t, c.Close())
require.NoError(t, err)
assert.Nil(t, val)
}()
<-waitCh
ok, err := c.Remove(context.TODO(), id)
require.NoError(t, err)
assert.True(t, ok)
}) })
} }