| 1 |
// Copyright (c) 2016-2023 Uber Technologies, Inc. |
| 2 |
// |
| 3 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
// of this software and associated documentation files (the "Software"), to deal |
| 5 |
// in the Software without restriction, including without limitation the rights |
| 6 |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 |
// copies of the Software, and to permit persons to whom the Software is |
| 8 |
// furnished to do so, subject to the following conditions: |
| 9 |
// |
| 10 |
// The above copyright notice and this permission notice shall be included in |
| 11 |
// all copies or substantial portions of the Software. |
| 12 |
// |
| 13 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 |
// THE SOFTWARE. |
| 20 |
|
| 21 |
package zapfield |
| 22 |
|
| 23 |
import ( |
| 24 |
"sync" |
| 25 |
"testing" |
| 26 |
|
| 27 |
"github.com/stretchr/testify/assert" |
| 28 |
"go.uber.org/zap" |
| 29 |
"go.uber.org/zap/zapcore" |
| 30 |
) |
| 31 |
|
| 32 |
type ( |
| 33 |
MyKey string |
| 34 |
MyValue string |
| 35 |
MyValues []MyValue |
| 36 |
) |
| 37 |
|
| 38 |
func TestFieldConstructors(t *testing.T) { |
| 39 |
var ( |
| 40 |
key = MyKey("test key") |
| 41 |
value = MyValue("test value") |
| 42 |
values = []MyValue{ |
| 43 |
MyValue("test value 1"), |
| 44 |
MyValue("test value 2"), |
| 45 |
} |
| 46 |
) |
| 47 |
|
| 48 |
tests := []struct { |
| 49 |
name string |
| 50 |
expect zap.Field |
| 51 |
field zap.Field |
| 52 |
}{ |
| 53 |
{"Str", zap.Field{Type: zapcore.StringType, Key: "test key", String: "test value"}, Str(key, value)}, |
| 54 |
{"Strs", zap.Array("test key", stringArray[MyValue]{"test value 1", "test value 2"}), Strs(key, values)}, |
| 55 |
} |
| 56 |
|
| 57 |
for _, tt := range tests { |
| 58 |
if !assert.Equal(t, tt.expect, tt.field, "Unexpected output from convenience field constructor %s.", tt.name) { |
| 59 |
t.Logf("type expected: %T\nGot: %T", tt.expect.Interface, tt.field.Interface) |
| 60 |
} |
| 61 |
assertCanBeReused(t, tt.field) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func assertCanBeReused(t testing.TB, field zap.Field) { |
| 66 |
var wg sync.WaitGroup |
| 67 |
|
| 68 |
for i := 0; i < 100; i++ { |
| 69 |
enc := zapcore.NewMapObjectEncoder() |
| 70 |
|
| 71 |
// Ensure using the field in multiple encoders in separate goroutines |
| 72 |
// does not cause any races or panics. |
| 73 |
wg.Add(1) |
| 74 |
go func() { |
| 75 |
defer wg.Done() |
| 76 |
assert.NotPanics(t, func() { |
| 77 |
field.AddTo(enc) |
| 78 |
}, "Reusing a field should not cause issues") |
| 79 |
}() |
| 80 |
} |
| 81 |
|
| 82 |
wg.Wait() |
| 83 |
} |