tailscale/util/multierr/multierr_test.go
Andrew Dunham 63b6a19ffa control/controlhttp, util/multierr: deduplicate context errors
Add a new function to the util/multierr package that allows
deduplicating the `context.Canceled` and `context.DeadlineExceeded`
errors from a slice, and then use that to reduce log spam in
controlhttp.

Updates #14233

Signed-off-by: Andrew Dunham <andrew@du.nham.ca>
Change-Id: I5a121124fe27c4449705ba35de6faf83665db5fe
2024-11-26 18:05:27 -05:00

181 lines
4.4 KiB
Go

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package multierr_test
import (
"context"
"errors"
"fmt"
"io"
"testing"
qt "github.com/frankban/quicktest"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"tailscale.com/util/multierr"
)
func TestAll(t *testing.T) {
C := qt.New(t)
eqErr := qt.CmpEquals(cmpopts.EquateErrors())
type E = []error
N := multierr.New
a := errors.New("a")
b := errors.New("b")
c := errors.New("c")
d := errors.New("d")
x := errors.New("x")
abcd := E{a, b, c, d}
tests := []struct {
In E // input to New
WantNil bool // want nil returned?
WantSingle error // if non-nil, want this single error returned
WantErrors []error // if non-nil, want an Error composed of these errors returned
}{
{In: nil, WantNil: true},
{In: E{nil}, WantNil: true},
{In: E{nil, nil}, WantNil: true},
{In: E{a}, WantSingle: a},
{In: E{a, nil}, WantSingle: a},
{In: E{nil, a}, WantSingle: a},
{In: E{nil, a, nil}, WantSingle: a},
{In: E{a, b}, WantErrors: E{a, b}},
{In: E{nil, a, nil, b, nil}, WantErrors: E{a, b}},
{In: E{a, b, N(c, d)}, WantErrors: E{a, b, c, d}},
{In: E{a, N(b, c), d}, WantErrors: E{a, b, c, d}},
{In: E{N(a, b), c, d}, WantErrors: E{a, b, c, d}},
{In: E{N(a, b), N(c, d)}, WantErrors: E{a, b, c, d}},
{In: E{nil, N(a, nil, b), nil, N(c, d)}, WantErrors: E{a, b, c, d}},
{In: E{N(a, N(b, N(c, N(d))))}, WantErrors: E{a, b, c, d}},
{In: E{N(N(N(N(a), b), c), d)}, WantErrors: E{a, b, c, d}},
{In: E{N(abcd...)}, WantErrors: E{a, b, c, d}},
{In: E{N(abcd...), N(abcd...)}, WantErrors: E{a, b, c, d, a, b, c, d}},
}
for _, test := range tests {
got := multierr.New(test.In...)
if test.WantNil {
C.Assert(got, qt.IsNil)
continue
}
if test.WantSingle != nil {
C.Assert(got, eqErr, test.WantSingle)
continue
}
ee, _ := got.(multierr.Error)
C.Assert(ee.Errors(), eqErr, test.WantErrors)
for _, e := range test.WantErrors {
C.Assert(ee.Is(e), qt.IsTrue)
}
C.Assert(ee.Is(x), qt.IsFalse)
}
}
func TestRange(t *testing.T) {
C := qt.New(t)
errA := errors.New("A")
errB := errors.New("B")
errC := errors.New("C")
errD := errors.New("D")
errCD := multierr.New(errC, errD)
errCD1 := fmt.Errorf("1:%w", errCD)
errE := errors.New("E")
errE1 := fmt.Errorf("1:%w", errE)
errE2 := fmt.Errorf("2:%w", errE1)
errF := errors.New("F")
root := multierr.New(errA, errB, errCD1, errE2, errF)
var got []error
want := []error{root, errA, errB, errCD1, errCD, errC, errD, errE2, errE1, errE, errF}
multierr.Range(root, func(err error) bool {
got = append(got, err)
return true
})
C.Assert(got, qt.CmpEquals(cmp.Comparer(func(x, y error) bool {
return x.Error() == y.Error()
})), want)
}
func TestDeduplicateContextErrors(t *testing.T) {
testError := errors.New("test error")
tests := []struct {
name string
input []error
want []error
}{
{name: "nil", input: nil, want: nil},
{name: "empty", input: []error{}, want: []error{}},
{name: "single", input: []error{testError}, want: []error{testError}},
{
name: "duplicate_non_context",
input: []error{testError, testError},
want: []error{testError, testError},
},
{
name: "single_context",
input: []error{context.Canceled},
want: []error{context.Canceled},
},
{
name: "duplicate_context",
input: []error{testError, context.Canceled, context.Canceled},
want: []error{testError, context.Canceled},
},
{
name: "duplicate_context_mixed",
input: []error{
testError,
context.Canceled,
context.Canceled,
testError,
context.DeadlineExceeded,
context.DeadlineExceeded,
},
want: []error{
testError,
context.Canceled,
testError,
context.DeadlineExceeded,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := multierr.DeduplicateContextErrors(tt.input)
if diff := cmp.Diff(tt.want, got, cmpopts.EquateErrors()); diff != "" {
t.Errorf("DeduplicateContextErrors() mismatch (-want +got):\n%s", diff)
}
})
}
}
var sink error
func BenchmarkEmpty(b *testing.B) {
b.ReportAllocs()
for range b.N {
sink = multierr.New(nil, nil, nil, multierr.Error{})
}
}
func BenchmarkNonEmpty(b *testing.B) {
merr := multierr.New(io.ErrShortBuffer, io.ErrNoProgress)
b.ReportAllocs()
for range b.N {
sink = multierr.New(io.ErrUnexpectedEOF, merr, io.ErrClosedPipe)
}
}