From a60166cee3d7ef1bfeba04ff44a69d9b84f8e416 Mon Sep 17 00:00:00 2001
From: Tengxiao Wang <126621870+twang-ps@users.noreply.github.com>
Date: Fri, 20 Sep 2024 15:04:10 -0700
Subject: [PATCH] Add SpeedUpForUT function to sched package (#2484) (#2486)

* Add SpeedUpForUT function to sched package

---------

(cherry picked from commit 2478442d0147651deedf2b0f376f2e0f3fd7ec70)

Signed-off-by: Tengxiao Wang <twang@purestorage.com>
---
 pkg/sched/intervals.go | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/pkg/sched/intervals.go b/pkg/sched/intervals.go
index 44ae6283f..0797aaa2c 100644
--- a/pkg/sched/intervals.go
+++ b/pkg/sched/intervals.go
@@ -27,6 +27,8 @@ const (
 var (
 	// speedUp advances the clock faster for tests.
 	speedUp = false
+	// speedUpIntervalForUT overrides the interval of schedules in unit tests; no-op if set to 0
+	speedUpIntervalForUT = time.Duration(0)
 )
 
 // SpeedUp advances teh clock faster for tests.
@@ -38,6 +40,10 @@ func inSpeedUp() bool {
 	return speedUp
 }
 
+func SpeedUpForUT(duration time.Duration) {
+	speedUpIntervalForUT = duration
+}
+
 type IntervalSpec struct {
 	Freq    string
 	Period  uint64 `yaml:"period,omitempty"`
@@ -560,6 +566,8 @@ func (p RetainIntervalImpl) nextAfter(t time.Time) time.Time {
 	newTime := p.iv.nextAfter(t)
 	if inSpeedUp() {
 		return t.Add(time.Minute)
+	} else if speedUpIntervalForUT != time.Duration(0) {
+		return t.Add(speedUpIntervalForUT)
 	}
 	return newTime
 }