From dc20a30955f09eb6dc5f5a90ef11d82a4a080fad Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Fri, 18 Nov 2022 13:59:18 +0100 Subject: [PATCH] unit test for context value logging This reuses the setup code and checks in the benchmark functions. --- examples/benchmarks/contextvalues_test.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/examples/benchmarks/contextvalues_test.go b/examples/benchmarks/contextvalues_test.go index 5a62f4c..0f4e39d 100644 --- a/examples/benchmarks/contextvalues_test.go +++ b/examples/benchmarks/contextvalues_test.go @@ -89,16 +89,16 @@ func useContext(ctx context.Context, j, mod, v int) { const expectedOutput = `{"logger":"","level":0,"msg":"ping","i":1,"j":2,"string":"hello world","int":1,"float":1}` -func setup(b *testing.B, v int, expectedCalls int64) context.Context { +func setup(tb testing.TB, v int, expectedCalls int64) context.Context { var actualCalls int64 - b.Cleanup(func() { + tb.Cleanup(func() { if actualCalls != expectedCalls { - b.Errorf("expected %d calls to Info, got %d", expectedCalls, actualCalls) + tb.Errorf("expected %d calls to Info, got %d", expectedCalls, actualCalls) } }) logger := funcr.NewJSON(func(actualOutput string) { if actualOutput != expectedOutput { - b.Fatalf("expected %s, got %s", expectedOutput, actualOutput) + tb.Fatalf("expected %s, got %s", expectedOutput, actualOutput) } actualCalls++ }, funcr.Options{}) @@ -108,3 +108,9 @@ func setup(b *testing.B, v int, expectedCalls int64) context.Context { logger = logger.WithContextValues(logr.ContextKey{Key: contextKey2{}, Name: "j"}) return logr.NewContext(context.Background(), logger) } + +func TestFromContext(t *testing.T) { + expectedCalls := int64(iterationsPerOp) / 100 + ctx := setup(t, 1, expectedCalls) + newContext(ctx, 0, 1, 0) +}