| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package xray |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"strconv" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
"go.opentelemetry.io/otel/trace" |
| 15 |
) |
| 16 |
|
| 17 |
func TestTraceIDIsValidLength(t *testing.T) { |
| 18 |
idg := NewIDGenerator() |
| 19 |
traceID, _ := idg.NewIDs(t.Context()) |
| 20 |
|
| 21 |
expectedTraceIDLength := 32 |
| 22 |
assert.Len(t, traceID.String(), expectedTraceIDLength, "TraceID has incorrect length.") |
| 23 |
} |
| 24 |
|
| 25 |
func TestTraceIDIsUnique(t *testing.T) { |
| 26 |
idg := NewIDGenerator() |
| 27 |
traceID1, _ := idg.NewIDs(t.Context()) |
| 28 |
traceID2, _ := idg.NewIDs(t.Context()) |
| 29 |
|
| 30 |
assert.NotEqual(t, traceID1.String(), traceID2.String(), "TraceID should be unique") |
| 31 |
} |
| 32 |
|
| 33 |
func TestTraceIDTimestampInBounds(t *testing.T) { |
| 34 |
idg := NewIDGenerator() |
| 35 |
|
| 36 |
previousTime := time.Now().Unix() |
| 37 |
|
| 38 |
traceID, _ := idg.NewIDs(t.Context()) |
| 39 |
|
| 40 |
currentTime, err := strconv.ParseInt(traceID.String()[0:8], 16, 64) |
| 41 |
require.NoError(t, err) |
| 42 |
|
| 43 |
nextTime := time.Now().Unix() |
| 44 |
|
| 45 |
assert.LessOrEqual(t, previousTime, currentTime, "TraceID is generated incorrectly with the wrong timestamp.") |
| 46 |
assert.LessOrEqual(t, currentTime, nextTime, "TraceID is generated incorrectly with the wrong timestamp.") |
| 47 |
} |
| 48 |
|
| 49 |
func TestTraceIDIsNotNil(t *testing.T) { |
| 50 |
var nilTraceID trace.TraceID |
| 51 |
idg := NewIDGenerator() |
| 52 |
traceID, _ := idg.NewIDs(t.Context()) |
| 53 |
|
| 54 |
assert.False(t, bytes.Equal(traceID[:], nilTraceID[:]), "TraceID cannot be empty.") |
| 55 |
} |
| 56 |
|
| 57 |
func TestSpanIDIsValidLength(t *testing.T) { |
| 58 |
idg := NewIDGenerator() |
| 59 |
ctx := t.Context() |
| 60 |
traceID, spanID1 := idg.NewIDs(ctx) |
| 61 |
spanID2 := idg.NewSpanID(t.Context(), traceID) |
| 62 |
expectedSpanIDLength := 16 |
| 63 |
|
| 64 |
assert.Len(t, spanID1.String(), expectedSpanIDLength, "SpanID has incorrect length") |
| 65 |
assert.Len(t, spanID2.String(), expectedSpanIDLength, "SpanID has incorrect length") |
| 66 |
} |
| 67 |
|
| 68 |
func TestSpanIDIsUnique(t *testing.T) { |
| 69 |
idg := NewIDGenerator() |
| 70 |
ctx := t.Context() |
| 71 |
traceID, spanID1 := idg.NewIDs(ctx) |
| 72 |
_, spanID2 := idg.NewIDs(ctx) |
| 73 |
|
| 74 |
spanID3 := idg.NewSpanID(ctx, traceID) |
| 75 |
spanID4 := idg.NewSpanID(ctx, traceID) |
| 76 |
|
| 77 |
assert.NotEqual(t, spanID1.String(), spanID2.String(), "SpanID should be unique") |
| 78 |
assert.NotEqual(t, spanID3.String(), spanID4.String(), "SpanID should be unique") |
| 79 |
} |
| 80 |
|
| 81 |
func TestSpanIDIsNotNil(t *testing.T) { |
| 82 |
var nilSpanID trace.SpanID |
| 83 |
idg := NewIDGenerator() |
| 84 |
ctx := t.Context() |
| 85 |
traceID, spanID1 := idg.NewIDs(ctx) |
| 86 |
spanID2 := idg.NewSpanID(ctx, traceID) |
| 87 |
|
| 88 |
assert.False(t, bytes.Equal(spanID1[:], nilSpanID[:]), "SpanID cannot be empty.") |
| 89 |
assert.False(t, bytes.Equal(spanID2[:], nilSpanID[:]), "SpanID cannot be empty.") |
| 90 |
} |