forked from grpc/grpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvoke_test.go
152 lines (138 loc) · 5.4 KB
/
invoke_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
*
* Copyright 2022 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package test
import (
"context"
"strings"
"testing"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/internal/stubserver"
testpb "google.golang.org/grpc/interop/grpc_testing"
"google.golang.org/grpc/status"
)
// TestInvoke verifies a straightforward invocation of ClientConn.Invoke().
func (s) TestInvoke(t *testing.T) {
ss := &stubserver.StubServer{
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { return &testpb.Empty{}, nil },
}
if err := ss.Start(nil); err != nil {
t.Fatalf("Failed to start stub server: %v", err)
}
defer ss.Stop()
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
if err := ss.CC.Invoke(ctx, "/grpc.testing.TestService/EmptyCall", &testpb.Empty{}, &testpb.Empty{}); err != nil {
t.Fatalf("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") failed: %v", err)
}
}
// TestInvokeLargeErr verifies an invocation of ClientConn.Invoke() where the
// server returns a really large error message.
func (s) TestInvokeLargeErr(t *testing.T) {
largeErrorStr := strings.Repeat("A", 1024*1024)
ss := &stubserver.StubServer{
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, status.Error(codes.Internal, largeErrorStr)
},
}
if err := ss.Start(nil); err != nil {
t.Fatalf("Failed to start stub server: %v", err)
}
defer ss.Stop()
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
err := ss.CC.Invoke(ctx, "/grpc.testing.TestService/EmptyCall", &testpb.Empty{}, &testpb.Empty{})
if err == nil {
t.Fatal("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") succeeded when expected to fail")
}
st, ok := status.FromError(err)
if !ok {
t.Fatal("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") received non-status error")
}
if status.Code(err) != codes.Internal || st.Message() != largeErrorStr {
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))
}
}
// TestInvokeErrorSpecialChars tests an invocation of ClientConn.Invoke() and
// verifies that error messages don't get mangled.
func (s) TestInvokeErrorSpecialChars(t *testing.T) {
const weirdError = "format verbs: %v%s"
ss := &stubserver.StubServer{
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, status.Error(codes.Internal, weirdError)
},
}
if err := ss.Start(nil); err != nil {
t.Fatalf("Failed to start stub server: %v", err)
}
defer ss.Stop()
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
err := ss.CC.Invoke(ctx, "/grpc.testing.TestService/EmptyCall", &testpb.Empty{}, &testpb.Empty{})
if err == nil {
t.Fatal("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") succeeded when expected to fail")
}
st, ok := status.FromError(err)
if !ok {
t.Fatal("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") received non-status error")
}
if status.Code(err) != codes.Internal || st.Message() != weirdError {
t.Fatalf("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") failed with error: %v, want %v", err, weirdError)
}
}
// TestInvokeCancel tests an invocation of ClientConn.Invoke() with a cancelled
// context and verifies that the request is not actually sent to the server.
func (s) TestInvokeCancel(t *testing.T) {
cancelled := 0
ss := &stubserver.StubServer{
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
cancelled++
return &testpb.Empty{}, nil
},
}
if err := ss.Start(nil); err != nil {
t.Fatalf("Failed to start stub server: %v", err)
}
defer ss.Stop()
for i := 0; i < 100; i++ {
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
cancel()
ss.CC.Invoke(ctx, "/grpc.testing.TestService/EmptyCall", &testpb.Empty{}, &testpb.Empty{})
}
if cancelled != 0 {
t.Fatalf("server received %d of 100 cancelled requests", cancelled)
}
}
// TestInvokeCancelClosedNonFail tests an invocation of ClientConn.Invoke() with
// a cancelled non-failfast RPC on a closed ClientConn and verifies that the
// call terminates with an error.
func (s) TestInvokeCancelClosedNonFailFast(t *testing.T) {
ss := &stubserver.StubServer{
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { return &testpb.Empty{}, nil },
}
if err := ss.Start(nil); err != nil {
t.Fatalf("Failed to start stub server: %v", err)
}
defer ss.Stop()
ss.CC.Close()
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
cancel()
if err := ss.CC.Invoke(ctx, "/grpc.testing.TestService/EmptyCall", &testpb.Empty{}, &testpb.Empty{}, grpc.WaitForReady(true)); err == nil {
t.Fatal("ClientConn.Invoke() on closed connection succeeded when expected to fail")
}
}