Debian Codegraph @

pdata/pcommon/traceid_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package pcommon
5
6 import (
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10 )
11
12 func TestTraceID(t *testing.T) {
13 tid := TraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1})
14 assert.Equal(t, [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}, [16]byte(tid))
15 assert.False(t, tid.IsEmpty())
16 }
17
18 func TestNewTraceIDEmpty(t *testing.T) {
19 tid := NewTraceIDEmpty()
20 assert.Equal(t, [16]byte{}, [16]byte(tid))
21 assert.True(t, tid.IsEmpty())
22 }
23
24 func TestTraceIDString(t *testing.T) {
25 tid := TraceID([16]byte{})
26 assert.Empty(t, tid.String())
27
28 tid = [16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}
29 assert.Equal(t, "12345678123456781234567812345678", tid.String())
30 }
31
32 func TestTraceIDImmutable(t *testing.T) {
33 initialBytes := [16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}
34 tid := TraceID(initialBytes)
35 assert.Equal(t, TraceID(initialBytes), tid)
36
37 // Get the bytes and try to mutate.
38 tid[4] = 0x23
39
40 // Does not change the already created TraceID.
41 assert.NotEqual(t, TraceID(initialBytes), tid)
42 }