From 80ac13ce328f4af7917739d4464a44c824864721 Mon Sep 17 00:00:00 2001 From: Jian Zhu Date: Fri, 13 Feb 2026 10:18:37 +0800 Subject: [PATCH] fix: remove flaky time.Sleep from hub timeout controller test (#1388) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The TestHubTimeoutController_Sync test was failing intermittently in CI due to timing issues with time.Sleep() and real-time execution overhead. Changes: - Removed time.Sleep() dependency that caused flakiness - Set lease renew time in the past using time.Now().Add(-duration) to deterministically simulate aged leases - Made timeout threshold configurable per test case - Increased safety margin from 2s to 3s in "not timeout" case (2s lease age vs 5s timeout, previously 1s wait vs 3s timeout) - Set startTime in the past to bypass the 10s grace period check that was added to handle stale lease scenarios This eliminates race conditions in CI environments where execution overhead could push the test beyond the timeout threshold. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: zhujian Co-authored-by: Claude Sonnet 4.5 --- .../hub_timeout_controller_test.go | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/pkg/registration/spoke/registration/hub_timeout_controller_test.go b/pkg/registration/spoke/registration/hub_timeout_controller_test.go index e461239d4..e0cdcf065 100644 --- a/pkg/registration/spoke/registration/hub_timeout_controller_test.go +++ b/pkg/registration/spoke/registration/hub_timeout_controller_test.go @@ -13,31 +13,33 @@ import ( func TestHubTimeoutController_Sync(t *testing.T) { cases := []struct { - name string - waitSeconds int - expect bool + name string + leaseAgeSeconds int + timeoutSeconds int32 + expect bool }{ { - name: "not timeout", - waitSeconds: 1, - expect: false, + name: "not timeout", + leaseAgeSeconds: 2, + timeoutSeconds: 5, + expect: false, }, { - name: "timeout", - waitSeconds: 5, - expect: true, + name: "timeout", + leaseAgeSeconds: 6, + timeoutSeconds: 5, + expect: true, }, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { - var leaseRenewTime = time.Now() + // Set lease renew time in the past to simulate aging + leaseRenewTime := time.Now().Add(-time.Duration(c.leaseAgeSeconds) * time.Second) lease := testinghelpers.NewManagedClusterLease("managed-cluster-lease", leaseRenewTime) leaseClient := kubefake.NewClientset(lease) - time.Sleep(time.Second * time.Duration(c.waitSeconds)) - handled := false controller := &hubTimeoutController{ leaseClient: leaseClient.CoordinationV1().Leases(testinghelpers.TestManagedClusterName), @@ -46,7 +48,9 @@ func TestHubTimeoutController_Sync(t *testing.T) { return nil }, clusterName: testinghelpers.TestManagedClusterName, - timeoutSeconds: 3, + timeoutSeconds: c.timeoutSeconds, + // Set startTime in the past to bypass the 10s grace period + startTime: time.Now().Add(-time.Second * 15), } err := controller.sync(context.Background(), testingcommon.NewFakeSyncContext(t, ""), "")