2023-10-24 17:16:58 +00:00
|
|
|
package dcontext
|
2015-04-10 01:48:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestWithTrace ensures that tracing has the expected values in the context.
|
|
|
|
func TestWithTrace(t *testing.T) {
|
2023-04-30 13:41:20 +00:00
|
|
|
t.Parallel()
|
2015-04-10 01:48:05 +00:00
|
|
|
pc, file, _, _ := runtime.Caller(0) // get current caller.
|
|
|
|
f := runtime.FuncForPC(pc)
|
|
|
|
|
|
|
|
base := []valueTestCase{
|
|
|
|
{
|
|
|
|
key: "trace.id",
|
|
|
|
notnilorempty: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
key: "trace.file",
|
|
|
|
expected: file,
|
|
|
|
notnilorempty: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "trace.line",
|
|
|
|
notnilorempty: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "trace.start",
|
|
|
|
notnilorempty: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, done := WithTrace(Background())
|
2023-11-29 21:40:20 +00:00
|
|
|
t.Cleanup(func() { done("this will be emitted at end of test") })
|
2015-04-10 01:48:05 +00:00
|
|
|
|
2023-04-30 13:41:20 +00:00
|
|
|
tests := append(base, valueTestCase{
|
2015-04-10 01:48:05 +00:00
|
|
|
key: "trace.func",
|
|
|
|
expected: f.Name(),
|
2023-04-30 13:41:20 +00:00
|
|
|
})
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.key, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
v := ctx.Value(tc.key)
|
|
|
|
if tc.notnilorempty {
|
|
|
|
if v == nil || v == "" {
|
|
|
|
t.Fatalf("value was nil or empty: %#v", v)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if v != tc.expected {
|
|
|
|
t.Fatalf("unexpected value: %v != %v", v, tc.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2015-04-10 01:48:05 +00:00
|
|
|
|
2023-04-30 12:43:13 +00:00
|
|
|
tracedFn := func() {
|
2015-04-10 01:48:05 +00:00
|
|
|
parentID := ctx.Value("trace.id") // ensure the parent trace id is correct.
|
|
|
|
|
|
|
|
pc, _, _, _ := runtime.Caller(0) // get current caller.
|
|
|
|
f := runtime.FuncForPC(pc)
|
|
|
|
ctx, done := WithTrace(ctx)
|
|
|
|
defer done("this should be subordinate to the other trace")
|
|
|
|
time.Sleep(time.Second)
|
2023-04-30 13:41:20 +00:00
|
|
|
tests := append(base, valueTestCase{
|
2015-04-10 01:48:05 +00:00
|
|
|
key: "trace.func",
|
|
|
|
expected: f.Name(),
|
|
|
|
}, valueTestCase{
|
|
|
|
key: "trace.parent.id",
|
|
|
|
expected: parentID,
|
2023-04-30 13:41:20 +00:00
|
|
|
})
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.key, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
v := ctx.Value(tc.key)
|
|
|
|
if tc.notnilorempty {
|
|
|
|
if v == nil || v == "" {
|
|
|
|
t.Fatalf("value was nil or empty: %#v", v)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if v != tc.expected {
|
|
|
|
t.Fatalf("unexpected value: %v != %v", v, tc.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2015-04-10 01:48:05 +00:00
|
|
|
}
|
2023-04-30 12:43:13 +00:00
|
|
|
tracedFn()
|
2015-04-10 01:48:05 +00:00
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
type valueTestCase struct {
|
|
|
|
key string
|
|
|
|
expected interface{}
|
|
|
|
notnilorempty bool // just check not empty/not nil
|
|
|
|
}
|