From 76dd91fe932fe25423649cd047ffc5737d8403c9 Mon Sep 17 00:00:00 2001 From: Edoardo Spadolini Date: Mon, 2 Dec 2024 15:03:16 +0100 Subject: [PATCH] Fix variable duration race in lib/inventory tests --- lib/inventory/controller.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/inventory/controller.go b/lib/inventory/controller.go index 8ee5c4cc40e0d..86bd6450fb0c1 100644 --- a/lib/inventory/controller.go +++ b/lib/inventory/controller.go @@ -284,13 +284,11 @@ func (c *Controller) RegisterControlStream(stream client.UpstreamInventoryContro c.store.Insert(handle) // Increment the concurrent connection counter that we use to calculate the - // variable instance heartbeat duration. It's done here synchronously rather - // than in handleControlStream for the sake of tests. + // variable instance heartbeat duration. To make the behavior more easily + // testable, we increment it here and we decrement it before closing the + // stream in handleControlStream. c.instanceHBVariableDuration.Inc() - go func() { - defer c.instanceHBVariableDuration.Dec() - c.handleControlStream(handle) - }() + go c.handleControlStream(handle) } // GetControlStream gets a control stream for the given server ID if one exists (if multiple control streams @@ -400,6 +398,7 @@ func (c *Controller) handleControlStream(handle *upstreamHandle) { } } + c.instanceHBVariableDuration.Dec() for _, service := range handle.hello.Services { c.serviceCounter.decrement(service) }