func (s) TestLDSResourceRemoved(t *testing.T) {
	apiClientCh, cleanup := overrideNewAPIClient()
	defer cleanup()
	client, err := newWithConfig(clientOpts(testXDSServer, false))
	if err != nil {
		t.Fatalf("failed to create client: %v", err)
	}
	defer client.Close()
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()
	c, err := apiClientCh.Receive(ctx)
	if err != nil {
		t.Fatalf("timeout when waiting for API client to be created: %v", err)
	}
	apiClient := c.(*testAPIClient)
	ldsUpdateCh1 := testutils.NewChannel()
	client.WatchListener(testLDSName+"1", func(update ListenerUpdate, err error) {
		ldsUpdateCh1.Send(ListenerUpdateErrTuple{Update: update, Err: err})
	})
	if _, err := apiClient.addWatches[ListenerResource].Receive(ctx); err != nil {
		t.Fatalf("want new watch to start, got error %v", err)
	}
	ldsUpdateCh2 := testutils.NewChannel()
	client.WatchListener(testLDSName+"2", func(update ListenerUpdate, err error) {
		ldsUpdateCh2.Send(ListenerUpdateErrTuple{Update: update, Err: err})
	})
	if _, err := apiClient.addWatches[ListenerResource].Receive(ctx); err != nil {
		t.Fatalf("want new watch to start, got error %v", err)
	}
	wantUpdate1 := ListenerUpdate{RouteConfigName: testEDSName + "1"}
	wantUpdate2 := ListenerUpdate{RouteConfigName: testEDSName + "2"}
	client.NewListeners(map[string]ListenerUpdateErrTuple{
		testLDSName + "1": {Update: wantUpdate1},
		testLDSName + "2": {Update: wantUpdate2},
	}, UpdateMetadata{})
	if err := verifyListenerUpdate(ctx, ldsUpdateCh1, wantUpdate1, nil); err != nil {
		t.Fatal(err)
	}
	if err := verifyListenerUpdate(ctx, ldsUpdateCh2, wantUpdate2, nil); err != nil {
		t.Fatal(err)
	}
	client.NewListeners(map[string]ListenerUpdateErrTuple{testLDSName + "2": {Update: wantUpdate2}}, UpdateMetadata{})
	if u, err := ldsUpdateCh1.Receive(ctx); err != nil || ErrType(u.(ListenerUpdateErrTuple).Err) != ErrorTypeResourceNotFound {
		t.Errorf("unexpected ListenerUpdate: %v, error receiving from channel: %v, want update with error resource not found", u, err)
	}
	sCtx, sCancel := context.WithTimeout(ctx, defaultTestShortTimeout)
	defer sCancel()
	if u, err := ldsUpdateCh2.Receive(sCtx); err != context.DeadlineExceeded {
		t.Errorf("unexpected ListenerUpdate: %v, want receiving from channel timeout", u)
	}
	wantUpdate2 = ListenerUpdate{RouteConfigName: testEDSName + "2", Raw: &anypb.Any{}}
	client.NewListeners(map[string]ListenerUpdateErrTuple{testLDSName + "2": {Update: wantUpdate2}}, UpdateMetadata{})
	sCtx, sCancel = context.WithTimeout(ctx, defaultTestShortTimeout)
	defer sCancel()
	if u, err := ldsUpdateCh1.Receive(sCtx); err != context.DeadlineExceeded {
		t.Errorf("unexpected ListenerUpdate: %v, want receiving from channel timeout", u)
	}
	if err := verifyListenerUpdate(ctx, ldsUpdateCh2, wantUpdate2, nil); err != nil {
		t.Fatal(err)
	}
}
TestLDSResourceRemoved covers the cases:
- an update is received after a watch()
- another update is received, with one resource removed
- this should trigger callback with resource removed error
- one more update without the removed resource
- the callback (above) shouldn't receive any update
package xdsclient
import (
	"context"
	"fmt"
	"testing"
	v3listenerpb "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
	v3httppb "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3"
	"google.golang.org/grpc/internal/testutils"
	"google.golang.org/protobuf/types/known/anypb"
)
func (s) TestLDSWatch(t *testing.T) {
	apiClientCh, cleanup := overrideNewAPIClient()
	defer cleanup()
	client, err := newWithConfig(clientOpts(testXDSServer, false))
	if err != nil {
		t.Fatalf("failed to create client: %v", err)
	}
	defer client.Close()
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()
	c, err := apiClientCh.Receive(ctx)
	if err != nil {
		t.Fatalf("timeout when waiting for API client to be created: %v", err)
	}
	apiClient := c.(*testAPIClient)
	ldsUpdateCh := testutils.NewChannel()
	cancelWatch := client.WatchListener(testLDSName, func(update ListenerUpdate, err error) {
		ldsUpdateCh.Send(ListenerUpdateErrTuple{Update: update, Err: err})
	})
	if _, err := apiClient.addWatches[ListenerResource].Receive(ctx); err != nil {
		t.Fatalf("want new watch to start, got error %v", err)
	}
	wantUpdate := ListenerUpdate{RouteConfigName: testRDSName}
	client.NewListeners(map[string]ListenerUpdateErrTuple{testLDSName: {Update: wantUpdate}}, UpdateMetadata{})
	if err := verifyListenerUpdate(ctx, ldsUpdateCh, wantUpdate, nil); err != nil {
		t.Fatal(err)
	}
	newUpdate := ListenerUpdate{RouteConfigName: testRDSName, Raw: &anypb.Any{}}
	client.NewListeners(map[string]ListenerUpdateErrTuple{
		testLDSName:  {Update: newUpdate},
		"randomName": {},
	}, UpdateMetadata{})
	if err := verifyListenerUpdate(ctx, ldsUpdateCh, newUpdate, nil); err != nil {
		t.Fatal(err)
	}
	cancelWatch()
	client.NewListeners(map[string]ListenerUpdateErrTuple{testLDSName: {Update: wantUpdate}}, UpdateMetadata{})
	sCtx, sCancel := context.WithTimeout(ctx, defaultTestShortTimeout)
	defer sCancel()
	if u, err := ldsUpdateCh.Receive(sCtx); err != context.DeadlineExceeded {
		t.Fatalf("unexpected ListenerUpdate: %v, %v, want channel recv timeout", u, err)
	}
}
func (s) TestLDSTwoWatchSameResourceName(t *testing.T) {
	apiClientCh, cleanup := overrideNewAPIClient()
	defer cleanup()
	client, err := newWithConfig(clientOpts(testXDSServer, false))
	if err != nil {
		t.Fatalf("failed to create client: %v", err)
	}
	defer client.Close()
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()
	c, err := apiClientCh.Receive(ctx)
	if err != nil {
		t.Fatalf("timeout when waiting for API client to be created: %v", err)
	}
	apiClient := c.(*testAPIClient)
	const count = 2
	var (
		ldsUpdateChs    []*testutils.Channel
		cancelLastWatch func()
	)
	for i := 0; i < count; i++ {
		ldsUpdateCh := testutils.NewChannel()
		ldsUpdateChs = append(ldsUpdateChs, ldsUpdateCh)
		cancelLastWatch = client.WatchListener(testLDSName, func(update ListenerUpdate, err error) {
			ldsUpdateCh.Send(ListenerUpdateErrTuple{Update: update, Err: err})
		})
		if i == 0 {
			if _, err := apiClient.addWatches[ListenerResource].Receive(ctx); err != nil {
				t.Fatalf("want new watch to start, got error %v", err)
			}
		}
	}
	wantUpdate := ListenerUpdate{RouteConfigName: testRDSName}
	client.NewListeners(map[string]ListenerUpdateErrTuple{testLDSName: {Update: wantUpdate}}, UpdateMetadata{})
	for i := 0; i < count; i++ {
		if err := verifyListenerUpdate(ctx, ldsUpdateChs[i], wantUpdate, nil); err != nil {
			t.Fatal(err)
		}
	}
	cancelLastWatch()
	client.NewListeners(map[string]ListenerUpdateErrTuple{testLDSName: {Update: wantUpdate}}, UpdateMetadata{})
	for i := 0; i < count; i++ {
		func() {
			sCtx, sCancel := context.WithTimeout(ctx, defaultTestShortTimeout)
			defer sCancel()
			if u, err := ldsUpdateChs[i].Receive(sCtx); err != context.DeadlineExceeded {
				t.Errorf("unexpected ListenerUpdate: %v, %v, want channel recv timeout", u, err)
			}
		}()
	}
	newUpdate := ListenerUpdate{RouteConfigName: testRDSName, Raw: &anypb.Any{}}
	client.NewListeners(map[string]ListenerUpdateErrTuple{testLDSName: {Update: newUpdate}}, UpdateMetadata{})
	if err := verifyListenerUpdate(ctx, ldsUpdateChs[0], newUpdate, nil); err != nil {
		t.Fatal(err)
	}
}
func (s) TestLDSThreeWatchDifferentResourceName(t *testing.T) {
	apiClientCh, cleanup := overrideNewAPIClient()
	defer cleanup()
	client, err := newWithConfig(clientOpts(testXDSServer, false))
	if err != nil {
		t.Fatalf("failed to create client: %v", err)
	}
	defer client.Close()
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()
	c, err := apiClientCh.Receive(ctx)
	if err != nil {
		t.Fatalf("timeout when waiting for API client to be created: %v", err)
	}
	apiClient := c.(*testAPIClient)
	var ldsUpdateChs []*testutils.Channel
	const count = 2
	for i := 0; i < count; i++ {
		ldsUpdateCh := testutils.NewChannel()
		ldsUpdateChs = append(ldsUpdateChs, ldsUpdateCh)
		client.WatchListener(testLDSName+"1", func(update ListenerUpdate, err error) {
			ldsUpdateCh.Send(ListenerUpdateErrTuple{Update: update, Err: err})
		})
		if i == 0 {
			if _, err := apiClient.addWatches[ListenerResource].Receive(ctx); err != nil {
				t.Fatalf("want new watch to start, got error %v", err)
			}
		}
	}
	ldsUpdateCh2 := testutils.NewChannel()
	client.WatchListener(testLDSName+"2", func(update ListenerUpdate, err error) {
		ldsUpdateCh2.Send(ListenerUpdateErrTuple{Update: update, Err: err})
	})
	if _, err := apiClient.addWatches[ListenerResource].Receive(ctx); err != nil {
		t.Fatalf("want new watch to start, got error %v", err)
	}
	wantUpdate1 := ListenerUpdate{RouteConfigName: testRDSName + "1"}
	wantUpdate2 := ListenerUpdate{RouteConfigName: testRDSName + "2"}
	client.NewListeners(map[string]ListenerUpdateErrTuple{
		testLDSName + "1": {Update: wantUpdate1},
		testLDSName + "2": {Update: wantUpdate2},
	}, UpdateMetadata{})
	for i := 0; i < count; i++ {
		if err := verifyListenerUpdate(ctx, ldsUpdateChs[i], wantUpdate1, nil); err != nil {
			t.Fatal(err)
		}
	}
	if err := verifyListenerUpdate(ctx, ldsUpdateCh2, wantUpdate2, nil); err != nil {
		t.Fatal(err)
	}
}
func (s) TestLDSWatchAfterCache(t *testing.T) {
	apiClientCh, cleanup := overrideNewAPIClient()
	defer cleanup()
	client, err := newWithConfig(clientOpts(testXDSServer, false))
	if err != nil {
		t.Fatalf("failed to create client: %v", err)
	}
	defer client.Close()
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()
	c, err := apiClientCh.Receive(ctx)
	if err != nil {
		t.Fatalf("timeout when waiting for API client to be created: %v", err)
	}
	apiClient := c.(*testAPIClient)
	ldsUpdateCh := testutils.NewChannel()
	client.WatchListener(testLDSName, func(update ListenerUpdate, err error) {
		ldsUpdateCh.Send(ListenerUpdateErrTuple{Update: update, Err: err})
	})
	if _, err := apiClient.addWatches[ListenerResource].Receive(ctx); err != nil {
		t.Fatalf("want new watch to start, got error %v", err)
	}
	wantUpdate := ListenerUpdate{RouteConfigName: testRDSName}
	client.NewListeners(map[string]ListenerUpdateErrTuple{testLDSName: {Update: wantUpdate}}, UpdateMetadata{})
	if err := verifyListenerUpdate(ctx, ldsUpdateCh, wantUpdate, nil); err != nil {
		t.Fatal(err)
	}
	ldsUpdateCh2 := testutils.NewChannel()
	client.WatchListener(testLDSName, func(update ListenerUpdate, err error) {
		ldsUpdateCh2.Send(ListenerUpdateErrTuple{Update: update, Err: err})
	})
	sCtx, sCancel := context.WithTimeout(ctx, defaultTestShortTimeout)
	defer sCancel()
	if n, err := apiClient.addWatches[ListenerResource].Receive(sCtx); err != context.DeadlineExceeded {
		t.Fatalf("want no new watch to start (recv timeout), got resource name: %v error %v", n, err)
	}
	if err := verifyListenerUpdate(ctx, ldsUpdateCh2, wantUpdate, nil); err != nil {
		t.Fatal(err)
	}
	sCtx, sCancel = context.WithTimeout(ctx, defaultTestShortTimeout)
	defer sCancel()
	if u, err := ldsUpdateCh.Receive(sCtx); err != context.DeadlineExceeded {
		t.Errorf("unexpected ListenerUpdate: %v, %v, want channel recv timeout", u, err)
	}
}