Skip to content

Commit

Permalink
helper function for unknown errors
Browse files Browse the repository at this point in the history
  • Loading branch information
purnesh42H committed Nov 21, 2024
1 parent 65d5760 commit 0e1bc31
Showing 1 changed file with 29 additions and 35 deletions.
64 changes: 29 additions & 35 deletions xds/internal/xdsclient/tests/lds_watchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ type listenerWatcherMultiple struct {
updateCh *testutils.Channel
}

// TODO: delete this once `newListenerWatcher` is modified to handle multiple
// updates.
func newListenerWatcherMultiple(size int) *listenerWatcherMultiple {
return &listenerWatcherMultiple{updateCh: testutils.NewChannelWithSize(size)}
}
Expand Down Expand Up @@ -178,6 +180,18 @@ func verifyListenerUpdate(ctx context.Context, updateCh *testutils.Channel, want
return nil
}

func verifyUnknownListenerError(ctx context.Context, updateCh *testutils.Channel, wantErr string) error {
u, err := updateCh.Receive(ctx)
if err != nil {
return fmt.Errorf("timeout when waiting for a listener error from the management server: %v", err)
}
gotErr := u.(listenerUpdateErrTuple).err
if gotErr == nil || !strings.Contains(gotErr.Error(), wantErr) {
return fmt.Errorf("update received with error: %v, want %q", gotErr, wantErr)
}
return nil
}

// TestLDSWatch covers the case where a single watcher exists for a single
// listener resource. The test verifies the following scenarios:
// 1. An update from the management server containing the resource being
Expand Down Expand Up @@ -1016,26 +1030,18 @@ func (s) TestLDSWatch_NACKError(t *testing.T) {
}

// Verify that the expected error is propagated to the watcher.
u, err := lw.updateCh.Receive(ctx)
if err != nil {
t.Fatalf("Timeout when waiting for a listener resource from the management server: %v", err)
}
gotErr := u.(listenerUpdateErrTuple).err
if gotErr == nil || !strings.Contains(gotErr.Error(), wantListenerNACKErr) {
t.Fatalf("Update received with error: %v, want %q", gotErr, wantListenerNACKErr)
// Verify that the expected error is propagated to the existing watcher.
if err := verifyUnknownListenerError(ctx, lw.updateCh, wantListenerNACKErr); err != nil {
t.Fatal(err)
}

// Verify that the expected error is propagated to the new watcher as well.
lw2 := newListenerWatcher()
ldsCancel2 := xdsresource.WatchListener(client, ldsName, lw2)
defer ldsCancel2()
u, err = lw2.updateCh.Receive(ctx)
if err != nil {
t.Fatalf("Timeout when waiting for a listener resource from the management server: %v", err)
}
gotErr = u.(listenerUpdateErrTuple).err
if gotErr == nil || !strings.Contains(gotErr.Error(), wantListenerNACKErr) {
t.Fatalf("Update received with error: %v, want %q", gotErr, wantListenerNACKErr)
// Verify that the expected error is propagated to the existing watcher.
if err := verifyUnknownListenerError(ctx, lw2.updateCh, wantListenerNACKErr); err != nil {
t.Fatal(err)
}
}

Expand Down Expand Up @@ -1128,13 +1134,8 @@ func TestLDSWatch_ResourceCaching_NACKError(t *testing.T) {
}

// Verify that the expected error is propagated to the existing watcher.
u, err := lw1.updateCh.Receive(ctx)
if err != nil {
t.Fatalf("timeout when waiting for a listener resource from the management server: %v", err)
}
gotErr := u.(listenerUpdateErrTuple).err
if gotErr == nil || !strings.Contains(gotErr.Error(), wantListenerNACKErr) {
t.Fatalf("Update received with error: %v, want %q", gotErr, wantListenerNACKErr)
if err := verifyUnknownListenerError(ctx, lw1.updateCh, wantListenerNACKErr); err != nil {
t.Fatal(err)
}

// Register another watch for the same resource. This should get the update
Expand All @@ -1145,14 +1146,11 @@ func TestLDSWatch_ResourceCaching_NACKError(t *testing.T) {
if err := verifyListenerUpdate(ctx, lw2.updateCh, wantUpdate); err != nil {
t.Fatal(err)
}
u, err = lw2.updateCh.Receive(ctx)
if err != nil {
t.Fatalf("Timeout when waiting for a listener resource from the management server: %v", err)
}
gotErr = u.(listenerUpdateErrTuple).err
if gotErr == nil || !strings.Contains(gotErr.Error(), wantListenerNACKErr) {
t.Fatalf("Update received with error: %v, want %q", gotErr, wantListenerNACKErr)
// Verify that the expected error is propagated to the existing watcher.
if err := verifyUnknownListenerError(ctx, lw2.updateCh, wantListenerNACKErr); err != nil {
t.Fatal(err)
}

// No request should get sent out as part of this watch.
sCtx, sCancel := context.WithTimeout(ctx, defaultTestShortTimeout)
defer sCancel()
Expand Down Expand Up @@ -1233,13 +1231,9 @@ func (s) TestLDSWatch_PartialValid(t *testing.T) {

// Verify that the expected error is propagated to the watcher which
// requested for the bad resource.
u, err := lw1.updateCh.Receive(ctx)
if err != nil {
t.Fatalf("timeout when waiting for a listener resource from the management server: %v", err)
}
gotErr := u.(listenerUpdateErrTuple).err
if gotErr == nil || !strings.Contains(gotErr.Error(), wantListenerNACKErr) {
t.Fatalf("update received with error: %v, want %q", gotErr, wantListenerNACKErr)
// Verify that the expected error is propagated to the existing watcher.
if err := verifyUnknownListenerError(ctx, lw1.updateCh, wantListenerNACKErr); err != nil {
t.Fatal(err)
}

// Verify that the watcher watching the good resource receives a good
Expand Down

0 comments on commit 0e1bc31

Please sign in to comment.