github.com/chenbh/concourse/v6@v6.4.2/atc/lidar/check_rate_calculator_test.go (about)

     1  package lidar_test
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	"github.com/chenbh/concourse/v6/atc/lidar"
     8  	"github.com/chenbh/concourse/v6/atc/lidar/lidarfakes"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	"golang.org/x/time/rate"
    12  )
    13  
    14  var _ = Describe("CheckRateCalculator", func() {
    15  	var (
    16  		maxChecksPerSecond       int
    17  		resourceCheckingInterval time.Duration
    18  		fakeCheckableCounter     *lidarfakes.FakeCheckableCounter
    19  		checkRateCalculator      lidar.CheckRateCalculator
    20  		rateLimit                lidar.Limiter
    21  		calcErr                  error
    22  	)
    23  
    24  	BeforeEach(func() {
    25  		fakeCheckableCounter = new(lidarfakes.FakeCheckableCounter)
    26  		fakeCheckableCounter.CheckableCountReturns(600, nil)
    27  		resourceCheckingInterval = 1 * time.Minute
    28  	})
    29  
    30  	JustBeforeEach(func() {
    31  		checkRateCalculator = lidar.CheckRateCalculator{
    32  			MaxChecksPerSecond:       maxChecksPerSecond,
    33  			ResourceCheckingInterval: resourceCheckingInterval,
    34  			CheckableCounter:         fakeCheckableCounter,
    35  		}
    36  
    37  		rateLimit, calcErr = checkRateCalculator.RateLimiter()
    38  	})
    39  
    40  	Context("when max checks per second is -1", func() {
    41  		BeforeEach(func() {
    42  			maxChecksPerSecond = -1
    43  		})
    44  
    45  		It("returns unlimited checks per second", func() {
    46  			Expect(calcErr).ToNot(HaveOccurred())
    47  			Expect(rateLimit).To(Equal(rate.NewLimiter(rate.Inf, 1)))
    48  		})
    49  	})
    50  
    51  	Context("when max checks per second is 0", func() {
    52  		BeforeEach(func() {
    53  			maxChecksPerSecond = 0
    54  		})
    55  
    56  		It("calulates rate limit using the number of checkables and resource checking interval", func() {
    57  			Expect(calcErr).ToNot(HaveOccurred())
    58  			Expect(rateLimit).To(Equal(rate.NewLimiter(rate.Limit(10), 1)))
    59  		})
    60  
    61  		Context("when fetching the checkable count errors", func() {
    62  			BeforeEach(func() {
    63  				fakeCheckableCounter.CheckableCountReturns(0, errors.New("disaster"))
    64  			})
    65  
    66  			It("returns the error", func() {
    67  				Expect(calcErr).To(HaveOccurred())
    68  				Expect(calcErr).To(Equal(errors.New("disaster")))
    69  			})
    70  		})
    71  	})
    72  
    73  	Context("when max checks per second is greater than 0", func() {
    74  		BeforeEach(func() {
    75  			maxChecksPerSecond = 2
    76  		})
    77  
    78  		It("sets the rate limit to the max checks per second", func() {
    79  			Expect(calcErr).ToNot(HaveOccurred())
    80  			Expect(rateLimit).To(Equal(rate.NewLimiter(rate.Limit(2), 1)))
    81  		})
    82  	})
    83  })