func incrementSleepInBackground(fakeTimeService *fakeclock.FakeClock, delay time.Duration) chan struct{} { doneChan := make(chan struct{}) go func() { for { select { case <-doneChan: return default: if fakeTimeService.WatcherCount() > 0 { fakeTimeService.Increment(delay) } } } }() return doneChan }
fakeClock.Increment(5 * time.Second) Consistently(doneSleeping, Δ).ShouldNot(BeClosed()) fakeClock.Increment(4 * time.Second) Consistently(doneSleeping, Δ).ShouldNot(BeClosed()) fakeClock.Increment(1 * time.Second) Eventually(doneSleeping).Should(BeClosed()) }) }) Describe("WatcherCount", func() { Context("when a timer is created", func() { It("increments the watcher count", func() { fakeClock.NewTimer(time.Second) Ω(fakeClock.WatcherCount()).Should(Equal(1)) fakeClock.NewTimer(2 * time.Second) Ω(fakeClock.WatcherCount()).Should(Equal(2)) }) }) Context("when a timer fires", func() { It("increments the watcher count", func() { fakeClock.NewTimer(time.Second) Ω(fakeClock.WatcherCount()).Should(Equal(1)) fakeClock.Increment(time.Second) Ω(fakeClock.WatcherCount()).Should(Equal(0)) }) })
IsRetryable: true, AttemptErr: errors.New("third-error"), }, { IsRetryable: true, AttemptErr: errors.New("fourth-error"), }, }) // deadline between 2nd and 3rd attempts delay := 10 * time.Second timeoutRetryStrategy := NewTimeoutRetryStrategy(25*time.Second, delay, retryable, fakeTimeService, logger) doneChan := incrementSleepInBackground(fakeTimeService, delay) err := timeoutRetryStrategy.Try() close(doneChan) Expect(fakeTimeService.WatcherCount()).To(Equal(0)) Expect(err.Error()).To(ContainSubstring("third-error")) Expect(retryable.Attempts).To(Equal(3)) }) It("stops without a trailing delay", func() { retryable := newSimpleRetryable([]attemptOutput{ { IsRetryable: true, AttemptErr: errors.New("first-error"), }, { IsRetryable: true, AttemptErr: errors.New("second-error"), },
fakeClock.Increment(5 * time.Second) Consistently(doneSleeping, Δ).ShouldNot(BeClosed()) fakeClock.Increment(4 * time.Second) Consistently(doneSleeping, Δ).ShouldNot(BeClosed()) fakeClock.Increment(1 * time.Second) Eventually(doneSleeping).Should(BeClosed()) }) }) Describe("WatcherCount", func() { Context("when a timer is created", func() { It("increments the watcher count", func() { fakeClock.NewTimer(time.Second) Expect(fakeClock.WatcherCount()).To(Equal(1)) fakeClock.NewTimer(2 * time.Second) Expect(fakeClock.WatcherCount()).To(Equal(2)) }) }) Context("when a timer fires", func() { It("increments the watcher count", func() { fakeClock.NewTimer(time.Second) Expect(fakeClock.WatcherCount()).To(Equal(1)) fakeClock.Increment(time.Second) Expect(fakeClock.WatcherCount()).To(Equal(0)) }) })