...
1
18
19 package test
20
21 import (
22 "context"
23 "fmt"
24 "strings"
25 "testing"
26 "time"
27
28 "google.golang.org/grpc"
29 "google.golang.org/grpc/codes"
30 "google.golang.org/grpc/credentials/insecure"
31 "google.golang.org/grpc/internal/channelz"
32 testgrpc "google.golang.org/grpc/interop/grpc_testing"
33 testpb "google.golang.org/grpc/interop/grpc_testing"
34 "google.golang.org/grpc/resolver/manual"
35 "google.golang.org/grpc/status"
36 )
37
38
39
40
41
42 func (s) TestClientConnClose_WithPendingRPC(t *testing.T) {
43 r := manual.NewBuilderWithScheme("whatever")
44 cc, err := grpc.NewClient(r.Scheme()+":///test.server", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(r))
45 if err != nil {
46 t.Fatalf("grpc.NewClient() failed: %v", err)
47 }
48 client := testgrpc.NewTestServiceClient(cc)
49
50 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
51 defer cancel()
52 doneErrCh := make(chan error, 1)
53 go func() {
54
55
56 _, err := client.EmptyCall(ctx, &testpb.Empty{})
57 if status.Code(err) != codes.Canceled || !strings.Contains(err.Error(), "client connection is closing") {
58 doneErrCh <- fmt.Errorf("EmptyCall() = %v, want %s", err, codes.Canceled)
59 }
60 doneErrCh <- nil
61 }()
62
63
64
65
66 for {
67 if err := ctx.Err(); err != nil {
68 t.Fatal(err)
69 }
70 tcs, _ := channelz.GetTopChannels(0, 0)
71 if len(tcs) != 1 {
72 t.Fatalf("there should only be one top channel, not %d", len(tcs))
73 }
74 started := tcs[0].ChannelMetrics.CallsStarted.Load()
75 completed := tcs[0].ChannelMetrics.CallsSucceeded.Load() + tcs[0].ChannelMetrics.CallsFailed.Load()
76 if (started - completed) == 1 {
77 break
78 }
79 time.Sleep(defaultTestShortTimeout)
80 }
81 cc.Close()
82 if err := <-doneErrCh; err != nil {
83 t.Fatal(err)
84 }
85 }
86
View as plain text