-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decouple the trace id struct generation from its encoding (#3019)
- Loading branch information
Showing
3 changed files
with
100 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,98 @@ | ||
package tracing | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTraceID_isValid(t *testing.T) { | ||
func TestNewTraceID(t *testing.T) { | ||
t.Parallel() | ||
|
||
type fields struct { | ||
Prefix int16 | ||
Code int8 | ||
Time time.Time | ||
} | ||
testTime := time.Date(2022, time.January, 1, 0, 0, 0, 0, time.UTC) | ||
testRandSourceFn := func() io.Reader { return bytes.NewReader([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) } | ||
|
||
// Precomputed hexadecimal representation of the binary values | ||
// of the traceID components. | ||
wantPrefixHexString := "dc07" | ||
wantCloudCodeHexString := "18" | ||
wantLocalCodeHexString := "42" | ||
wantTimeHexString := "8080f8e1949cfec52d" | ||
wantRandHexString := "01020304" | ||
|
||
testCases := []struct { | ||
name string | ||
fields fields | ||
want bool | ||
name string | ||
prefix int16 | ||
code int8 | ||
t time.Time | ||
randSource io.Reader | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "traceID with k6 cloud code is valid", | ||
fields: fields{ | ||
Prefix: k6Prefix, | ||
Code: k6CloudCode, | ||
Time: time.Unix(123456789, 0), | ||
}, | ||
want: true, | ||
name: "valid traceID with cloud code should succeed", | ||
prefix: k6Prefix, | ||
code: k6CloudCode, | ||
t: testTime, | ||
randSource: testRandSourceFn(), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "traceID with k6 local code is valid", | ||
fields: fields{ | ||
Prefix: k6Prefix, | ||
Code: k6LocalCode, | ||
Time: time.Unix(123456789, 0), | ||
}, | ||
want: true, | ||
name: "valid traceID with local code should succeed", | ||
prefix: k6Prefix, | ||
code: k6LocalCode, | ||
t: testTime, | ||
randSource: testRandSourceFn(), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "traceID with prefix != k6Prefix is invalid", | ||
fields: fields{ | ||
Prefix: 0, | ||
Code: k6CloudCode, | ||
Time: time.Unix(123456789, 0), | ||
}, | ||
want: false, | ||
name: "traceID with invalid prefix should fail", | ||
prefix: 0o123, | ||
code: k6CloudCode, | ||
t: testTime, | ||
randSource: testRandSourceFn(), | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "traceID code with code != k6CloudCode and code != k6LocalCode is invalid", | ||
fields: fields{ | ||
Prefix: k6Prefix, | ||
Code: 0, | ||
Time: time.Unix(123456789, 0), | ||
}, | ||
want: false, | ||
name: "traceID with invalid code should fail", | ||
prefix: k6Prefix, | ||
code: 0o123, | ||
t: testTime, | ||
randSource: testRandSourceFn(), | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
tr := &TraceID{ | ||
Prefix: tc.fields.Prefix, | ||
Code: tc.fields.Code, | ||
Time: tc.fields.Time, | ||
gotTraceID, gotErr := newTraceID(tc.prefix, tc.code, tc.t, tc.randSource) | ||
|
||
if tc.wantErr { | ||
require.Error(t, gotErr) | ||
return | ||
} | ||
|
||
if got := tr.isValid(); got != tc.want { | ||
t.Errorf("TraceID.isValid() = %v, want %v", got, tc.want) | ||
prefixEndOffset := len(wantPrefixHexString) | ||
assert.Equal(t, wantPrefixHexString, gotTraceID[:prefixEndOffset]) | ||
|
||
codeEndOffset := prefixEndOffset + len(wantCloudCodeHexString) | ||
if tc.code == k6CloudCode { | ||
assert.Equal(t, wantCloudCodeHexString, gotTraceID[prefixEndOffset:codeEndOffset]) | ||
} else { | ||
assert.Equal(t, wantLocalCodeHexString, gotTraceID[prefixEndOffset:codeEndOffset]) | ||
} | ||
|
||
timeEndOffset := codeEndOffset + len(wantTimeHexString) | ||
assert.Equal(t, wantTimeHexString, gotTraceID[codeEndOffset:timeEndOffset]) | ||
|
||
assert.Equal(t, wantRandHexString, gotTraceID[timeEndOffset:]) | ||
}) | ||
} | ||
} |