|
| 1 | +// Copyright The OpenTelemetry 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 xray |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "context" |
| 20 | + "strconv" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + |
| 27 | + "go.opentelemetry.io/otel/trace" |
| 28 | +) |
| 29 | + |
| 30 | +func TestTraceIDIsValidLength(t *testing.T) { |
| 31 | + idg := NewIDGenerator() |
| 32 | + traceID, _ := idg.NewIDs(context.Background()) |
| 33 | + |
| 34 | + expectedTraceIDLength := 32 |
| 35 | + assert.Equal(t, len(traceID.String()), expectedTraceIDLength, "TraceID has incorrect length.") |
| 36 | +} |
| 37 | + |
| 38 | +func TestTraceIDIsUnique(t *testing.T) { |
| 39 | + idg := NewIDGenerator() |
| 40 | + traceID1, _ := idg.NewIDs(context.Background()) |
| 41 | + traceID2, _ := idg.NewIDs(context.Background()) |
| 42 | + |
| 43 | + assert.NotEqual(t, traceID1.String(), traceID2.String(), "TraceID should be unique") |
| 44 | +} |
| 45 | + |
| 46 | +func TestTraceIDTimestampInBounds(t *testing.T) { |
| 47 | + |
| 48 | + idg := NewIDGenerator() |
| 49 | + |
| 50 | + previousTime := time.Now().Unix() |
| 51 | + |
| 52 | + traceID, _ := idg.NewIDs(context.Background()) |
| 53 | + |
| 54 | + currentTime, err := strconv.ParseInt(traceID.String()[0:8], 16, 64) |
| 55 | + require.NoError(t, err) |
| 56 | + |
| 57 | + nextTime := time.Now().Unix() |
| 58 | + |
| 59 | + assert.LessOrEqual(t, previousTime, currentTime, "TraceID is generated incorrectly with the wrong timestamp.") |
| 60 | + assert.LessOrEqual(t, currentTime, nextTime, "TraceID is generated incorrectly with the wrong timestamp.") |
| 61 | +} |
| 62 | + |
| 63 | +func TestTraceIDIsNotNil(t *testing.T) { |
| 64 | + var nilTraceID trace.TraceID |
| 65 | + idg := NewIDGenerator() |
| 66 | + traceID, _ := idg.NewIDs(context.Background()) |
| 67 | + |
| 68 | + assert.False(t, bytes.Equal(traceID[:], nilTraceID[:]), "TraceID cannot be empty.") |
| 69 | +} |
| 70 | + |
| 71 | +func TestSpanIDIsValidLength(t *testing.T) { |
| 72 | + idg := NewIDGenerator() |
| 73 | + ctx := context.Background() |
| 74 | + traceID, spanID1 := idg.NewIDs(ctx) |
| 75 | + spanID2 := idg.NewSpanID(context.Background(), traceID) |
| 76 | + expectedSpanIDLength := 16 |
| 77 | + |
| 78 | + assert.Equal(t, len(spanID1.String()), expectedSpanIDLength, "SpanID has incorrect length") |
| 79 | + assert.Equal(t, len(spanID2.String()), expectedSpanIDLength, "SpanID has incorrect length") |
| 80 | +} |
| 81 | + |
| 82 | +func TestSpanIDIsUnique(t *testing.T) { |
| 83 | + idg := NewIDGenerator() |
| 84 | + ctx := context.Background() |
| 85 | + traceID, spanID1 := idg.NewIDs(ctx) |
| 86 | + _, spanID2 := idg.NewIDs(ctx) |
| 87 | + |
| 88 | + spanID3 := idg.NewSpanID(ctx, traceID) |
| 89 | + spanID4 := idg.NewSpanID(ctx, traceID) |
| 90 | + |
| 91 | + assert.NotEqual(t, spanID1.String(), spanID2.String(), "SpanID should be unique") |
| 92 | + assert.NotEqual(t, spanID3.String(), spanID4.String(), "SpanID should be unique") |
| 93 | +} |
| 94 | + |
| 95 | +func TestSpanIDIsNotNil(t *testing.T) { |
| 96 | + var nilSpanID trace.SpanID |
| 97 | + idg := NewIDGenerator() |
| 98 | + ctx := context.Background() |
| 99 | + traceID, spanID1 := idg.NewIDs(ctx) |
| 100 | + spanID2 := idg.NewSpanID(ctx, traceID) |
| 101 | + |
| 102 | + assert.False(t, bytes.Equal(spanID1[:], nilSpanID[:]), "SpanID cannot be empty.") |
| 103 | + assert.False(t, bytes.Equal(spanID2[:], nilSpanID[:]), "SpanID cannot be empty.") |
| 104 | +} |
0 commit comments