Ocache fix tests and return deadline back

This commit is contained in:
mcrakhman 2022-10-21 11:21:20 +02:00 committed by Mikhail Iudin
parent 27ff00eb48
commit 8c5ac1337a
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
2 changed files with 4 additions and 4 deletions

View File

@ -356,6 +356,7 @@ func (c *oCache) GC() {
c.mu.Unlock()
return
}
deadline := c.timeNow().Add(-c.ttl)
var toClose []*entry
for _, e := range c.data {
if e.isClosing {
@ -365,8 +366,7 @@ func (c *oCache) GC() {
if lug, ok := e.value.(ObjectLastUsage); ok {
lu = lug.LastUsage()
}
deadline := lu.Add(c.ttl)
if !e.locked() && e.refCount <= 0 && c.timeNow().After(deadline) {
if !e.locked() && e.refCount <= 0 && lu.Before(deadline) {
e.isClosing = true
e.close = make(chan struct{})
toClose = append(toClose, e)

View File

@ -119,7 +119,7 @@ func TestOCache_GC(t *testing.T) {
t.Run("test without close wait", func(t *testing.T) {
c := New(func(ctx context.Context, id string) (value Object, err error) {
return &testObject{name: id}, nil
}, WithTTL(time.Millisecond*10))
}, WithTTL(time.Millisecond*10), WithRefCounter(true))
val, err := c.Get(context.TODO(), "id")
require.NoError(t, err)
require.NotNil(t, val)
@ -140,7 +140,7 @@ func TestOCache_GC(t *testing.T) {
c := New(func(ctx context.Context, id string) (value Object, err error) {
return NewTestObject(id, closeCh), nil
}, WithTTL(time.Millisecond*10))
}, WithTTL(time.Millisecond*10), WithRefCounter(true))
val, err := c.Get(context.TODO(), "id")
require.NoError(t, err)
require.NotNil(t, val)