proto_test.go 3.2 KB

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