MCPcopy
hub / github.com/grpc/grpc-go / testStateTransitionSingleAddress

Function testStateTransitionSingleAddress

test/clientconn_state_transition_test.go:176–233  ·  view source on GitHub ↗
(t *testing.T, wantStates []connectivity.State, server func(net.Listener) net.Conn)

Source from the content-addressed store, hash-verified

174}
175
176func testStateTransitionSingleAddress(t *testing.T, wantStates []connectivity.State, server func(net.Listener) net.Conn) {
177 pl := testutils.NewPipeListener()
178 defer pl.Close()
179
180 // Launch the server.
181 var conn net.Conn
182 var connMu sync.Mutex
183 go func() {
184 connMu.Lock()
185 conn = server(pl)
186 connMu.Unlock()
187 }()
188
189 dopts := []grpc.DialOption{
190 grpc.WithTransportCredentials(insecure.NewCredentials()),
191 grpc.WithDialer(pl.Dialer()),
192 grpc.WithConnectParams(grpc.ConnectParams{
193 Backoff: backoff.Config{},
194 MinConnectTimeout: 100 * time.Millisecond,
195 }),
196 }
197 cc, err := grpc.NewClient("passthrough:///", dopts...)
198 if err != nil {
199 t.Fatal(err)
200 }
201 defer cc.Close()
202
203 // Ensure that the client is in IDLE before connecting.
204 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
205 defer cancel()
206 testutils.AwaitState(ctx, t, cc, connectivity.Idle)
207
208 // Subscribe to state updates.
209 stateCh := make(chan connectivity.State, 1)
210 s := &funcConnectivityStateSubscriber{
211 onMsg: func(s connectivity.State) {
212 select {
213 case stateCh <- s:
214 case <-ctx.Done():
215 }
216 },
217 }
218 internal.SubscribeToConnectivityStateChanges.(func(cc *grpc.ClientConn, s grpcsync.Subscriber) func())(cc, s)
219
220 cc.Connect()
221 for _, wantState := range wantStates {
222 waitForState(ctx, t, stateCh, wantState)
223 }
224
225 connMu.Lock()
226 defer connMu.Unlock()
227 if conn != nil {
228 err = conn.Close()
229 if err != nil {
230 t.Fatal(err)
231 }
232 }
233}

Callers 1

Calls 15

CloseMethod · 0.95
DialerMethod · 0.95
NewPipeListenerFunction · 0.92
WithTransportCredentialsFunction · 0.92
NewCredentialsFunction · 0.92
WithDialerFunction · 0.92
WithConnectParamsFunction · 0.92
NewClientFunction · 0.92
AwaitStateFunction · 0.92
waitForStateFunction · 0.85
FatalMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected