Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 3e7d1d1

Browse files
troyrondaGerrit Code Review
authored andcommitted
Merge "[FAB-9411] LazyRef finalizer should provide value"
2 parents fda0d9a + fe28d90 commit 3e7d1d1

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

pkg/fabsdk/provider/fabpvdr/eventserviceref.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (ref *EventClientRef) initializer() lazyref.Initializer {
148148
}
149149

150150
func (ref *EventClientRef) finalizer() lazyref.Finalizer {
151-
return func() {
151+
return func(interface{}) {
152152
logger.Debug("Finalizer called")
153153
if ref.eventClient != nil {
154154
if ref.Closed() {

pkg/util/concurrent/lazyref/lazyref.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Initializer func() (interface{}, error)
2424

2525
// Finalizer is a function that is called when the reference
2626
// is closed
27-
type Finalizer func()
27+
type Finalizer func(value interface{})
2828

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

318318
r.lock.Lock()
319-
r.finalizer()
320-
r.lock.Unlock()
319+
defer r.lock.Unlock()
320+
321+
if r.isSet() {
322+
value, _ := r.get()
323+
r.finalizer(value)
324+
}
321325
}
322326

323327
func (r *Reference) handleExpiration() {
@@ -334,7 +338,8 @@ func (r *Reference) handleExpiration() {
334338
// lock so there's no need to lock
335339
func (r *Reference) resetValue() {
336340
if r.finalizer != nil {
337-
r.finalizer()
341+
value, _ := r.get()
342+
r.finalizer(value)
338343
}
339344
atomic.StorePointer(&r.ref, nil)
340345
}

pkg/util/concurrent/lazyref/lazyref_test.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"sync/atomic"
1313
"testing"
1414
"time"
15+
16+
"github.com/stretchr/testify/assert"
1517
)
1618

1719
func ExampleReference() {
@@ -169,9 +171,10 @@ func TestGetWithFinalizer(t *testing.T) {
169171
return expectedValue, nil
170172
},
171173
WithFinalizer(
172-
func() {
173-
t.Logf("Finalizer called\n")
174+
func(value interface{}) {
175+
t.Logf("Finalizer called - value: %s\n", value)
174176
atomic.AddInt32(&numTimesFinalized, 1)
177+
assert.Equal(t, expectedValue, value, "got different value than expected in finalizer")
175178
},
176179
),
177180
)
@@ -228,7 +231,7 @@ func TestExpiring(t *testing.T) {
228231
return value, nil
229232
},
230233
WithFinalizer(
231-
func() {
234+
func(interface{}) {
232235
atomic.AddInt32(&seq, 1)
233236
atomic.AddInt32(&numTimesFinalized, 1)
234237
},
@@ -299,7 +302,7 @@ func TestExpiringWithErr(t *testing.T) {
299302
return value, nil
300303
},
301304
WithFinalizer(
302-
func() {
305+
func(interface{}) {
303306
atomic.AddInt32(&numTimesFinalized, 1)
304307
seq++
305308
},
@@ -367,7 +370,7 @@ func TestExpiringOnIdle(t *testing.T) {
367370
return value, nil
368371
},
369372
WithFinalizer(
370-
func() {
373+
func(interface{}) {
371374
seq++
372375
atomic.AddInt32(&numTimesFinalized, 1)
373376
},
@@ -429,7 +432,7 @@ func TestProactiveRefresh(t *testing.T) {
429432
return value, nil
430433
},
431434
WithFinalizer(
432-
func() {
435+
func(interface{}) {
433436
atomic.AddInt32(&numTimesFinalized, 1)
434437
t.Logf("Finalizer called")
435438
},

0 commit comments

Comments
 (0)