-
Notifications
You must be signed in to change notification settings - Fork 694
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
logging: Reversed policy of fields - from immutable to fresh winning. (…
…#616) * logging: Reversed policy of fields - from immutable to fresh winning. Fixes: #613 Signed-off-by: bwplotka <bwplotka@gmail.com> * Addressed comments. Signed-off-by: bwplotka <bwplotka@gmail.com> --------- Signed-off-by: bwplotka <bwplotka@gmail.com>
- Loading branch information
Showing
4 changed files
with
56 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) The go-grpc-middleware Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package logging | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFieldsInjectExtractFromContext(t *testing.T) { | ||
c := context.Background() | ||
f := ExtractFields(c) | ||
require.Equal(t, Fields(nil), f) | ||
|
||
f = f.AppendUnique([]any{"a", "1", "b", "2"}) | ||
require.Equal(t, Fields{"a", "1", "b", "2"}, f) | ||
|
||
c2 := InjectFields(c, f) | ||
|
||
// First context should be untouched. | ||
f = ExtractFields(c) | ||
require.Equal(t, Fields(nil), f) | ||
f = ExtractFields(c2) | ||
require.Equal(t, Fields{"a", "1", "b", "2"}, f) | ||
|
||
f = Fields{"a", "changed"}.WithUnique(f) | ||
require.Equal(t, Fields{"a", "changed", "b", "2"}, f) | ||
|
||
c3 := InjectFields(c, f) | ||
|
||
// Old contexts should be untouched. | ||
f = ExtractFields(c) | ||
require.Equal(t, Fields(nil), f) | ||
f = ExtractFields(c2) | ||
require.Equal(t, Fields{"a", "1", "b", "2"}, f) | ||
f = ExtractFields(c3) | ||
require.Equal(t, Fields{"a", "changed", "b", "2"}, f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters