Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

137 строки
3.5 KiB

  1. // Copyright 2014 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package tracecontext
  15. import (
  16. "testing"
  17. "cloud.google.com/go/internal/testutil"
  18. )
  19. var validData = []byte{0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1, 97, 98, 99, 100, 101, 102, 103, 104, 2, 1}
  20. func TestDecode(t *testing.T) {
  21. tests := []struct {
  22. name string
  23. data []byte
  24. wantTraceID []byte
  25. wantSpanID uint64
  26. wantOpts byte
  27. wantOk bool
  28. }{
  29. {
  30. name: "nil data",
  31. data: nil,
  32. wantTraceID: nil,
  33. wantSpanID: 0,
  34. wantOpts: 0,
  35. wantOk: false,
  36. },
  37. {
  38. name: "short data",
  39. data: []byte{0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77},
  40. wantTraceID: nil,
  41. wantSpanID: 0,
  42. wantOpts: 0,
  43. wantOk: false,
  44. },
  45. {
  46. name: "wrong field number",
  47. data: []byte{0, 1, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77},
  48. wantTraceID: nil,
  49. wantSpanID: 0,
  50. wantOpts: 0,
  51. wantOk: false,
  52. },
  53. {
  54. name: "valid data",
  55. data: validData,
  56. wantTraceID: []byte{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79},
  57. wantSpanID: 0x6867666564636261,
  58. wantOpts: 1,
  59. wantOk: true,
  60. },
  61. }
  62. for _, tt := range tests {
  63. gotTraceID, gotSpanID, gotOpts, gotOk := Decode(tt.data)
  64. if !testutil.Equal(gotTraceID, tt.wantTraceID) {
  65. t.Errorf("%s: Decode() gotTraceID = %v, want %v", tt.name, gotTraceID, tt.wantTraceID)
  66. }
  67. if gotSpanID != tt.wantSpanID {
  68. t.Errorf("%s: Decode() gotSpanID = %v, want %v", tt.name, gotSpanID, tt.wantSpanID)
  69. }
  70. if gotOpts != tt.wantOpts {
  71. t.Errorf("%s: Decode() gotOpts = %v, want %v", tt.name, gotOpts, tt.wantOpts)
  72. }
  73. if gotOk != tt.wantOk {
  74. t.Errorf("%s: Decode() gotOk = %v, want %v", tt.name, gotOk, tt.wantOk)
  75. }
  76. }
  77. }
  78. func TestEncode(t *testing.T) {
  79. tests := []struct {
  80. name string
  81. dst []byte
  82. traceID []byte
  83. spanID uint64
  84. opts byte
  85. wantN int
  86. wantData []byte
  87. }{
  88. {
  89. name: "short data",
  90. dst: make([]byte, 0),
  91. traceID: []byte("00112233445566"),
  92. spanID: 0x6867666564636261,
  93. opts: 1,
  94. wantN: -1,
  95. wantData: make([]byte, 0),
  96. },
  97. {
  98. name: "valid data",
  99. dst: make([]byte, Len),
  100. traceID: []byte{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79},
  101. spanID: 0x6867666564636261,
  102. opts: 1,
  103. wantN: Len,
  104. wantData: validData,
  105. },
  106. }
  107. for _, tt := range tests {
  108. gotN := Encode(tt.dst, tt.traceID, tt.spanID, tt.opts)
  109. if gotN != tt.wantN {
  110. t.Errorf("%s: n = %v, want %v", tt.name, gotN, tt.wantN)
  111. }
  112. if gotData := tt.dst; !testutil.Equal(gotData, tt.wantData) {
  113. t.Errorf("%s: dst = %v, want %v", tt.name, gotData, tt.wantData)
  114. }
  115. }
  116. }
  117. func BenchmarkDecode(b *testing.B) {
  118. for i := 0; i < b.N; i++ {
  119. Decode(validData)
  120. }
  121. }
  122. func BenchmarkEncode(b *testing.B) {
  123. for i := 0; i < b.N; i++ {
  124. traceID := make([]byte, 16)
  125. var opts byte
  126. Encode(validData, traceID, 0, opts)
  127. }
  128. }