| 1 | // Copyright (c) 2016 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 zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/ |
| 22 | |
| 23 | import ( |
| 24 | "syncscip-go gomod github.com/golang/go/src go1.19 sync/" |
| 25 | "testingscip-go gomod github.com/golang/go/src go1.19 testing/" |
| 26 | |
| 27 | "go.uber.org/zap/internal/exitscip-go gomod go.uber.org/zap . `go.uber.org/zap/internal/exit`/" |
| 28 | |
| 29 | "github.com/stretchr/testify/assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/" |
| 30 | ) |
| 31 | |
| 32 | func assertGoexitscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/assertGoexit().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#, f func()) { |
| 33 | var finished bool |
| 34 | recovered := make(chan interface{}) |
| 35 | go func() { |
| 36 | defer func() { |
| 37 | recovered <- recover() |
| 38 | }() |
| 39 | |
| 40 | f() |
| 41 | finished = true |
| 42 | }() |
| 43 | |
| 44 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Nilscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Nil().(t, <-recovered, "Goexit should cause recover to return nil") |
| 45 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Falsescip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/False().(t, finished, "Goroutine should not finish after Goexit") |
| 46 | } |
| 47 | |
| 48 | func TestPutNilEntryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/TestPutNilEntry().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 49 | // Pooling nil entries defeats the purpose. |
| 50 | var wg syncscip-go gomod github.com/golang/go/src go1.19 sync/.WaitGroupscip-go gomod github.com/golang/go/src go1.19 sync/WaitGroup# |
| 51 | wg.Addscip-go gomod github.com/golang/go/src go1.19 sync/WaitGroup#Add().(2) |
| 52 | |
| 53 | go func() { |
| 54 | defer wg.Donescip-go gomod github.com/golang/go/src go1.19 sync/WaitGroup#Done().() |
| 55 | for i := 0; i < 1000; i++ { |
| 56 | putCheckedEntryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/putCheckedEntry().(nil) |
| 57 | } |
| 58 | }() |
| 59 | |
| 60 | go func() { |
| 61 | defer wg.Donescip-go gomod github.com/golang/go/src go1.19 sync/WaitGroup#Done().() |
| 62 | for i := 0; i < 1000; i++ { |
| 63 | ce := getCheckedEntryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/getCheckedEntry().() |
| 64 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.NotNilscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/NotNil().(t, ce, "Expected only non-nil CheckedEntries in pool.") |
| 65 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Falsescip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/False().(t, ce.dirtyscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry#dirty., "Unexpected dirty bit set.") |
| 66 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Nilscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Nil().(t, ce.ErrorOutputscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry#ErrorOutput., "Non-nil ErrorOutput.") |
| 67 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Nilscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Nil().(t, ce.afterscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry#after., "Unexpected terminal behavior.") |
| 68 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Equal().(t, 0, len(ce.coresscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry#cores.), "Expected empty slice of cores.") |
| 69 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Truescip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/True().(t, cap(ce.coresscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry#cores.) > 0, "Expected pooled CheckedEntries to pre-allocate slice of Cores.") |
| 70 | } |
| 71 | }() |
| 72 | |
| 73 | wg.Waitscip-go gomod github.com/golang/go/src go1.19 sync/WaitGroup#Wait().() |
| 74 | } |
| 75 | |
| 76 | func TestEntryCallerscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/TestEntryCaller().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 77 | tests := []struct { |
| 78 | caller EntryCallerscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/EntryCaller# |
| 79 | full string |
| 80 | short string |
| 81 | }{ |
| 82 | { |
| 83 | caller: NewEntryCallerscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/NewEntryCaller().(100, "/path/to/foo.go", 42, false), |
| 84 | full: "undefined", |
| 85 | short: "undefined", |
| 86 | }, |
| 87 | { |
| 88 | caller: NewEntryCallerscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/NewEntryCaller().(100, "/path/to/foo.go", 42, true), |
| 89 | full: "/path/to/foo.go:42", |
| 90 | short: "to/foo.go:42", |
| 91 | }, |
| 92 | { |
| 93 | caller: NewEntryCallerscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/NewEntryCaller().(100, "to/foo.go", 42, true), |
| 94 | full: "to/foo.go:42", |
| 95 | short: "to/foo.go:42", |
| 96 | }, |
| 97 | } |
| 98 | |
| 99 | for _, tt := range tests { |
| 100 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Equal().(t, tt.full, tt.caller.Stringscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/EntryCaller#String().(), "Unexpected string from EntryCaller.") |
| 101 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Equal().(t, tt.full, tt.caller.FullPathscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/EntryCaller#FullPath().(), "Unexpected FullPath from EntryCaller.") |
| 102 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Equal().(t, tt.short, tt.caller.TrimmedPathscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/EntryCaller#TrimmedPath().(), "Unexpected TrimmedPath from EntryCaller.") |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func TestCheckedEntryWritescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/TestCheckedEntryWrite().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 107 | t.Runscip-go gomod github.com/golang/go/src go1.19 testing/T#Run().("nil is safe", func(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 108 | var ce *CheckedEntryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry# |
| 109 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.NotPanicsscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/NotPanics().(t, func() { ce.Writescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry#Write().() }, "Unexpected panic writing nil CheckedEntry.") |
| 110 | }) |
| 111 | |
| 112 | t.Runscip-go gomod github.com/golang/go/src go1.19 testing/T#Run().("WriteThenPanic", func(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 113 | var ce *CheckedEntryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry# |
| 114 | ce = ce.Afterscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry#After().(Entryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Entry#{}, WriteThenPanicscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/WriteThenPanic.) |
| 115 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Panicsscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Panics().(t, func() { ce.Writescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry#Write().() }, "Expected to panic when WriteThenPanic is set.") |
| 116 | }) |
| 117 | |
| 118 | t.Runscip-go gomod github.com/golang/go/src go1.19 testing/T#Run().("WriteThenGoexit", func(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 119 | var ce *CheckedEntryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry# |
| 120 | ce = ce.Afterscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry#After().(Entryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Entry#{}, WriteThenGoexitscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/WriteThenGoexit.) |
| 121 | assertGoexitscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/assertGoexit().(t, func() { ce.Writescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry#Write().() }) |
| 122 | }) |
| 123 | |
| 124 | t.Runscip-go gomod github.com/golang/go/src go1.19 testing/T#Run().("WriteThenFatal", func(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 125 | var ce *CheckedEntryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry# |
| 126 | ce = ce.Afterscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry#After().(Entryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Entry#{}, WriteThenFatalscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/WriteThenFatal.) |
| 127 | stub := exitscip-go gomod go.uber.org/zap . `go.uber.org/zap/internal/exit`/.WithStubscip-go gomod go.uber.org/zap . `go.uber.org/zap/internal/exit`/WithStub().(func() { |
| 128 | ce.Writescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry#Write().() |
| 129 | }) |
| 130 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Truescip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/True().(t, stub.Exitedscip-go gomod go.uber.org/zap . `go.uber.org/zap/internal/exit`/StubbedExit#Exited., "Expected to exit when WriteThenFatal is set.") |
| 131 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Equal().(t, 1, stub.Codescip-go gomod go.uber.org/zap . `go.uber.org/zap/internal/exit`/StubbedExit#Code., "Expected to exit when WriteThenFatal is set.") |
| 132 | }) |
| 133 | |
| 134 | t.Runscip-go gomod github.com/golang/go/src go1.19 testing/T#Run().("After", func(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 135 | var ce *CheckedEntryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry# |
| 136 | hook := &customHookscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/customHook#{} |
| 137 | ce = ce.Afterscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry#After().(Entryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Entry#{}, hook) |
| 138 | ce.Writescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry#Write().() |
| 139 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Truescip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/True().(t, hook.calledscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/customHook#called., "Expected to call custom action after Write.") |
| 140 | }) |
| 141 | } |
| 142 | |
| 143 | type customHookscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/customHook# struct { |
| 144 | calledscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/customHook#called. bool |
| 145 | } |
| 146 | |
| 147 | func (c *customHookscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/customHook#) OnWritescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/customHook#OnWrite().(_ *CheckedEntryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry#, _ []Fieldscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Field#) { |
| 148 | c.calledscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/customHook#called. = true |
| 149 | } |