Skip to content

Commit

Permalink
bugfix: cache get function, when in pin mode, should not increase the…
Browse files Browse the repository at this point in the history
… counter before expiration check (cadence-workflow#665)
  • Loading branch information
wxing1292 authored Apr 13, 2018
1 parent df48665 commit 1bbf59c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
7 changes: 3 additions & 4 deletions common/cache/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,15 @@ func (c *lru) Get(key interface{}) interface{} {

entry := element.Value.(*entryImpl)

if c.pin {
entry.refCount++
}

if c.isEntryExpired(entry, time.Now()) {
// Entry has expired
c.deleteInternal(element)
return nil
}

if c.pin {
entry.refCount++
}
c.byAccess.MoveToFront(element)
return entry.value
}
Expand Down
31 changes: 31 additions & 0 deletions common/cache/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,37 @@ func TestRemovedFuncWithTTL(t *testing.T) {
}
}

func TestRemovedFuncWithTTL_Pin(t *testing.T) {
ch := make(chan bool)
cache := New(5, &Options{
TTL: time.Millisecond * 50,
Pin: true,
RemovedFunc: func(i interface{}) {
_, ok := i.(*testing.T)
assert.True(t, ok)
ch <- true
},
})

cache.PutIfNotExist("A", t)
assert.Equal(t, t, cache.Get("A"))
time.Sleep(time.Millisecond * 100)
assert.Equal(t, t, cache.Get("A"))
// release 3 time since put if not exist also increase the counter
cache.Release("A")
cache.Release("A")
cache.Release("A")
assert.Nil(t, cache.Get("A"))

timeout := time.NewTimer(time.Millisecond * 300)
select {
case b := <-ch:
assert.True(t, b)
case <-timeout.C:
t.Error("RemovedFunc did not send true on channel ch")
}
}

func TestIterator(t *testing.T) {
expected := map[string]string{
"A": "Alpha",
Expand Down

0 comments on commit 1bbf59c

Please sign in to comment.