multierr_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package multierr_test
  4. import (
  5. "errors"
  6. "fmt"
  7. "io"
  8. "testing"
  9. qt "github.com/frankban/quicktest"
  10. "github.com/google/go-cmp/cmp"
  11. "github.com/google/go-cmp/cmp/cmpopts"
  12. "tailscale.com/util/multierr"
  13. )
  14. func TestAll(t *testing.T) {
  15. C := qt.New(t)
  16. eqErr := qt.CmpEquals(cmpopts.EquateErrors())
  17. type E = []error
  18. N := multierr.New
  19. a := errors.New("a")
  20. b := errors.New("b")
  21. c := errors.New("c")
  22. d := errors.New("d")
  23. x := errors.New("x")
  24. abcd := E{a, b, c, d}
  25. tests := []struct {
  26. In E // input to New
  27. WantNil bool // want nil returned?
  28. WantSingle error // if non-nil, want this single error returned
  29. WantErrors []error // if non-nil, want an Error composed of these errors returned
  30. }{
  31. {In: nil, WantNil: true},
  32. {In: E{nil}, WantNil: true},
  33. {In: E{nil, nil}, WantNil: true},
  34. {In: E{a}, WantSingle: a},
  35. {In: E{a, nil}, WantSingle: a},
  36. {In: E{nil, a}, WantSingle: a},
  37. {In: E{nil, a, nil}, WantSingle: a},
  38. {In: E{a, b}, WantErrors: E{a, b}},
  39. {In: E{nil, a, nil, b, nil}, WantErrors: E{a, b}},
  40. {In: E{a, b, N(c, d)}, WantErrors: E{a, b, c, d}},
  41. {In: E{a, N(b, c), d}, WantErrors: E{a, b, c, d}},
  42. {In: E{N(a, b), c, d}, WantErrors: E{a, b, c, d}},
  43. {In: E{N(a, b), N(c, d)}, WantErrors: E{a, b, c, d}},
  44. {In: E{nil, N(a, nil, b), nil, N(c, d)}, WantErrors: E{a, b, c, d}},
  45. {In: E{N(a, N(b, N(c, N(d))))}, WantErrors: E{a, b, c, d}},
  46. {In: E{N(N(N(N(a), b), c), d)}, WantErrors: E{a, b, c, d}},
  47. {In: E{N(abcd...)}, WantErrors: E{a, b, c, d}},
  48. {In: E{N(abcd...), N(abcd...)}, WantErrors: E{a, b, c, d, a, b, c, d}},
  49. }
  50. for _, test := range tests {
  51. got := multierr.New(test.In...)
  52. if test.WantNil {
  53. C.Assert(got, qt.IsNil)
  54. continue
  55. }
  56. if test.WantSingle != nil {
  57. C.Assert(got, eqErr, test.WantSingle)
  58. continue
  59. }
  60. ee, _ := got.(multierr.Error)
  61. C.Assert(ee.Errors(), eqErr, test.WantErrors)
  62. for _, e := range test.WantErrors {
  63. C.Assert(ee.Is(e), qt.IsTrue)
  64. }
  65. C.Assert(ee.Is(x), qt.IsFalse)
  66. }
  67. }
  68. func TestRange(t *testing.T) {
  69. C := qt.New(t)
  70. errA := errors.New("A")
  71. errB := errors.New("B")
  72. errC := errors.New("C")
  73. errD := errors.New("D")
  74. errCD := multierr.New(errC, errD)
  75. errCD1 := fmt.Errorf("1:%w", errCD)
  76. errE := errors.New("E")
  77. errE1 := fmt.Errorf("1:%w", errE)
  78. errE2 := fmt.Errorf("2:%w", errE1)
  79. errF := errors.New("F")
  80. root := multierr.New(errA, errB, errCD1, errE2, errF)
  81. var got []error
  82. want := []error{root, errA, errB, errCD1, errCD, errC, errD, errE2, errE1, errE, errF}
  83. multierr.Range(root, func(err error) bool {
  84. got = append(got, err)
  85. return true
  86. })
  87. C.Assert(got, qt.CmpEquals(cmp.Comparer(func(x, y error) bool {
  88. return x.Error() == y.Error()
  89. })), want)
  90. }
  91. var sink error
  92. func BenchmarkEmpty(b *testing.B) {
  93. b.ReportAllocs()
  94. for i := 0; i < b.N; i++ {
  95. sink = multierr.New(nil, nil, nil, multierr.Error{})
  96. }
  97. }
  98. func BenchmarkNonEmpty(b *testing.B) {
  99. merr := multierr.New(io.ErrShortBuffer, io.ErrNoProgress)
  100. b.ReportAllocs()
  101. for i := 0; i < b.N; i++ {
  102. sink = multierr.New(io.ErrUnexpectedEOF, merr, io.ErrClosedPipe)
  103. }
  104. }