cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ber...@apache.org
Subject [cassandra] branch cassandra-4.0 updated: Replace spinAsserts code with Awaitility code
Date Tue, 31 Aug 2021 05:27:47 GMT
This is an automated email from the ASF dual-hosted git repository.

bereng pushed a commit to branch cassandra-4.0
in repository https://gitbox.apache.org/repos/asf/cassandra.git


The following commit(s) were added to refs/heads/cassandra-4.0 by this push:
     new 78c6279  Replace spinAsserts code with Awaitility code
78c6279 is described below

commit 78c6279658d5c18a8704a8e1ab1bc5200b8b1e0b
Author: Bereng <berenguerblasi@gmail.com>
AuthorDate: Wed Aug 18 10:49:15 2021 +0200

    Replace spinAsserts code with Awaitility code
    
    patch by Jogesh Anand, Andres de la Peña, Berenguer Blasi; reviewed by Andres de la Peña,
Berenguer Blasi for CASSANDRA-16621
    
    Co-authored-by: Jogesh Anand <jogeshanand@gmail.com>
    Co-authored-by: Andres de la Peña <a.penya.garcia@gmail.com>
    Co-authored-by: Berenguer Blasi <berenguerblasi@gmail.com>
---
 test/unit/org/apache/cassandra/Util.java             | 20 +++++++++-----------
 .../cassandra/metrics/ThreadPoolMetricsTest.java     |  4 ++--
 2 files changed, 11 insertions(+), 13 deletions(-)

diff --git a/test/unit/org/apache/cassandra/Util.java b/test/unit/org/apache/cassandra/Util.java
index e0d29b6..4b7b6ea 100644
--- a/test/unit/org/apache/cassandra/Util.java
+++ b/test/unit/org/apache/cassandra/Util.java
@@ -25,6 +25,7 @@ import java.io.File;
 import java.io.IOError;
 import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
+import java.time.Duration;
 import java.util.*;
 import java.util.concurrent.Callable;
 import java.util.concurrent.Future;
@@ -82,11 +83,14 @@ import org.apache.cassandra.utils.CassandraVersion;
 import org.apache.cassandra.utils.CounterId;
 import org.apache.cassandra.utils.FBUtilities;
 import org.apache.cassandra.utils.FilterFactory;
+import org.awaitility.Awaitility;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
 
 public class Util
 {
@@ -621,17 +625,11 @@ public class Util
 
     public static <T> void spinAssertEquals(String message, T expected, Supplier<?
extends T> actualSupplier, long timeout, TimeUnit timeUnit)
     {
-        long startNano = System.nanoTime();
-        long expireAtNano = startNano + timeUnit.toNanos(timeout);
-        T actual = null;
-        while (System.nanoTime() < expireAtNano)
-        {
-            actual = actualSupplier.get();
-            if (actual.equals(expected))
-                break;
-            Thread.yield();
-        }
-        assertEquals(message, expected, actual);
+        Awaitility.await()
+                  .pollInterval(Duration.ofMillis(100))
+                  .pollDelay(0, TimeUnit.MILLISECONDS)
+                  .atMost(timeout, timeUnit)
+                  .untilAsserted(() -> assertThat(message, actualSupplier.get(), equalTo(expected)));
     }
 
     public static void joinThread(Thread thread) throws InterruptedException
diff --git a/test/unit/org/apache/cassandra/metrics/ThreadPoolMetricsTest.java b/test/unit/org/apache/cassandra/metrics/ThreadPoolMetricsTest.java
index 03c1acf..7302655 100644
--- a/test/unit/org/apache/cassandra/metrics/ThreadPoolMetricsTest.java
+++ b/test/unit/org/apache/cassandra/metrics/ThreadPoolMetricsTest.java
@@ -159,7 +159,7 @@ public class ThreadPoolMetricsTest
         // Allowing third task to complete
         task3.allowToComplete();
 
-        spinAssertEquals(true, task5::isStarted);
+        spinAssertEquals(true, () -> task5.isStarted() || task6.isStarted());
         spinAssertEquals(2, metrics.activeTasks::getValue);
         spinAssertEquals(3L, metrics.completedTasks::getValue);
         spinAssertEquals(1, metrics.pendingTasks::getValue);
@@ -169,7 +169,7 @@ public class ThreadPoolMetricsTest
         // Allowing fourth task to complete
         task4.allowToComplete();
 
-        spinAssertEquals(true, task6::isStarted);
+        spinAssertEquals(true, () -> task5.isStarted() && task6.isStarted());
         spinAssertEquals(2, metrics.activeTasks::getValue);
         spinAssertEquals(4L, metrics.completedTasks::getValue);
         spinAssertEquals(0, metrics.pendingTasks::getValue);

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For additional commands, e-mail: commits-help@cassandra.apache.org


Mime
View raw message