mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-01-01 22:09:57 +02:00
add IsValid() for SpanContext (#46)
* add IsValid() for SpanContext * add unit test
This commit is contained in:
parent
0f32efcdaa
commit
0a37b8f43e
@ -44,6 +44,10 @@ var (
|
||||
INVALID_SPAN_CONTEXT = SpanContext{}
|
||||
)
|
||||
|
||||
func (sc SpanContext) IsValid() bool {
|
||||
return sc.HasTraceID() && sc.HasSpanID()
|
||||
}
|
||||
|
||||
func (sc SpanContext) HasTraceID() bool {
|
||||
return sc.TraceID.High != 0 || sc.TraceID.Low != 0
|
||||
}
|
||||
|
@ -18,6 +18,43 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsValid(t *testing.T) {
|
||||
for _, testcase := range []struct {
|
||||
name string
|
||||
tid TraceID
|
||||
sid uint64
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "bothTrue",
|
||||
tid: TraceID{High: uint64(42)},
|
||||
sid: uint64(42),
|
||||
want: true,
|
||||
}, {
|
||||
name: "bothFalse",
|
||||
tid: TraceID{High: uint64(0)},
|
||||
sid: uint64(0),
|
||||
want: false,
|
||||
}, {
|
||||
name: "oneTrue",
|
||||
tid: TraceID{High: uint64(0)},
|
||||
sid: uint64(42),
|
||||
want: false,
|
||||
},
|
||||
} {
|
||||
t.Run(testcase.name, func(t *testing.T) {
|
||||
sc := SpanContext{
|
||||
TraceID: testcase.tid,
|
||||
SpanID: testcase.sid,
|
||||
}
|
||||
have := sc.IsValid()
|
||||
if have != testcase.want {
|
||||
t.Errorf("Want: %v, but have: %v", testcase.want, have)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasTraceID(t *testing.T) {
|
||||
for _, testcase := range []struct {
|
||||
name string
|
||||
|
Loading…
Reference in New Issue
Block a user