Skip to content

Commit

Permalink
unit test for context value logging
Browse files Browse the repository at this point in the history
This reuses the setup code and checks in the benchmark functions.
  • Loading branch information
pohly committed Nov 20, 2022
1 parent 9a6d803 commit dc20a30
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions examples/benchmarks/contextvalues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand All @@ -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)
}

0 comments on commit dc20a30

Please sign in to comment.