Skip to content

Commit

Permalink
[FAB-9411] LazyRef finalizer should provide value
Browse files Browse the repository at this point in the history
Added the currently set value as an argument to
the finalizer.

Change-Id: I17c7e497415c7a084f5c0cae275ed399aa56cd32
Signed-off-by: Bob Stasyszyn <Bob.Stasyszyn@securekey.com>
  • Loading branch information
bstasyszyn committed Apr 8, 2018
1 parent db394dc commit fe28d90
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pkg/fabsdk/provider/fabpvdr/eventserviceref.go
Expand Up @@ -148,7 +148,7 @@ func (ref *EventClientRef) initializer() lazyref.Initializer {
}

func (ref *EventClientRef) finalizer() lazyref.Finalizer {
return func() {
return func(interface{}) {
logger.Debug("Finalizer called")
if ref.eventClient != nil {
if ref.Closed() {
Expand Down
13 changes: 9 additions & 4 deletions pkg/util/concurrent/lazyref/lazyref.go
Expand Up @@ -24,7 +24,7 @@ type Initializer func() (interface{}, error)

// Finalizer is a function that is called when the reference
// is closed
type Finalizer func()
type Finalizer func(value interface{})

// ExpirationProvider is a function that returns the
// expiration time of a reference
Expand Down Expand Up @@ -316,8 +316,12 @@ func (r *Reference) finalize() {
}

r.lock.Lock()
r.finalizer()
r.lock.Unlock()
defer r.lock.Unlock()

if r.isSet() {
value, _ := r.get()
r.finalizer(value)
}
}

func (r *Reference) handleExpiration() {
Expand All @@ -334,7 +338,8 @@ func (r *Reference) handleExpiration() {
// lock so there's no need to lock
func (r *Reference) resetValue() {
if r.finalizer != nil {
r.finalizer()
value, _ := r.get()
r.finalizer(value)
}
atomic.StorePointer(&r.ref, nil)
}
Expand Down
15 changes: 9 additions & 6 deletions pkg/util/concurrent/lazyref/lazyref_test.go
Expand Up @@ -12,6 +12,8 @@ import (
"sync/atomic"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func ExampleReference() {
Expand Down Expand Up @@ -169,9 +171,10 @@ func TestGetWithFinalizer(t *testing.T) {
return expectedValue, nil
},
WithFinalizer(
func() {
t.Logf("Finalizer called\n")
func(value interface{}) {
t.Logf("Finalizer called - value: %s\n", value)
atomic.AddInt32(&numTimesFinalized, 1)
assert.Equal(t, expectedValue, value, "got different value than expected in finalizer")
},
),
)
Expand Down Expand Up @@ -228,7 +231,7 @@ func TestExpiring(t *testing.T) {
return value, nil
},
WithFinalizer(
func() {
func(interface{}) {
atomic.AddInt32(&seq, 1)
atomic.AddInt32(&numTimesFinalized, 1)
},
Expand Down Expand Up @@ -299,7 +302,7 @@ func TestExpiringWithErr(t *testing.T) {
return value, nil
},
WithFinalizer(
func() {
func(interface{}) {
atomic.AddInt32(&numTimesFinalized, 1)
seq++
},
Expand Down Expand Up @@ -367,7 +370,7 @@ func TestExpiringOnIdle(t *testing.T) {
return value, nil
},
WithFinalizer(
func() {
func(interface{}) {
seq++
atomic.AddInt32(&numTimesFinalized, 1)
},
Expand Down Expand Up @@ -429,7 +432,7 @@ func TestProactiveRefresh(t *testing.T) {
return value, nil
},
WithFinalizer(
func() {
func(interface{}) {
atomic.AddInt32(&numTimesFinalized, 1)
t.Logf("Finalizer called")
},
Expand Down

0 comments on commit fe28d90

Please sign in to comment.