Skip to content

Agent Mesh

node: avoid wall-clock sleep and 50ms race in TestRecoverEnrollmentTrustsOnlyKnownKeys

Antonio Ojeaeee4de9dad9b
diff --git a/internal/node/controlplane_sync.go b/internal/node/controlplane_sync.go
index 69be4182..eea3dd64 100644
--- a/internal/node/controlplane_sync.go
+++ b/internal/node/controlplane_sync.go
@@ -385,9 +385,8 @@ func (n *AgentMeshNode) controlPlaneForgotUs(err error) bool {
// reenrollCooldown bounds how often a node spends its bootstrap token on a
// re-enrollment: a 401 is a claim by whoever answered the control plane
-// URL, and one attempt per cooldown is all it can cause. A package variable
-// so a test can shorten it.
-var reenrollCooldown = time.Minute
+// URL, and one attempt per cooldown is all it can cause.
+const reenrollCooldown = time.Minute
// recoverEnrollment enrolls again with the bootstrap token at
// Options.BootstrapTokenPath and reports whether it did. The answer must
diff --git a/internal/node/controlplane_sync_test.go b/internal/node/controlplane_sync_test.go
index e1d4e7f3..d285af38 100644
--- a/internal/node/controlplane_sync_test.go
+++ b/internal/node/controlplane_sync_test.go
@@ -120,10 +120,6 @@ func TestSyncRecoversWhenControlPlaneForgotNode(t *testing.T) {
// of its own making must not be able to replace it, and one attempt per
// cooldown is all a 401 can cause.
func TestRecoverEnrollmentTrustsOnlyKnownKeys(t *testing.T) {
- prev := reenrollCooldown
- reenrollCooldown = 50 * time.Millisecond
- t.Cleanup(func() { reenrollCooldown = prev })
-
newHarness := func(t *testing.T, answer func(h *refreshHarness) *api.BootstrapEnrollResponse) (*refreshHarness, *atomic.Int32) {
t.Helper()
var enrolls atomic.Int32
@@ -216,7 +212,9 @@ func TestRecoverEnrollmentTrustsOnlyKnownKeys(t *testing.T) {
if enrolls.Load() != 1 {
t.Fatalf("enroll attempts within the cooldown = %d, want 1", enrolls.Load())
}
- time.Sleep(2 * reenrollCooldown)
+ harness.node.reenrollMu.Lock()
+ harness.node.lastReenroll = time.Now().Add(-reenrollCooldown)
+ harness.node.reenrollMu.Unlock()
_ = harness.node.SyncControlPlane(context.Background())
if enrolls.Load() != 2 {
t.Fatalf("enroll attempts after the cooldown = %d, want 2", enrolls.Load())