Skip to content

Commit 09792b5

Browse files
authored
test: move codec tests to a separate file (#6663)
1 parent 57cb4d8 commit 09792b5

File tree

2 files changed

+188
-151
lines changed

2 files changed

+188
-151
lines changed

test/codec_test.go

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/*
2+
*
3+
* Copyright 2023 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 test
20+
21+
import (
22+
"context"
23+
"fmt"
24+
"reflect"
25+
"sync/atomic"
26+
"testing"
27+
28+
"google.golang.org/grpc"
29+
"google.golang.org/grpc/codes"
30+
"google.golang.org/grpc/encoding"
31+
"google.golang.org/grpc/internal/stubserver"
32+
testgrpc "google.golang.org/grpc/interop/grpc_testing"
33+
testpb "google.golang.org/grpc/interop/grpc_testing"
34+
"google.golang.org/grpc/metadata"
35+
"google.golang.org/grpc/status"
36+
"google.golang.org/protobuf/proto"
37+
)
38+
39+
type errCodec struct {
40+
noError bool
41+
}
42+
43+
func (c *errCodec) Marshal(v any) ([]byte, error) {
44+
if c.noError {
45+
return []byte{}, nil
46+
}
47+
return nil, fmt.Errorf("3987^12 + 4365^12 = 4472^12")
48+
}
49+
50+
func (c *errCodec) Unmarshal(data []byte, v any) error {
51+
return nil
52+
}
53+
54+
func (c *errCodec) Name() string {
55+
return "Fermat's near-miss."
56+
}
57+
58+
func (s) TestEncodeDoesntPanic(t *testing.T) {
59+
for _, e := range listTestEnv() {
60+
testEncodeDoesntPanic(t, e)
61+
}
62+
}
63+
64+
func testEncodeDoesntPanic(t *testing.T, e env) {
65+
te := newTest(t, e)
66+
erc := &errCodec{}
67+
te.customCodec = erc
68+
te.startServer(&testServer{security: e.security})
69+
defer te.tearDown()
70+
te.customCodec = nil
71+
tc := testgrpc.NewTestServiceClient(te.clientConn())
72+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
73+
defer cancel()
74+
// Failure case, should not panic.
75+
tc.EmptyCall(ctx, &testpb.Empty{})
76+
erc.noError = true
77+
// Passing case.
78+
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
79+
t.Fatalf("EmptyCall(_, _) = _, %v, want _, <nil>", err)
80+
}
81+
}
82+
83+
type countingProtoCodec struct {
84+
marshalCount int32
85+
unmarshalCount int32
86+
}
87+
88+
func (p *countingProtoCodec) Marshal(v any) ([]byte, error) {
89+
atomic.AddInt32(&p.marshalCount, 1)
90+
vv, ok := v.(proto.Message)
91+
if !ok {
92+
return nil, fmt.Errorf("failed to marshal, message is %T, want proto.Message", v)
93+
}
94+
return proto.Marshal(vv)
95+
}
96+
97+
func (p *countingProtoCodec) Unmarshal(data []byte, v any) error {
98+
atomic.AddInt32(&p.unmarshalCount, 1)
99+
vv, ok := v.(proto.Message)
100+
if !ok {
101+
return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v)
102+
}
103+
return proto.Unmarshal(data, vv)
104+
}
105+
106+
func (*countingProtoCodec) Name() string {
107+
return "proto"
108+
}
109+
110+
func (s) TestForceServerCodec(t *testing.T) {
111+
ss := &stubserver.StubServer{
112+
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
113+
return &testpb.Empty{}, nil
114+
},
115+
}
116+
codec := &countingProtoCodec{}
117+
if err := ss.Start([]grpc.ServerOption{grpc.ForceServerCodec(codec)}); err != nil {
118+
t.Fatalf("Error starting endpoint server: %v", err)
119+
}
120+
defer ss.Stop()
121+
122+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
123+
defer cancel()
124+
125+
if _, err := ss.Client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
126+
t.Fatalf("ss.Client.EmptyCall(_, _) = _, %v; want _, nil", err)
127+
}
128+
129+
unmarshalCount := atomic.LoadInt32(&codec.unmarshalCount)
130+
const wantUnmarshalCount = 1
131+
if unmarshalCount != wantUnmarshalCount {
132+
t.Fatalf("protoCodec.unmarshalCount = %d; want %d", unmarshalCount, wantUnmarshalCount)
133+
}
134+
marshalCount := atomic.LoadInt32(&codec.marshalCount)
135+
const wantMarshalCount = 1
136+
if marshalCount != wantMarshalCount {
137+
t.Fatalf("protoCodec.marshalCount = %d; want %d", marshalCount, wantMarshalCount)
138+
}
139+
}
140+
141+
// renameProtoCodec is an encoding.Codec wrapper that allows customizing the
142+
// Name() of another codec.
143+
type renameProtoCodec struct {
144+
encoding.Codec
145+
name string
146+
}
147+
148+
func (r *renameProtoCodec) Name() string { return r.name }
149+
150+
// TestForceCodecName confirms that the ForceCodec call option sets the subtype
151+
// in the content-type header according to the Name() of the codec provided.
152+
func (s) TestForceCodecName(t *testing.T) {
153+
wantContentTypeCh := make(chan []string, 1)
154+
defer close(wantContentTypeCh)
155+
156+
ss := &stubserver.StubServer{
157+
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
158+
md, ok := metadata.FromIncomingContext(ctx)
159+
if !ok {
160+
return nil, status.Errorf(codes.Internal, "no metadata in context")
161+
}
162+
if got, want := md["content-type"], <-wantContentTypeCh; !reflect.DeepEqual(got, want) {
163+
return nil, status.Errorf(codes.Internal, "got content-type=%q; want [%q]", got, want)
164+
}
165+
return &testpb.Empty{}, nil
166+
},
167+
}
168+
if err := ss.Start([]grpc.ServerOption{grpc.ForceServerCodec(encoding.GetCodec("proto"))}); err != nil {
169+
t.Fatalf("Error starting endpoint server: %v", err)
170+
}
171+
defer ss.Stop()
172+
173+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
174+
defer cancel()
175+
176+
codec := &renameProtoCodec{Codec: encoding.GetCodec("proto"), name: "some-test-name"}
177+
wantContentTypeCh <- []string{"application/grpc+some-test-name"}
178+
if _, err := ss.Client.EmptyCall(ctx, &testpb.Empty{}, grpc.ForceCodec(codec)); err != nil {
179+
t.Fatalf("ss.Client.EmptyCall(_, _) = _, %v; want _, nil", err)
180+
}
181+
182+
// Confirm the name is converted to lowercase before transmitting.
183+
codec.name = "aNoTHeRNaME"
184+
wantContentTypeCh <- []string{"application/grpc+anothername"}
185+
if _, err := ss.Client.EmptyCall(ctx, &testpb.Empty{}, grpc.ForceCodec(codec)); err != nil {
186+
t.Fatalf("ss.Client.EmptyCall(_, _) = _, %v; want _, nil", err)
187+
}
188+
}

test/end2end_test.go

Lines changed: 0 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -4443,86 +4443,6 @@ func (s) TestGRPCMethod(t *testing.T) {
44434443
}
44444444
}
44454445

4446-
// renameProtoCodec is an encoding.Codec wrapper that allows customizing the
4447-
// Name() of another codec.
4448-
type renameProtoCodec struct {
4449-
encoding.Codec
4450-
name string
4451-
}
4452-
4453-
func (r *renameProtoCodec) Name() string { return r.name }
4454-
4455-
// TestForceCodecName confirms that the ForceCodec call option sets the subtype
4456-
// in the content-type header according to the Name() of the codec provided.
4457-
func (s) TestForceCodecName(t *testing.T) {
4458-
wantContentTypeCh := make(chan []string, 1)
4459-
defer close(wantContentTypeCh)
4460-
4461-
ss := &stubserver.StubServer{
4462-
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
4463-
md, ok := metadata.FromIncomingContext(ctx)
4464-
if !ok {
4465-
return nil, status.Errorf(codes.Internal, "no metadata in context")
4466-
}
4467-
if got, want := md["content-type"], <-wantContentTypeCh; !reflect.DeepEqual(got, want) {
4468-
return nil, status.Errorf(codes.Internal, "got content-type=%q; want [%q]", got, want)
4469-
}
4470-
return &testpb.Empty{}, nil
4471-
},
4472-
}
4473-
if err := ss.Start([]grpc.ServerOption{grpc.ForceServerCodec(encoding.GetCodec("proto"))}); err != nil {
4474-
t.Fatalf("Error starting endpoint server: %v", err)
4475-
}
4476-
defer ss.Stop()
4477-
4478-
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
4479-
defer cancel()
4480-
4481-
codec := &renameProtoCodec{Codec: encoding.GetCodec("proto"), name: "some-test-name"}
4482-
wantContentTypeCh <- []string{"application/grpc+some-test-name"}
4483-
if _, err := ss.Client.EmptyCall(ctx, &testpb.Empty{}, grpc.ForceCodec(codec)); err != nil {
4484-
t.Fatalf("ss.Client.EmptyCall(_, _) = _, %v; want _, nil", err)
4485-
}
4486-
4487-
// Confirm the name is converted to lowercase before transmitting.
4488-
codec.name = "aNoTHeRNaME"
4489-
wantContentTypeCh <- []string{"application/grpc+anothername"}
4490-
if _, err := ss.Client.EmptyCall(ctx, &testpb.Empty{}, grpc.ForceCodec(codec)); err != nil {
4491-
t.Fatalf("ss.Client.EmptyCall(_, _) = _, %v; want _, nil", err)
4492-
}
4493-
}
4494-
4495-
func (s) TestForceServerCodec(t *testing.T) {
4496-
ss := &stubserver.StubServer{
4497-
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
4498-
return &testpb.Empty{}, nil
4499-
},
4500-
}
4501-
codec := &countingProtoCodec{}
4502-
if err := ss.Start([]grpc.ServerOption{grpc.ForceServerCodec(codec)}); err != nil {
4503-
t.Fatalf("Error starting endpoint server: %v", err)
4504-
}
4505-
defer ss.Stop()
4506-
4507-
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
4508-
defer cancel()
4509-
4510-
if _, err := ss.Client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
4511-
t.Fatalf("ss.Client.EmptyCall(_, _) = _, %v; want _, nil", err)
4512-
}
4513-
4514-
unmarshalCount := atomic.LoadInt32(&codec.unmarshalCount)
4515-
const wantUnmarshalCount = 1
4516-
if unmarshalCount != wantUnmarshalCount {
4517-
t.Fatalf("protoCodec.unmarshalCount = %d; want %d", unmarshalCount, wantUnmarshalCount)
4518-
}
4519-
marshalCount := atomic.LoadInt32(&codec.marshalCount)
4520-
const wantMarshalCount = 1
4521-
if marshalCount != wantMarshalCount {
4522-
t.Fatalf("protoCodec.marshalCount = %d; want %d", marshalCount, wantMarshalCount)
4523-
}
4524-
}
4525-
45264446
func (s) TestUnaryProxyDoesNotForwardMetadata(t *testing.T) {
45274447
const mdkey = "somedata"
45284448

@@ -4865,77 +4785,6 @@ func testWaitForReadyConnection(t *testing.T, e env) {
48654785
}
48664786
}
48674787

4868-
type errCodec struct {
4869-
noError bool
4870-
}
4871-
4872-
func (c *errCodec) Marshal(v any) ([]byte, error) {
4873-
if c.noError {
4874-
return []byte{}, nil
4875-
}
4876-
return nil, fmt.Errorf("3987^12 + 4365^12 = 4472^12")
4877-
}
4878-
4879-
func (c *errCodec) Unmarshal(data []byte, v any) error {
4880-
return nil
4881-
}
4882-
4883-
func (c *errCodec) Name() string {
4884-
return "Fermat's near-miss."
4885-
}
4886-
4887-
type countingProtoCodec struct {
4888-
marshalCount int32
4889-
unmarshalCount int32
4890-
}
4891-
4892-
func (p *countingProtoCodec) Marshal(v any) ([]byte, error) {
4893-
atomic.AddInt32(&p.marshalCount, 1)
4894-
vv, ok := v.(proto.Message)
4895-
if !ok {
4896-
return nil, fmt.Errorf("failed to marshal, message is %T, want proto.Message", v)
4897-
}
4898-
return proto.Marshal(vv)
4899-
}
4900-
4901-
func (p *countingProtoCodec) Unmarshal(data []byte, v any) error {
4902-
atomic.AddInt32(&p.unmarshalCount, 1)
4903-
vv, ok := v.(proto.Message)
4904-
if !ok {
4905-
return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v)
4906-
}
4907-
return proto.Unmarshal(data, vv)
4908-
}
4909-
4910-
func (*countingProtoCodec) Name() string {
4911-
return "proto"
4912-
}
4913-
4914-
func (s) TestEncodeDoesntPanic(t *testing.T) {
4915-
for _, e := range listTestEnv() {
4916-
testEncodeDoesntPanic(t, e)
4917-
}
4918-
}
4919-
4920-
func testEncodeDoesntPanic(t *testing.T, e env) {
4921-
te := newTest(t, e)
4922-
erc := &errCodec{}
4923-
te.customCodec = erc
4924-
te.startServer(&testServer{security: e.security})
4925-
defer te.tearDown()
4926-
te.customCodec = nil
4927-
tc := testgrpc.NewTestServiceClient(te.clientConn())
4928-
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
4929-
defer cancel()
4930-
// Failure case, should not panic.
4931-
tc.EmptyCall(ctx, &testpb.Empty{})
4932-
erc.noError = true
4933-
// Passing case.
4934-
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
4935-
t.Fatalf("EmptyCall(_, _) = _, %v, want _, <nil>", err)
4936-
}
4937-
}
4938-
49394788
func (s) TestSvrWriteStatusEarlyWrite(t *testing.T) {
49404789
for _, e := range listTestEnv() {
49414790
testSvrWriteStatusEarlyWrite(t, e)

0 commit comments

Comments
 (0)