...

Source file src/go.opencensus.io/trace/propagation/propagation_test.go

Documentation: go.opencensus.io/trace/propagation

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

View as plain text