gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/encoding/proto/proto_test.go (about)

     1  /*
     2   *
     3   * Copyright 2018 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 proto
    20  
    21  import (
    22  	"bytes"
    23  	"sync"
    24  	"testing"
    25  
    26  	"gitee.com/ks-custle/core-gm/grpc/encoding"
    27  	"gitee.com/ks-custle/core-gm/grpc/internal/grpctest"
    28  	"gitee.com/ks-custle/core-gm/grpc/test/codec_perf"
    29  )
    30  
    31  func marshalAndUnmarshal(t *testing.T, codec encoding.Codec, expectedBody []byte) {
    32  	p := &codec_perf.Buffer{}
    33  	p.Body = expectedBody
    34  
    35  	marshalledBytes, err := codec.Marshal(p)
    36  	if err != nil {
    37  		t.Errorf("codec.Marshal(_) returned an error")
    38  	}
    39  
    40  	if err := codec.Unmarshal(marshalledBytes, p); err != nil {
    41  		t.Errorf("codec.Unmarshal(_) returned an error")
    42  	}
    43  
    44  	if !bytes.Equal(p.GetBody(), expectedBody) {
    45  		t.Errorf("Unexpected body; got %v; want %v", p.GetBody(), expectedBody)
    46  	}
    47  }
    48  
    49  type s struct {
    50  	grpctest.Tester
    51  }
    52  
    53  func Test(t *testing.T) {
    54  	grpctest.RunSubTests(t, s{})
    55  }
    56  
    57  func (s) TestBasicProtoCodecMarshalAndUnmarshal(t *testing.T) {
    58  	marshalAndUnmarshal(t, codec{}, []byte{1, 2, 3})
    59  }
    60  
    61  // Try to catch possible race conditions around use of pools
    62  func (s) TestConcurrentUsage(t *testing.T) {
    63  	const (
    64  		numGoRoutines   = 100
    65  		numMarshUnmarsh = 1000
    66  	)
    67  
    68  	// small, arbitrary byte slices
    69  	protoBodies := [][]byte{
    70  		[]byte("one"),
    71  		[]byte("two"),
    72  		[]byte("three"),
    73  		[]byte("four"),
    74  		[]byte("five"),
    75  	}
    76  
    77  	var wg sync.WaitGroup
    78  	codec := codec{}
    79  
    80  	for i := 0; i < numGoRoutines; i++ {
    81  		wg.Add(1)
    82  		go func() {
    83  			defer wg.Done()
    84  			for k := 0; k < numMarshUnmarsh; k++ {
    85  				marshalAndUnmarshal(t, codec, protoBodies[k%len(protoBodies)])
    86  			}
    87  		}()
    88  	}
    89  
    90  	wg.Wait()
    91  }
    92  
    93  // TestStaggeredMarshalAndUnmarshalUsingSamePool tries to catch potential errors in which slices get
    94  // stomped on during reuse of a proto.Buffer.
    95  func (s) TestStaggeredMarshalAndUnmarshalUsingSamePool(t *testing.T) {
    96  	codec1 := codec{}
    97  	codec2 := codec{}
    98  
    99  	expectedBody1 := []byte{1, 2, 3}
   100  	expectedBody2 := []byte{4, 5, 6}
   101  
   102  	proto1 := codec_perf.Buffer{Body: expectedBody1}
   103  	proto2 := codec_perf.Buffer{Body: expectedBody2}
   104  
   105  	var m1, m2 []byte
   106  	var err error
   107  
   108  	if m1, err = codec1.Marshal(&proto1); err != nil {
   109  		t.Errorf("codec.Marshal(%s) failed", &proto1)
   110  	}
   111  
   112  	if m2, err = codec2.Marshal(&proto2); err != nil {
   113  		t.Errorf("codec.Marshal(%s) failed", &proto2)
   114  	}
   115  
   116  	if err = codec1.Unmarshal(m1, &proto1); err != nil {
   117  		t.Errorf("codec.Unmarshal(%v) failed", m1)
   118  	}
   119  
   120  	if err = codec2.Unmarshal(m2, &proto2); err != nil {
   121  		t.Errorf("codec.Unmarshal(%v) failed", m2)
   122  	}
   123  
   124  	b1 := proto1.GetBody()
   125  	b2 := proto2.GetBody()
   126  
   127  	for i, v := range b1 {
   128  		if expectedBody1[i] != v {
   129  			t.Errorf("expected %v at index %v but got %v", i, expectedBody1[i], v)
   130  		}
   131  	}
   132  
   133  	for i, v := range b2 {
   134  		if expectedBody2[i] != v {
   135  			t.Errorf("expected %v at index %v but got %v", i, expectedBody2[i], v)
   136  		}
   137  	}
   138  }