1
19
20 package test
21
22 import (
23 "context"
24 "strings"
25 "testing"
26
27 "google.golang.org/grpc"
28 "google.golang.org/grpc/codes"
29 "google.golang.org/grpc/internal/stubserver"
30 testpb "google.golang.org/grpc/interop/grpc_testing"
31 "google.golang.org/grpc/status"
32 )
33
34
35 func (s) TestInvoke(t *testing.T) {
36 ss := &stubserver.StubServer{
37 EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { return &testpb.Empty{}, nil },
38 }
39 if err := ss.Start(nil); err != nil {
40 t.Fatalf("Failed to start stub server: %v", err)
41 }
42 defer ss.Stop()
43
44 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
45 defer cancel()
46 if err := ss.CC.Invoke(ctx, "/grpc.testing.TestService/EmptyCall", &testpb.Empty{}, &testpb.Empty{}); err != nil {
47 t.Fatalf("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") failed: %v", err)
48 }
49 }
50
51
52
53 func (s) TestInvokeLargeErr(t *testing.T) {
54 largeErrorStr := strings.Repeat("A", 1024*1024)
55 ss := &stubserver.StubServer{
56 EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
57 return &testpb.Empty{}, status.Error(codes.Internal, largeErrorStr)
58 },
59 }
60 if err := ss.Start(nil); err != nil {
61 t.Fatalf("Failed to start stub server: %v", err)
62 }
63 defer ss.Stop()
64
65 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
66 defer cancel()
67 err := ss.CC.Invoke(ctx, "/grpc.testing.TestService/EmptyCall", &testpb.Empty{}, &testpb.Empty{})
68 if err == nil {
69 t.Fatal("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") succeeded when expected to fail")
70 }
71 st, ok := status.FromError(err)
72 if !ok {
73 t.Fatal("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") received non-status error")
74 }
75 if status.Code(err) != codes.Internal || st.Message() != largeErrorStr {
76 t.Fatalf("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") failed with error: %v, want an error of code %d and desc size %d", err, codes.Internal, len(largeErrorStr))
77 }
78 }
79
80
81
82 func (s) TestInvokeErrorSpecialChars(t *testing.T) {
83 const weirdError = "format verbs: %v%s"
84 ss := &stubserver.StubServer{
85 EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
86 return &testpb.Empty{}, status.Error(codes.Internal, weirdError)
87 },
88 }
89 if err := ss.Start(nil); err != nil {
90 t.Fatalf("Failed to start stub server: %v", err)
91 }
92 defer ss.Stop()
93
94 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
95 defer cancel()
96 err := ss.CC.Invoke(ctx, "/grpc.testing.TestService/EmptyCall", &testpb.Empty{}, &testpb.Empty{})
97 if err == nil {
98 t.Fatal("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") succeeded when expected to fail")
99 }
100 st, ok := status.FromError(err)
101 if !ok {
102 t.Fatal("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") received non-status error")
103 }
104 if status.Code(err) != codes.Internal || st.Message() != weirdError {
105 t.Fatalf("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") failed with error: %v, want %v", err, weirdError)
106 }
107 }
108
109
110
111 func (s) TestInvokeCancel(t *testing.T) {
112 cancelled := 0
113 ss := &stubserver.StubServer{
114 EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
115 cancelled++
116 return &testpb.Empty{}, nil
117 },
118 }
119 if err := ss.Start(nil); err != nil {
120 t.Fatalf("Failed to start stub server: %v", err)
121 }
122 defer ss.Stop()
123
124 for i := 0; i < 100; i++ {
125 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
126 cancel()
127 ss.CC.Invoke(ctx, "/grpc.testing.TestService/EmptyCall", &testpb.Empty{}, &testpb.Empty{})
128 }
129 if cancelled != 0 {
130 t.Fatalf("server received %d of 100 cancelled requests", cancelled)
131 }
132 }
133
134
135
136
137 func (s) TestInvokeCancelClosedNonFailFast(t *testing.T) {
138 ss := &stubserver.StubServer{
139 EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { return &testpb.Empty{}, nil },
140 }
141 if err := ss.Start(nil); err != nil {
142 t.Fatalf("Failed to start stub server: %v", err)
143 }
144 defer ss.Stop()
145
146 ss.CC.Close()
147 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
148 cancel()
149 if err := ss.CC.Invoke(ctx, "/grpc.testing.TestService/EmptyCall", &testpb.Empty{}, &testpb.Empty{}, grpc.WaitForReady(true)); err == nil {
150 t.Fatal("ClientConn.Invoke() on closed connection succeeded when expected to fail")
151 }
152 }
153
View as plain text