| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package errorhandler |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"errors" |
| 9 |
"log" |
| 10 |
"os" |
| 11 |
"strings" |
| 12 |
"sync" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/stretchr/testify/assert" |
| 16 |
) |
| 17 |
|
| 18 |
type fnErrHandler func(error) |
| 19 |
|
| 20 |
func (f fnErrHandler) Handle(err error) { f(err) } |
| 21 |
|
| 22 |
var noopEH = fnErrHandler(func(error) {}) |
| 23 |
|
| 24 |
type nonComparableErrorHandler struct { |
| 25 |
ErrorHandler |
| 26 |
|
| 27 |
nonComparable func() //nolint:unused // This is not called. |
| 28 |
} |
| 29 |
|
| 30 |
func resetForTest(t testing.TB) { |
| 31 |
t.Cleanup(func() { |
| 32 |
globalErrorHandler = defaultErrorHandler() |
| 33 |
delegateErrorHandlerOnce = sync.Once{} |
| 34 |
}) |
| 35 |
} |
| 36 |
|
| 37 |
func TestErrDelegator(t *testing.T) { |
| 38 |
buf := new(bytes.Buffer) |
| 39 |
log.Default().SetOutput(buf) |
| 40 |
t.Cleanup(func() { log.Default().SetOutput(os.Stderr) }) |
| 41 |
|
| 42 |
e := &ErrDelegator{} |
| 43 |
|
| 44 |
err := errors.New("testing") |
| 45 |
e.Handle(err) |
| 46 |
|
| 47 |
got := buf.String() |
| 48 |
if !strings.Contains(got, err.Error()) { |
| 49 |
t.Error("default handler did not log") |
| 50 |
} |
| 51 |
buf.Reset() |
| 52 |
|
| 53 |
var gotErr error |
| 54 |
e.setDelegate(fnErrHandler(func(e error) { gotErr = e })) |
| 55 |
e.Handle(err) |
| 56 |
|
| 57 |
if buf.String() != "" { |
| 58 |
t.Error("delegate not set") |
| 59 |
} else if !errors.Is(gotErr, err) { |
| 60 |
t.Error("error not passed to delegate") |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func TestSetErrorHandler(t *testing.T) { |
| 65 |
t.Run("Set With default is a noop", func(t *testing.T) { |
| 66 |
resetForTest(t) |
| 67 |
SetErrorHandler(GetErrorHandler()) |
| 68 |
|
| 69 |
eh, ok := GetErrorHandler().(*ErrDelegator) |
| 70 |
if !ok { |
| 71 |
t.Fatal("Global ErrorHandler should be the default ErrorHandler") |
| 72 |
} |
| 73 |
|
| 74 |
if eh.delegate.Load() != nil { |
| 75 |
t.Fatal("ErrorHandler should not delegate when setting itself") |
| 76 |
} |
| 77 |
}) |
| 78 |
|
| 79 |
t.Run("First Set() should replace the delegate", func(t *testing.T) { |
| 80 |
resetForTest(t) |
| 81 |
|
| 82 |
SetErrorHandler(noopEH) |
| 83 |
|
| 84 |
_, ok := GetErrorHandler().(*ErrDelegator) |
| 85 |
if ok { |
| 86 |
t.Fatal("Global ErrorHandler was not changed") |
| 87 |
} |
| 88 |
}) |
| 89 |
|
| 90 |
t.Run("Set() should delegate existing ErrorHandlers", func(t *testing.T) { |
| 91 |
resetForTest(t) |
| 92 |
|
| 93 |
eh := GetErrorHandler() |
| 94 |
SetErrorHandler(noopEH) |
| 95 |
|
| 96 |
errDel, ok := eh.(*ErrDelegator) |
| 97 |
if !ok { |
| 98 |
t.Fatal("Wrong ErrorHandler returned") |
| 99 |
} |
| 100 |
|
| 101 |
if errDel.delegate.Load() == nil { |
| 102 |
t.Fatal("The ErrDelegator should have a delegate") |
| 103 |
} |
| 104 |
}) |
| 105 |
|
| 106 |
t.Run("non-comparable types should not panic", func(t *testing.T) { |
| 107 |
resetForTest(t) |
| 108 |
|
| 109 |
eh := nonComparableErrorHandler{} |
| 110 |
assert.NotPanics(t, func() { SetErrorHandler(eh) }, "delegate") |
| 111 |
assert.NotPanics(t, func() { SetErrorHandler(eh) }, "replacement") |
| 112 |
}) |
| 113 |
} |