This is an automated email from the ASF dual-hosted git repository.

hepin pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pekko.git


The following commit(s) were added to refs/heads/main by this push:
     new cd6987c507 chore: fix flasky test in MetricsBasedResizerSpec (#2769)
cd6987c507 is described below

commit cd6987c507fa9fd6c69a4f71d1499d99631b24c9
Author: He-Pin(kerr) <[email protected]>
AuthorDate: Sun Mar 22 18:58:51 2026 +0800

    chore: fix flasky test in MetricsBasedResizerSpec (#2769)
    
    Fix Summary
    **Problem:** The test case "must update the underutilizationStreak 
highestUtilization if current utilization is higher" within 
`MetricsBasedResizerSpec` was failing intermittently on CI; the expected 
`highestUtilization` value was 2, but the actual value was 1.
    
    **Root Cause:** A race condition. The test sends one message each to 
`routee 0` and `routee 1`, followed by an `await` call; however, the `await` 
only guarantees that the actors have *begun* processing the messages 
(specifically, that `first.countDown()` has been invoked). When 
`reportMessageCount` checks `cell.currentMessage` and 
`mailbox.numberOfMessages`, a very brief time window may exist—particularly 
under the high load conditions of the CI environment—during which a specific 
rou [...]
    
    **Fix:** After the initial `mockSend(await = true)` calls, an additional 
`mockSend(await = false)` message is now sent to both `routee 0` and `routee 
1`. This ensures that, even if the `currentMessage` state is momentarily 
indeterminate, there remain pending messages within the mailbox (i.e., 
`numberOfMessages > 0`), thereby guaranteeing the correct calculation of the 
`utilized` metric. This approach aligns with the pattern utilized by other 
tests within the same test file (e.g., the  [...]
---
 .../scala/org/apache/pekko/routing/MetricsBasedResizerSpec.scala     | 5 +++++
 1 file changed, 5 insertions(+)

diff --git 
a/actor-tests/src/test/scala/org/apache/pekko/routing/MetricsBasedResizerSpec.scala
 
b/actor-tests/src/test/scala/org/apache/pekko/routing/MetricsBasedResizerSpec.scala
index 0c930e491e..3642c07a3d 100644
--- 
a/actor-tests/src/test/scala/org/apache/pekko/routing/MetricsBasedResizerSpec.scala
+++ 
b/actor-tests/src/test/scala/org/apache/pekko/routing/MetricsBasedResizerSpec.scala
@@ -187,8 +187,13 @@ class MetricsBasedResizerSpec extends 
PekkoSpec(ResizerSpec.config) with Default
         underutilizationStreak = Some(UnderUtilizationStreak(start = 
LocalDateTime.now, highestUtilization = 1)))
 
       val router = TestRouter(routees(3))
+      // Send messages to routee 0 and 1, await ensures the actor started 
processing
       router.mockSend(await = true, routeeIdx = 0)
       router.mockSend(await = true, routeeIdx = 1)
+      // Send additional messages to ensure mailbox is non-empty when checked,
+      // avoiding a race condition where currentMessage could be null 
momentarily
+      router.mockSend(await = false, routeeIdx = 0)
+      router.mockSend(await = false, routeeIdx = 1)
 
       resizer.reportMessageCount(router.routees, router.msgs.size)
       resizer.record.underutilizationStreak.get.highestUtilization shouldBe 2


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to