context: TestWithTrace(): inline checkContextForValues, use sub-tests

checkContextForValues was effectively running sub-tests, but disguided
as a helper (to DRY). While it helped some duplication, rewriting it to
run subtests within a helper also was a bit confusing, so just inline
what it does.

While updating, also run tests with t.Parallel()

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-04-30 15:41:20 +02:00
parent 2829f7b7d5
commit f4be328bff
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -1,7 +1,6 @@
package context package context
import ( import (
"context"
"runtime" "runtime"
"testing" "testing"
"time" "time"
@ -9,6 +8,7 @@ import (
// TestWithTrace ensures that tracing has the expected values in the context. // TestWithTrace ensures that tracing has the expected values in the context.
func TestWithTrace(t *testing.T) { func TestWithTrace(t *testing.T) {
t.Parallel()
pc, file, _, _ := runtime.Caller(0) // get current caller. pc, file, _, _ := runtime.Caller(0) // get current caller.
f := runtime.FuncForPC(pc) f := runtime.FuncForPC(pc)
@ -36,10 +36,27 @@ func TestWithTrace(t *testing.T) {
ctx, done := WithTrace(Background()) ctx, done := WithTrace(Background())
defer done("this will be emitted at end of test") defer done("this will be emitted at end of test")
checkContextForValues(ctx, t, append(base, valueTestCase{ tests := append(base, valueTestCase{
key: "trace.func", key: "trace.func",
expected: f.Name(), expected: f.Name(),
})) })
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)
}
})
}
tracedFn := func() { tracedFn := func() {
parentID := ctx.Value("trace.id") // ensure the parent trace id is correct. parentID := ctx.Value("trace.id") // ensure the parent trace id is correct.
@ -49,13 +66,30 @@ func TestWithTrace(t *testing.T) {
ctx, done := WithTrace(ctx) ctx, done := WithTrace(ctx)
defer done("this should be subordinate to the other trace") defer done("this should be subordinate to the other trace")
time.Sleep(time.Second) time.Sleep(time.Second)
checkContextForValues(ctx, t, append(base, valueTestCase{ tests := append(base, valueTestCase{
key: "trace.func", key: "trace.func",
expected: f.Name(), expected: f.Name(),
}, valueTestCase{ }, valueTestCase{
key: "trace.parent.id", key: "trace.parent.id",
expected: parentID, expected: parentID,
})) })
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)
}
})
}
} }
tracedFn() tracedFn()
@ -67,20 +101,3 @@ type valueTestCase struct {
expected interface{} expected interface{}
notnilorempty bool // just check not empty/not nil notnilorempty bool // just check not empty/not nil
} }
func checkContextForValues(ctx context.Context, t *testing.T, tests []valueTestCase) {
t.Helper()
for _, tc := range tests {
v := ctx.Value(tc.key)
if tc.notnilorempty {
if v == nil || v == "" {
t.Fatalf("value was nil or empty for %q: %#v", tc.key, v)
}
continue
}
if v != tc.expected {
t.Fatalf("unexpected value for key %q: %v != %v", tc.key, v, tc.expected)
}
}
}