storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/sync/errgroup/errgroup_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2017 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package errgroup
    18  
    19  import (
    20  	"fmt"
    21  	"reflect"
    22  	"testing"
    23  )
    24  
    25  func TestGroupWithNErrs(t *testing.T) {
    26  	err1 := fmt.Errorf("errgroup_test: 1")
    27  	err2 := fmt.Errorf("errgroup_test: 2")
    28  
    29  	cases := []struct {
    30  		errs []error
    31  	}{
    32  		{errs: []error{nil}},
    33  		{errs: []error{err1}},
    34  		{errs: []error{err1, nil}},
    35  		{errs: []error{err1, nil, err2}},
    36  	}
    37  
    38  	for j, tc := range cases {
    39  		t.Run(fmt.Sprintf("Test%d", j+1), func(t *testing.T) {
    40  			g := WithNErrs(len(tc.errs))
    41  			for i, err := range tc.errs {
    42  				err := err
    43  				g.Go(func() error { return err }, i)
    44  			}
    45  
    46  			gotErrs := g.Wait()
    47  			if !reflect.DeepEqual(gotErrs, tc.errs) {
    48  				t.Errorf("Expected %#v, got %#v", tc.errs, gotErrs)
    49  			}
    50  		})
    51  	}
    52  }