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 18, 2022
1 parent b6c08cc commit 10d9593
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions examples/benchmarks/contextvalues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ 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 discardWithV(b *testing.B, v int) logr.Logger {
func discardWithV(tb testing.TB, v int) logr.Logger {
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)
}
calls++
}, funcr.Options{
Expand All @@ -123,3 +123,15 @@ func discardWithV(b *testing.B, v int) logr.Logger {
}

var calls int64

func TestFromContext(t *testing.T) {
ctx := logr.NewContext(context.Background(), discardWithV(t, 1))
defer func() {
expected := int64(1)
if calls != expected {
t.Errorf("expected %d calls to Info, got %d", expected, calls)
}
calls = 0
}()
newContext(ctx, 0, 1, 0)
}

0 comments on commit 10d9593

Please sign in to comment.