propagation_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2017, OpenCensus Authors
  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 propagation
  15. import (
  16. "bytes"
  17. "fmt"
  18. "testing"
  19. . "go.opencensus.io/trace"
  20. )
  21. func TestBinary(t *testing.T) {
  22. tid := TraceID{0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f}
  23. sid := SpanID{0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68}
  24. b := []byte{
  25. 0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1, 97, 98, 99, 100,
  26. 101, 102, 103, 104, 2, 1,
  27. }
  28. if b2 := Binary(SpanContext{
  29. TraceID: tid,
  30. SpanID: sid,
  31. TraceOptions: 1,
  32. }); !bytes.Equal(b2, b) {
  33. t.Errorf("Binary: got serialization %02x want %02x", b2, b)
  34. }
  35. sc, ok := FromBinary(b)
  36. if !ok {
  37. t.Errorf("FromBinary: got ok==%t, want true", ok)
  38. }
  39. if got := sc.TraceID; got != tid {
  40. t.Errorf("FromBinary: got trace ID %s want %s", got, tid)
  41. }
  42. if got := sc.SpanID; got != sid {
  43. t.Errorf("FromBinary: got span ID %s want %s", got, sid)
  44. }
  45. b[0] = 1
  46. sc, ok = FromBinary(b)
  47. if ok {
  48. t.Errorf("FromBinary: decoding bytes containing an unsupported version: got ok==%t want false", ok)
  49. }
  50. b = []byte{0, 1, 97, 98, 99, 100, 101, 102, 103, 104, 2, 1}
  51. sc, ok = FromBinary(b)
  52. if ok {
  53. t.Errorf("FromBinary: decoding bytes without a TraceID: got ok==%t want false", ok)
  54. }
  55. if b := Binary(SpanContext{}); b != nil {
  56. t.Errorf("Binary(SpanContext{}): got serialization %02x want nil", b)
  57. }
  58. }
  59. func TestFromBinary(t *testing.T) {
  60. 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}
  61. tests := []struct {
  62. name string
  63. data []byte
  64. wantTraceID TraceID
  65. wantSpanID SpanID
  66. wantOpts TraceOptions
  67. wantOk bool
  68. }{
  69. {
  70. name: "nil data",
  71. data: nil,
  72. wantOk: false,
  73. },
  74. {
  75. name: "short data",
  76. data: []byte{0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77},
  77. wantOk: false,
  78. },
  79. {
  80. name: "wrong field number",
  81. data: []byte{0, 1, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77},
  82. wantOk: false,
  83. },
  84. {
  85. name: "valid data",
  86. data: validData,
  87. wantTraceID: TraceID{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79},
  88. wantSpanID: SpanID{97, 98, 99, 100, 101, 102, 103, 104},
  89. wantOpts: 1,
  90. wantOk: true,
  91. },
  92. }
  93. for _, tt := range tests {
  94. sc, gotOk := FromBinary(tt.data)
  95. gotTraceID, gotSpanID, gotOpts := sc.TraceID, sc.SpanID, sc.TraceOptions
  96. if gotTraceID != tt.wantTraceID {
  97. t.Errorf("%s: Decode() gotTraceID = %v, want %v", tt.name, gotTraceID, tt.wantTraceID)
  98. }
  99. if gotSpanID != tt.wantSpanID {
  100. t.Errorf("%s: Decode() gotSpanID = %v, want %v", tt.name, gotSpanID, tt.wantSpanID)
  101. }
  102. if gotOpts != tt.wantOpts {
  103. t.Errorf("%s: Decode() gotOpts = %v, want %v", tt.name, gotOpts, tt.wantOpts)
  104. }
  105. if gotOk != tt.wantOk {
  106. t.Errorf("%s: Decode() gotOk = %v, want %v", tt.name, gotOk, tt.wantOk)
  107. }
  108. }
  109. }
  110. func BenchmarkBinary(b *testing.B) {
  111. tid := TraceID{0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f}
  112. sid := SpanID{0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68}
  113. sc := SpanContext{
  114. TraceID: tid,
  115. SpanID: sid,
  116. }
  117. var x byte
  118. for i := 0; i < b.N; i++ {
  119. bin := Binary(sc)
  120. x += bin[0]
  121. }
  122. if x == 1 {
  123. fmt.Println(x) // try to prevent optimizing-out
  124. }
  125. }
  126. func BenchmarkFromBinary(b *testing.B) {
  127. bin := []byte{
  128. 0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1, 97, 98, 99, 100,
  129. 101, 102, 103, 104, 2, 1,
  130. }
  131. var x byte
  132. for i := 0; i < b.N; i++ {
  133. sc, _ := FromBinary(bin)
  134. x += sc.TraceID[0]
  135. }
  136. if x == 1 {
  137. fmt.Println(x) // try to prevent optimizing-out
  138. }
  139. }