|
| 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 | + "bytes" |
| 23 | + "context" |
| 24 | + "io" |
| 25 | + "testing" |
| 26 | + "time" |
| 27 | + |
| 28 | + "google.golang.org/grpc" |
| 29 | + "google.golang.org/grpc/internal/stubserver" |
| 30 | + testgrpc "google.golang.org/grpc/interop/grpc_testing" |
| 31 | + testpb "google.golang.org/grpc/interop/grpc_testing" |
| 32 | +) |
| 33 | + |
| 34 | +func (s) TestRecvBufferPool(t *testing.T) { |
| 35 | + ss := &stubserver.StubServer{ |
| 36 | + FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error { |
| 37 | + for i := 0; i < 10; i++ { |
| 38 | + preparedMsg := &grpc.PreparedMsg{} |
| 39 | + err := preparedMsg.Encode(stream, &testpb.StreamingOutputCallResponse{ |
| 40 | + Payload: &testpb.Payload{ |
| 41 | + Body: []byte{'0' + uint8(i)}, |
| 42 | + }, |
| 43 | + }) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + stream.SendMsg(preparedMsg) |
| 48 | + } |
| 49 | + return nil |
| 50 | + }, |
| 51 | + } |
| 52 | + if err := ss.Start( |
| 53 | + []grpc.ServerOption{grpc.RecvBufferPool(grpc.NewsimpleSharedBufferPool())}, |
| 54 | + grpc.WithRecvBufferPool(grpc.NewsimpleSharedBufferPool()), |
| 55 | + ); err != nil { |
| 56 | + t.Fatalf("Error starting endpoint server: %v", err) |
| 57 | + } |
| 58 | + defer ss.Stop() |
| 59 | + |
| 60 | + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 61 | + defer cancel() |
| 62 | + |
| 63 | + stream, err := ss.Client.FullDuplexCall(ctx) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("ss.Client.FullDuplexCall failed: %f", err) |
| 66 | + } |
| 67 | + |
| 68 | + var ngot int |
| 69 | + var buf bytes.Buffer |
| 70 | + for { |
| 71 | + reply, err := stream.Recv() |
| 72 | + if err == io.EOF { |
| 73 | + break |
| 74 | + } |
| 75 | + if err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + } |
| 78 | + ngot++ |
| 79 | + if buf.Len() > 0 { |
| 80 | + buf.WriteByte(',') |
| 81 | + } |
| 82 | + buf.Write(reply.GetPayload().GetBody()) |
| 83 | + } |
| 84 | + if want := 10; ngot != want { |
| 85 | + t.Errorf("Got %d replies, want %d", ngot, want) |
| 86 | + } |
| 87 | + if got, want := buf.String(), "0,1,2,3,4,5,6,7,8,9"; got != want { |
| 88 | + t.Errorf("Got replies %q; want %q", got, want) |
| 89 | + } |
| 90 | +} |
0 commit comments