sigs.k8s.io/kueue@v0.6.2/pkg/util/parallelize/parallelize_test.go (about)

     1  /*
     2  Copyright 2024 The Kubernetes Authors.
     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 parallelize
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"testing"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  )
    26  
    27  var errEven = errors.New("even")
    28  
    29  func TestUntil(t *testing.T) {
    30  	var result []int
    31  	cases := map[string]struct {
    32  		op         func(int) error
    33  		wantOutput []int
    34  		wantErr    error
    35  	}{
    36  		"double the index": {
    37  			op: func(i int) error {
    38  				result[i] = 2 * i
    39  				return nil
    40  			},
    41  			wantOutput: []int{0, 2, 4, 6, 8},
    42  		},
    43  		"error in even numbers": {
    44  			op: func(i int) error {
    45  				if i%2 == 0 {
    46  					return errEven
    47  				}
    48  				return nil
    49  			},
    50  			wantOutput: make([]int, 5),
    51  			wantErr:    errEven,
    52  		},
    53  	}
    54  	for name, tc := range cases {
    55  		t.Run(name, func(t *testing.T) {
    56  			result = make([]int, 5)
    57  			err := Until(context.Background(), len(result), tc.op)
    58  			if !errors.Is(err, tc.wantErr) {
    59  				t.Errorf("Got error %q, want %q", err, tc.wantErr)
    60  			}
    61  			if diff := cmp.Diff(tc.wantOutput, result); diff != "" {
    62  				t.Errorf("Processed result (-want,+got):\n%s", diff)
    63  			}
    64  		})
    65  	}
    66  }