From 99109604f02e65f4d5bf66fa71d5e82683f9fc0e Mon Sep 17 00:00:00 2001 From: Nikita Date: Mon, 12 Dec 2016 12:55:25 +0300 Subject: [PATCH] Test fixed --- .../RedissonBlockingFairQueueTest.java | 63 +++++++++++++------ 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/redisson/src/test/java/org/redisson/RedissonBlockingFairQueueTest.java b/redisson/src/test/java/org/redisson/RedissonBlockingFairQueueTest.java index 6e4c3a04d..4caa89567 100644 --- a/redisson/src/test/java/org/redisson/RedissonBlockingFairQueueTest.java +++ b/redisson/src/test/java/org/redisson/RedissonBlockingFairQueueTest.java @@ -6,45 +6,70 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import org.junit.Assert; import org.junit.Test; import org.redisson.api.RBlockingFairQueue; +import org.redisson.api.RBlockingQueue; public class RedissonBlockingFairQueueTest extends BaseTest { @Test - public void testPollTimeout() throws InterruptedException { - int size = 100; - RBlockingFairQueue queue = redisson.getBlockingFairQueue("test"); + public void testFairness() throws InterruptedException { + int size = 10000; + RBlockingQueue queue = redisson.getBlockingQueue("test"); CountDownLatch latch = new CountDownLatch(size); - List threads = new ArrayList(); - for (int i = 0; i < size; i++) { - final int j = i; - Thread t = new Thread() { - public void run() { + AtomicInteger t1Counter = new AtomicInteger(); + AtomicInteger t2Counter = new AtomicInteger(); + Thread t1 = new Thread("test-thread1") { + public void run() { + while (true) { try { - String value = queue.poll(1, TimeUnit.SECONDS); - assertThat(value).isEqualTo("" + j); + String a = queue.poll(1, TimeUnit.SECONDS); + if (a == null) { + break; + } latch.countDown(); + t1Counter.incrementAndGet(); } catch (InterruptedException e) { } - }; + } }; - - threads.add(t); - } - - for (Thread thread : threads) { - thread.start(); - thread.join(5); - } + }; + + Thread t2 = new Thread("test-thread1") { + public void run() { + while (true) { + try { + String a = queue.poll(1, TimeUnit.SECONDS); + if (a == null) { + break; + } + Thread.sleep(5); + latch.countDown(); + t2Counter.incrementAndGet(); + } catch (InterruptedException e) { + } + } + }; + }; for (int i = 0; i < size; i++) { queue.add("" + i); } + + t1.start(); + t2.start(); + t2.join(); + t1.join(); + assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue(); + System.out.println("t1: " + t1Counter.get()); + System.out.println("t2: " + t2Counter.get()); } } +