You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

130 lines
3.0 KiB

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