Skip to content

Commit

Permalink
snapshot cache: fix heartbeat panic (#579)
Browse files Browse the repository at this point in the history
* add failing test

Signed-off-by: Daniel Hochman <danielhochman@users.noreply.github.com>
  • Loading branch information
danielhochman authored Aug 8, 2022
1 parent 26c3efe commit 7857518
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/cache/v3/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ func NewSnapshotCacheWithHeartbeating(ctx context.Context, ads bool, hash NodeHa
}

func (cache *snapshotCache) sendHeartbeats(ctx context.Context, node string) {
snapshot := cache.snapshots[node]
snapshot, ok := cache.snapshots[node]
if !ok {
return
}

if info, ok := cache.status[node]; ok {
info.mu.Lock()
for id, watch := range info.watches {
Expand Down
31 changes: 31 additions & 0 deletions pkg/cache/v3/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,3 +604,34 @@ func TestSnapshotSingleResourceFetch(t *testing.T) {
protocmp.Transform()),
)
}

func TestAvertPanicForWatchOnNonExistentSnapshot(t *testing.T) {
ctx := context.Background()
c := cache.NewSnapshotCacheWithHeartbeating(ctx, false, cache.IDHash{}, nil, time.Millisecond)

// Create watch.
req := &cache.Request{
Node: &core.Node{Id: "test"},
ResourceNames: []string{"rtds"},
TypeUrl: rsrc.RuntimeType,
}
ss := stream.NewStreamState(false, map[string]string{"cluster": "abcdef"})
responder := make(chan cache.Response)
c.CreateWatch(req, ss, responder)

go func() {
// Wait for at least one heartbeat to occur, then set snapshot.
time.Sleep(time.Millisecond * 5)
srs := &singleResourceSnapshot{
version: "version-one",
typeurl: rsrc.RuntimeType,
name: "one-second",
resource: durationpb.New(time.Second),
}
if err := c.SetSnapshot(ctx, "test", srs); err != nil {
t.Errorf("unexpected error setting snapshot %v", err)
}
}()

<-responder
}

0 comments on commit 7857518

Please sign in to comment.