Skip to content

Commit

Permalink
Add standing unit test for #4 (#5)
Browse files Browse the repository at this point in the history
Just as a precautionary measure, this updates an existing unit test to
specifically check whether the fix introduced in #4 works as expected.
This has been tested by reverting #4 and checking that it does indeed
trigger the panic, and with #4 the panic is fixed.
  • Loading branch information
PapaCharlie authored Oct 7, 2024
1 parent f886eed commit 1d64b49
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,30 +338,36 @@ func TestNotifyMetadata(t *testing.T) {
// loop. The loop should abort and not call the remaining subscribers. It should instead restart and run through each
// subscriber with the updated value.
func TestWatchableValueUpdateCancel(t *testing.T) {
prefix := ads.XDSTPScheme + "/" + diderot.TypeOf[*Timestamp]().TrimmedURL() + "/foo/"

c := newCache()

r1 := newResource(name1, "0")
r1 := newResource(prefix+name1, "0")

var r1Wg, r2Wg sync.WaitGroup
r1Wg.Add(1)
r2Wg.Add(2)
r2Wg.Add(3)
notify := func(name string, r *ads.Resource[*Timestamp], _ ads.SubscriptionMetadata) {
// r is nil during the initial invocation of notify since the resource does not yet exist.
if r == nil {
return
}

if r == r1 {
c.Set(name1, "1", Now(), noTime)
c.Set(prefix+name1, "1", Now(), noTime)
// if notify is invoked with r1 more than once, this will panic
r1Wg.Done()
} else {
r2Wg.Done()
}
}

c.Subscribe(name1, testutils.NewSubscriptionHandler(notify))
c.Subscribe(name1, testutils.NewSubscriptionHandler(notify))
c.Subscribe(prefix+name1, testutils.NewSubscriptionHandler(notify))
// Use a glob and wildcard subscriptions, this flexes the code path in watchableValue that iterates
// through the various subscriber sets, and had a bug where breaking before exhausting the iterator
// would panic.
c.Subscribe(prefix+ads.WildcardSubscription, testutils.NewSubscriptionHandler(notify))
c.Subscribe(ads.WildcardSubscription, testutils.NewSubscriptionHandler(notify))

c.SetResource(r1, noTime)

Expand Down

0 comments on commit 1d64b49

Please sign in to comment.