go.uber.org/yarpc@v1.72.1/internal/errorsync/err_test.go (about) 1 // Copyright (c) 2022 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 errorsync 22 23 import ( 24 "errors" 25 "testing" 26 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestErrorWaiter(t *testing.T) { 31 one := errors.New("1") 32 two := errors.New("2") 33 34 tests := []struct { 35 desc string 36 errs []error 37 want []error 38 }{ 39 { 40 "nothing", 41 nil, 42 nil, 43 }, 44 { 45 "empty list", 46 []error{}, 47 nil, 48 }, 49 { 50 "no errors", 51 []error{nil, nil, nil}, 52 nil, 53 }, 54 { 55 "single error", 56 []error{nil, one, nil}, 57 []error{one}, 58 }, 59 { 60 "multiple errors", 61 []error{nil, one, two, nil}, 62 []error{one, two}, 63 }, 64 } 65 66 for _, tt := range tests { 67 want := make(map[error]struct{}) 68 for _, err := range tt.want { 69 want[err] = struct{}{} 70 } 71 72 var ew ErrorWaiter 73 for _, err := range tt.errs { 74 // Need to create a local variable to make sure that the correct 75 // value is used by the closure since the value 'err' points to 76 // will change between iterations. 77 errLocal := err 78 ew.Submit(func() error { return errLocal }) 79 } 80 81 got := make(map[error]struct{}) 82 for _, err := range ew.Wait() { 83 got[err] = struct{}{} 84 } 85 86 assert.Equal(t, want, got, tt.desc) 87 } 88 }