test(controller): increase timeout and refactor toggle registry test (#5518)

* test(controller): increase timeout and refactor toggle registry test

* Update controller/controller_test.go TestToggleRegistry deadline

Co-authored-by: Michel Loiseleur <97035654+mloiseleur@users.noreply.github.com>

---------

Co-authored-by: Michel Loiseleur <97035654+mloiseleur@users.noreply.github.com>
This commit is contained in:
kaikai 2025-06-13 16:39:03 +09:00 committed by GitHub
parent f5a366767d
commit 662fb3652d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -747,10 +747,12 @@ type toggleRegistry struct {
failCountMu sync.Mutex // protects failCount failCountMu sync.Mutex // protects failCount
} }
const toggleRegistryFailureCount = 3
func (r *toggleRegistry) Records(ctx context.Context) ([]*endpoint.Endpoint, error) { func (r *toggleRegistry) Records(ctx context.Context) ([]*endpoint.Endpoint, error) {
r.failCountMu.Lock() r.failCountMu.Lock()
defer r.failCountMu.Unlock() defer r.failCountMu.Unlock()
if r.failCount < 3 { if r.failCount < toggleRegistryFailureCount {
r.failCount++ r.failCount++
return nil, provider.SoftError return nil, provider.SoftError
} }
@ -766,12 +768,13 @@ func TestToggleRegistry(t *testing.T) {
cfg := getTestConfig() cfg := getTestConfig()
r := &toggleRegistry{} r := &toggleRegistry{}
interval := 10 * time.Millisecond
ctrl := &Controller{ ctrl := &Controller{
Source: source, Source: source,
Registry: r, Registry: r,
Policy: &plan.SyncPolicy{}, Policy: &plan.SyncPolicy{},
ManagedRecordTypes: cfg.ManagedDNSRecordTypes, ManagedRecordTypes: cfg.ManagedDNSRecordTypes,
Interval: 10 * time.Millisecond, Interval: interval,
} }
ctrl.nextRunAt = time.Now().Add(-time.Millisecond) ctrl.nextRunAt = time.Now().Add(-time.Millisecond)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
@ -781,19 +784,23 @@ func TestToggleRegistry(t *testing.T) {
close(stopped) close(stopped)
}() }()
// Wait up to 2 seconds for failCount to reach at least 3 // Wait up to 1 minute for failCount to reach at least 3
deadline := time.Now().Add(2 * time.Second) // The timeout serves as a safety net against infinite loops while being
// sufficiently large to accommodate slow CI environments
deadline := time.Now().Add(15 * time.Second)
for { for {
r.failCountMu.Lock() r.failCountMu.Lock()
count := r.failCount count := r.failCount
r.failCountMu.Unlock() r.failCountMu.Unlock()
if count >= 3 { if count >= toggleRegistryFailureCount {
break break
} }
if time.Now().After(deadline) { if time.Now().After(deadline) {
break break
} }
time.Sleep(10 * time.Millisecond) // Sleep for the controller interval to avoid busy waiting
// since the controller won't run again until the interval passes
time.Sleep(interval)
} }
cancel() cancel()
<-stopped <-stopped
@ -801,7 +808,5 @@ func TestToggleRegistry(t *testing.T) {
r.failCountMu.Lock() r.failCountMu.Lock()
finalCount := r.failCount finalCount := r.failCount
r.failCountMu.Unlock() r.failCountMu.Unlock()
if finalCount < 3 { assert.Equal(t, toggleRegistryFailureCount, finalCount, "failCount should be at least %d", toggleRegistryFailureCount)
t.Fatalf("failCount should be at least 3 after waiting up to 2s, got %d", finalCount)
}
} }