|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2024 gRPC authors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package grpc_test |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "strings" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + "google.golang.org/grpc" |
| 28 | + "google.golang.org/grpc/balancer" |
| 29 | + "google.golang.org/grpc/connectivity" |
| 30 | + "google.golang.org/grpc/credentials/insecure" |
| 31 | + "google.golang.org/grpc/internal/balancer/stub" |
| 32 | + "google.golang.org/grpc/internal/stubserver" |
| 33 | + testgrpc "google.golang.org/grpc/interop/grpc_testing" |
| 34 | +) |
| 35 | + |
| 36 | +type producerBuilder struct{} |
| 37 | + |
| 38 | +type producer struct { |
| 39 | + client testgrpc.TestServiceClient |
| 40 | + stopped chan struct{} |
| 41 | +} |
| 42 | + |
| 43 | +// Build constructs and returns a producer and its cleanup function |
| 44 | +func (*producerBuilder) Build(cci any) (balancer.Producer, func()) { |
| 45 | + p := &producer{ |
| 46 | + client: testgrpc.NewTestServiceClient(cci.(grpc.ClientConnInterface)), |
| 47 | + stopped: make(chan struct{}), |
| 48 | + } |
| 49 | + return p, func() { |
| 50 | + <-p.stopped |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func (p *producer) TestStreamStart(t *testing.T, streamStarted chan<- struct{}) { |
| 55 | + go func() { |
| 56 | + defer close(p.stopped) |
| 57 | + ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) |
| 58 | + defer cancel() |
| 59 | + if _, err := p.client.FullDuplexCall(ctx); err != nil { |
| 60 | + t.Errorf("Unexpected error starting stream: %v", err) |
| 61 | + } |
| 62 | + close(streamStarted) |
| 63 | + }() |
| 64 | +} |
| 65 | + |
| 66 | +var producerBuilderSingleton = &producerBuilder{} |
| 67 | + |
| 68 | +// TestProducerStreamStartsAfterReady ensures producer streams only start after |
| 69 | +// the subchannel reports as READY to the LB policy. |
| 70 | +func (s) TestProducerStreamStartsAfterReady(t *testing.T) { |
| 71 | + name := strings.ReplaceAll(strings.ToLower(t.Name()), "/", "") |
| 72 | + producerCh := make(chan balancer.Producer) |
| 73 | + streamStarted := make(chan struct{}) |
| 74 | + done := make(chan struct{}) |
| 75 | + bf := stub.BalancerFuncs{ |
| 76 | + UpdateClientConnState: func(bd *stub.BalancerData, ccs balancer.ClientConnState) error { |
| 77 | + sc, err := bd.ClientConn.NewSubConn(ccs.ResolverState.Addresses, balancer.NewSubConnOptions{ |
| 78 | + StateListener: func(scs balancer.SubConnState) { |
| 79 | + if scs.ConnectivityState == connectivity.Ready { |
| 80 | + timer := time.NewTimer(5 * time.Millisecond) |
| 81 | + select { |
| 82 | + case <-streamStarted: |
| 83 | + t.Errorf("Producer stream started before Ready listener returned") |
| 84 | + case <-timer.C: |
| 85 | + } |
| 86 | + close(done) |
| 87 | + } |
| 88 | + }, |
| 89 | + }) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + producer, _ := sc.GetOrBuildProducer(producerBuilderSingleton) |
| 94 | + producerCh <- producer |
| 95 | + sc.Connect() |
| 96 | + return nil |
| 97 | + }, |
| 98 | + } |
| 99 | + stub.Register(name, bf) |
| 100 | + |
| 101 | + ss := stubserver.StubServer{ |
| 102 | + FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error { |
| 103 | + return nil |
| 104 | + }, |
| 105 | + } |
| 106 | + if err := ss.StartServer(); err != nil { |
| 107 | + t.Fatal("Error starting server:", err) |
| 108 | + } |
| 109 | + defer ss.Stop() |
| 110 | + |
| 111 | + cc, err := grpc.NewClient("dns:///"+ss.Address, |
| 112 | + grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"`+name+`":{}}]}`), |
| 113 | + grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 114 | + ) |
| 115 | + if err != nil { |
| 116 | + t.Fatalf("Error creating client: %v", err) |
| 117 | + } |
| 118 | + defer cc.Close() |
| 119 | + |
| 120 | + go cc.Connect() |
| 121 | + p := <-producerCh |
| 122 | + p.(*producer).TestStreamStart(t, streamStarted) |
| 123 | + |
| 124 | + <-done |
| 125 | +} |
0 commit comments