From f4be328bff4cc94435d70ea69e6c17ac2f05c20d Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 30 Apr 2023 15:41:20 +0200 Subject: [PATCH] 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 --- context/trace_test.go | 61 +++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/context/trace_test.go b/context/trace_test.go index 2fcf1e65..4ee530bf 100644 --- a/context/trace_test.go +++ b/context/trace_test.go @@ -1,7 +1,6 @@ package context import ( - "context" "runtime" "testing" "time" @@ -9,6 +8,7 @@ import ( // TestWithTrace ensures that tracing has the expected values in the context. func TestWithTrace(t *testing.T) { + t.Parallel() pc, file, _, _ := runtime.Caller(0) // get current caller. f := runtime.FuncForPC(pc) @@ -36,10 +36,27 @@ func TestWithTrace(t *testing.T) { ctx, done := WithTrace(Background()) defer done("this will be emitted at end of test") - checkContextForValues(ctx, t, append(base, valueTestCase{ + tests := append(base, valueTestCase{ key: "trace.func", 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() { 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) defer done("this should be subordinate to the other trace") time.Sleep(time.Second) - checkContextForValues(ctx, t, append(base, valueTestCase{ + tests := append(base, valueTestCase{ key: "trace.func", expected: f.Name(), }, valueTestCase{ key: "trace.parent.id", 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() @@ -67,20 +101,3 @@ type valueTestCase struct { expected interface{} 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) - } - } -}