github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/util/batcher/batcher_test.go (about)

     1  package batcher_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/errors"
     7  
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     9  	"code.cloudfoundry.org/cli/util/batcher"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("Batcher", func() {
    15  	var (
    16  		calls            [][]string
    17  		warningsToReturn []ccv3.Warnings
    18  		errorsToReturn   []error
    19  	)
    20  
    21  	fakeCallback := func(guids []string) (ccv3.Warnings, error) {
    22  		index := len(calls)
    23  		calls = append(calls, guids)
    24  
    25  		var (
    26  			warnings ccv3.Warnings
    27  			err      error
    28  		)
    29  
    30  		if len(warningsToReturn) > index {
    31  			warnings = warningsToReturn[index]
    32  		}
    33  
    34  		if len(errorsToReturn) > index {
    35  			err = errorsToReturn[index]
    36  		}
    37  
    38  		return warnings, err
    39  	}
    40  
    41  	spreadGuids := func(start, end int) (result []string) {
    42  		for i := start; i < end; i++ {
    43  			result = append(result, fmt.Sprintf("fake-guid-%d", i))
    44  		}
    45  		return
    46  	}
    47  
    48  	BeforeEach(func() {
    49  		calls = nil
    50  		warningsToReturn = nil
    51  		errorsToReturn = nil
    52  	})
    53  
    54  	It("calls the callback", func() {
    55  		_, _ = batcher.RequestByGUID([]string{"one", "two", "three"}, fakeCallback)
    56  
    57  		Expect(calls).To(HaveLen(1))
    58  		Expect(calls[0]).To(Equal([]string{"one", "two", "three"}))
    59  	})
    60  
    61  	When("the guids list exceeds the batch size", func() {
    62  		It("calls the callback multiple times", func() {
    63  			_, _ = batcher.RequestByGUID(spreadGuids(0, 520), fakeCallback)
    64  
    65  			Expect(calls).To(HaveLen(3))
    66  			Expect(calls[0]).To(Equal(spreadGuids(0, 200)))
    67  			Expect(calls[1]).To(Equal(spreadGuids(200, 400)))
    68  			Expect(calls[2]).To(Equal(spreadGuids(400, 520)))
    69  		})
    70  	})
    71  
    72  	When("the callback returns warnings", func() {
    73  		BeforeEach(func() {
    74  			warningsToReturn = []ccv3.Warnings{
    75  				{"one", "two"},
    76  				{"three", "four"},
    77  				{},
    78  				{"five"},
    79  			}
    80  		})
    81  
    82  		It("returns all the accumulated warnings", func() {
    83  			warnings, _ := batcher.RequestByGUID(spreadGuids(0, 960), fakeCallback)
    84  
    85  			Expect(warnings).To(ConsistOf("one", "two", "three", "four", "five"))
    86  		})
    87  	})
    88  
    89  	When("the callback returns an error", func() {
    90  		BeforeEach(func() {
    91  			warningsToReturn = []ccv3.Warnings{
    92  				{"one", "two"},
    93  				{"three", "four"},
    94  				{"five"},
    95  				{"six", "seven"},
    96  			}
    97  
    98  			errorsToReturn = []error{
    99  				nil,
   100  				nil,
   101  				errors.New("bang"),
   102  				nil,
   103  			}
   104  		})
   105  
   106  		It("returns the error and accumulated warnings", func() {
   107  			warnings, err := batcher.RequestByGUID(spreadGuids(0, 960), fakeCallback)
   108  
   109  			Expect(calls).To(HaveLen(3))
   110  			Expect(warnings).To(ConsistOf("one", "two", "three", "four", "five"))
   111  			Expect(err).To(MatchError("bang"))
   112  		})
   113  	})
   114  })