xds: work around xdsclient race in fault injection test (#4377)

This commit is contained in:
Doug Fawley 2021-05-04 14:51:32 -07:00 коммит произвёл GitHub
Родитель 75497df97f
Коммит 11bd77660d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 30 добавлений и 30 удалений

Просмотреть файл

@ -465,15 +465,8 @@ func (s) TestFaultInjection_Unary(t *testing.T) {
fs, nodeID, port, cleanup := clientSetup(t) fs, nodeID, port, cleanup := clientSetup(t)
defer cleanup() defer cleanup()
resources := e2e.DefaultClientResources("myservice", nodeID, "localhost", port)
hcm := new(v3httppb.HttpConnectionManager)
err := ptypes.UnmarshalAny(resources.Listeners[0].GetApiListener().GetApiListener(), hcm)
if err != nil {
t.Fatal(err)
}
routerFilter := hcm.HttpFilters[len(hcm.HttpFilters)-1]
for _, tc := range testCases { for tcNum, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
defer func() { randIntn = grpcrand.Intn; newTimer = time.NewTimer }() defer func() { randIntn = grpcrand.Intn; newTimer = time.NewTimer }()
var intnCalls []int var intnCalls []int
@ -489,6 +482,15 @@ func (s) TestFaultInjection_Unary(t *testing.T) {
return time.NewTimer(0) return time.NewTimer(0)
} }
serviceName := fmt.Sprintf("myservice%d", tcNum)
resources := e2e.DefaultClientResources(serviceName, nodeID, "localhost", port)
hcm := new(v3httppb.HttpConnectionManager)
err := ptypes.UnmarshalAny(resources.Listeners[0].GetApiListener().GetApiListener(), hcm)
if err != nil {
t.Fatal(err)
}
routerFilter := hcm.HttpFilters[len(hcm.HttpFilters)-1]
hcm.HttpFilters = nil hcm.HttpFilters = nil
for i, cfg := range tc.cfgs { for i, cfg := range tc.cfgs {
hcm.HttpFilters = append(hcm.HttpFilters, e2e.HTTPFilter(fmt.Sprintf("fault%d", i), cfg)) hcm.HttpFilters = append(hcm.HttpFilters, e2e.HTTPFilter(fmt.Sprintf("fault%d", i), cfg))
@ -506,7 +508,7 @@ func (s) TestFaultInjection_Unary(t *testing.T) {
} }
// Create a ClientConn and run the test case. // Create a ClientConn and run the test case.
cc, err := grpc.Dial("xds:///myservice", grpc.WithTransportCredentials(insecure.NewCredentials())) cc, err := grpc.Dial("xds:///"+serviceName, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil { if err != nil {
t.Fatalf("failed to dial local test server: %v", err) t.Fatalf("failed to dial local test server: %v", err)
} }
@ -515,25 +517,23 @@ func (s) TestFaultInjection_Unary(t *testing.T) {
client := testpb.NewTestServiceClient(cc) client := testpb.NewTestServiceClient(cc)
count := 0 count := 0
for _, want := range tc.want { for _, want := range tc.want {
t.Run(want.name, func(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel()
defer cancel() if want.repeat == 0 {
if want.repeat == 0 { t.Fatalf("invalid repeat count")
t.Fatalf("invalid repeat count") }
for n := 0; n < want.repeat; n++ {
intnCalls = nil
newTimerCalls = nil
ctx = metadata.NewOutgoingContext(ctx, want.md)
_, err := client.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true))
t.Logf("%v: RPC %d: err: %v, intnCalls: %v, newTimerCalls: %v", want.name, count, err, intnCalls, newTimerCalls)
if status.Code(err) != want.code || !reflect.DeepEqual(intnCalls, want.randIn) || !reflect.DeepEqual(newTimerCalls, want.delays) {
t.Fatalf("WANTED code: %v, intnCalls: %v, newTimerCalls: %v", want.code, want.randIn, want.delays)
} }
for n := 0; n < want.repeat; n++ { randOut += tc.randOutInc
intnCalls = nil count++
newTimerCalls = nil }
ctx = metadata.NewOutgoingContext(ctx, want.md)
_, err := client.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true))
t.Logf("RPC %d: err: %v, intnCalls: %v, newTimerCalls: %v", count, err, intnCalls, newTimerCalls)
if status.Code(err) != want.code || !reflect.DeepEqual(intnCalls, want.randIn) || !reflect.DeepEqual(newTimerCalls, want.delays) {
t.Errorf("WANTED code: %v, intnCalls: %v, newTimerCalls: %v", want.code, want.randIn, want.delays)
}
randOut += tc.randOutInc
count++
}
})
} }
}) })
} }

Просмотреть файл

@ -45,9 +45,9 @@ func any(m proto.Message) *anypb.Any {
// DefaultClientResources returns a set of resources (LDS, RDS, CDS, EDS) for a // DefaultClientResources returns a set of resources (LDS, RDS, CDS, EDS) for a
// client to generically connect to one server. // client to generically connect to one server.
func DefaultClientResources(target, nodeID, host string, port uint32) UpdateOptions { func DefaultClientResources(target, nodeID, host string, port uint32) UpdateOptions {
const routeConfigName = "route" routeConfigName := "route-" + target
const clusterName = "cluster" clusterName := "cluster-" + target
const endpointsName = "endpoints" endpointsName := "endpoints-" + target
return UpdateOptions{ return UpdateOptions{
NodeID: nodeID, NodeID: nodeID,