This is an automated email from the ASF dual-hosted git repository.
clebertsuconic pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/activemq-artemis.git
The following commit(s) were added to refs/heads/main by this push:
new e90a659 ARTEMIS-3587 Fixing false positives on critical analyzer
e90a659 is described below
commit e90a6599815b073a655f2343689c62533d6404ac
Author: Clebert Suconic <[email protected]>
AuthorDate: Mon Nov 29 10:05:19 2021 -0500
ARTEMIS-3587 Fixing false positives on critical analyzer
---
.../artemis/utils/critical/CriticalMeasure.java | 2 +-
.../utils/critical/CriticalMeasureTest.java | 35 ++++++++++++++++++++--
2 files changed, 34 insertions(+), 3 deletions(-)
diff --git
a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/critical/CriticalMeasure.java
b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/critical/CriticalMeasure.java
index e0df253..16fa099 100644
---
a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/critical/CriticalMeasure.java
+++
b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/critical/CriticalMeasure.java
@@ -129,7 +129,7 @@ public class CriticalMeasure {
final long thisTimeEnter = this.timeEnter;
if (thisTimeEnter != 0L) {
long time = System.nanoTime();
- boolean expired = time - timeEnter > timeout;
+ boolean expired = time - thisTimeEnter > timeout;
if (expired) {
Exception lastTraceEnter = this.traceEnter;
diff --git
a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/critical/CriticalMeasureTest.java
b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/critical/CriticalMeasureTest.java
index 466e91e..0d89fb8 100644
---
a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/critical/CriticalMeasureTest.java
+++
b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/critical/CriticalMeasureTest.java
@@ -18,6 +18,8 @@
package org.apache.activemq.artemis.utils.critical;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;
import org.junit.Assert;
@@ -65,12 +67,41 @@ public class CriticalMeasureTest {
public void testWithCloseable() throws Exception {
CriticalAnalyzer analyzer = new CriticalAnalyzerImpl();
CriticalComponent component = new CriticalComponentImpl(analyzer, 5);
- CriticalMeasure measure = new CriticalMeasure(component, 1);
- long time = System.nanoTime();
try (AutoCloseable theMeasure = component.measureCritical(0)) {
LockSupport.parkNanos(1000);
Assert.assertTrue(component.checkExpiration(100, false));
}
Assert.assertFalse(component.checkExpiration(100, false));
}
+
+ @Test
+ public void testRace() throws Exception {
+ CriticalAnalyzer analyzer = new CriticalAnalyzerImpl();
+ CriticalComponent component = new CriticalComponentImpl(analyzer, 5);
+
+ AtomicInteger errors = new AtomicInteger(0);
+ AtomicBoolean running = new AtomicBoolean(true);
+ Thread t = new Thread(() -> {
+ long oneSecond = TimeUnit.SECONDS.toNanos(1);
+ while (running.get()) {
+ if (component.checkExpiration(oneSecond, false)) {
+ errors.incrementAndGet();
+ }
+ }
+ });
+ t.start();
+ try {
+ long timeRunning = System.currentTimeMillis() + 100;
+ while (timeRunning > System.currentTimeMillis()) {
+ try (AutoCloseable theMeasure = component.measureCritical(0)) {
+ LockSupport.parkNanos(1);
+ }
+ }
+ } finally {
+ running.set(false);
+ }
+ t.join(1000);
+ Assert.assertFalse(t.isAlive());
+ Assert.assertEquals(0, errors.get());
+ }
}