ifesdjeen commented on code in PR #3408:
URL: https://github.com/apache/cassandra/pull/3408#discussion_r1676812352


##########
test/simulator/test/org/apache/cassandra/simulator/test/TrivialSimulationTest.java:
##########
@@ -85,9 +97,97 @@ public void componentTest()
     }
 
     @Test
-    public void identityHashMapTest()
+    public void semaphoreTest()
     {
-        simulate(arr(() -> new IdentityHashMap<>().put(1, 1)),
-                 () -> {});
+        long seed = System.currentTimeMillis();
+        semaphoreTestInternal(seed);
+        State.record = false;
+        // Verify that subsequent interleavings will be the same
+        semaphoreTestInternal(seed);
+    }
+
+    protected void semaphoreTestInternal(long seed)
+    {
+        simulate(arr(() -> {
+                     ExecutorPlus executor = 
ExecutorFactory.Global.executorFactory().pooled("semaphore-test-", 10);
+                     Semaphore semaphore = Semaphore.newSemaphore(5);
+                     CountDownLatch latch = 
CountDownLatch.newCountDownLatch(5);
+
+                     for (int i = 0; i < 5; i++)
+                     {
+                         int thread = i;
+                         executor.submit(() -> {
+                             for (int j = 0; j < 100; j++)
+                             {
+                                 int permits = semaphore.permits();
+                                 Assert.assertTrue(permits + " should be non 
negative", permits >= 0);
+
+                                 try
+                                 {
+                                     semaphore.acquire(1);
+                                     State.tick(thread, j);
+                                     semaphore.release(1);
+                                 }
+                                 catch (Throwable e)
+                                 {
+                                     throw new RuntimeException(e);
+                                 }
+                             }
+                             latch.decrement();
+                         });
+                     }
+
+                     latch.awaitUninterruptibly();
+                     int permits = semaphore.permits();
+                     Assert.assertEquals(5, permits);
+                 }),
+                 () -> {},
+                 seed);
+    }
+
+    @Shared
+    public static class State
+    {
+        static final List<Tick> ticks = new ArrayList<>();
+        static boolean record = true;
+        static int i = 0;
+
+        public static void tick(int thread, int iteration)

Review Comment:
   It does not have to be thread safe. It is executed effectively in a single 
thread; this is also why we have same interleavings no matter how many times 
you run it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to