dimitarndimitrov commented on PR #17184:
URL: https://github.com/apache/kafka/pull/17184#issuecomment-2347304340

   > because on my development machine I wasn't able to see consistent 
reproducibility without more sophisticated testing tools like jcstress or 
Byteman/BMUnit.
   
   After a bunch of experiments, I did arrive at this test, which on my 
development machine fails >50% without this change and passes 100% (in more 
than 500 attempts) with this change:
   ```
   @Test
   public void testLatestHistogramRace() throws InterruptedException {
       long maxSnapshotAgeMs = 10L;
       long now = System.currentTimeMillis();
       HdrHistogram hdrHistogram = new HdrHistogram(maxSnapshotAgeMs, 
MAX_VALUE, 3);
       for (int i = 1; i < 10000; i++) {
           int numEvents = 10;
           for (int j = 0; j < numEvents; j++) {
               hdrHistogram.record(i);
           }
           AtomicLong t1Counter = new AtomicLong();
           AtomicLong t2Counter = new AtomicLong();
           final long moreThanMaxAge = now + maxSnapshotAgeMs + 1;
           now = moreThanMaxAge;
           Thread t1 = new Thread(() -> 
t1Counter.set(hdrHistogram.count(moreThanMaxAge)));
           Thread t2 = new Thread(() -> 
t2Counter.set(hdrHistogram.count(moreThanMaxAge)));
           t1.start();
           t2.start();
           t1.join();
           t2.join();
           long t1Count = t1Counter.get();
           long t2Count = t2Counter.get();
           assertTrue(
               numEvents == t1Count && numEvents == t2Count,
               String.format("Expected %d events in both threads, got %d in T1 
and %d in T2",
                   numEvents, t1Count, t2Count));
       }
   }
   ```
   
   It does rely on numbers and it takes >2 sec locally when it succeeds, so I 
am inclined not to include it. Let me know if you prefer otherwise. I did spend 
some time trying to make the iteration body smaller by using a `Phaser` and 
reusing the threads, but that made the test less readable and didn't help with 
reproducibility, hence why I'm posting this version.


-- 
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: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to