作者 crossoverJie

:white_check_mark: Adding tests.

@@ -41,10 +41,15 @@ public final class RingBufferWheel { @@ -41,10 +41,15 @@ public final class RingBufferWheel {
41 private AtomicInteger taskSize = new AtomicInteger(); 41 private AtomicInteger taskSize = new AtomicInteger();
42 42
43 /*** 43 /***
44 - * task running sign 44 + * task stop sign
45 */ 45 */
46 private volatile boolean stop = false; 46 private volatile boolean stop = false;
47 47
  48 + /**
  49 + * task start sign
  50 + */
  51 + private volatile boolean start = false ;
  52 +
48 private Lock lock = new ReentrantLock(); 53 private Lock lock = new ReentrantLock();
49 private Condition condition = lock.newCondition(); 54 private Condition condition = lock.newCondition();
50 55
@@ -98,6 +103,7 @@ public final class RingBufferWheel { @@ -98,6 +103,7 @@ public final class RingBufferWheel {
98 103
99 taskSize.incrementAndGet(); 104 taskSize.incrementAndGet();
100 105
  106 + start();
101 } 107 }
102 108
103 /** 109 /**
@@ -112,10 +118,13 @@ public final class RingBufferWheel { @@ -112,10 +118,13 @@ public final class RingBufferWheel {
112 * Start background thread to consumer wheel timer, it will run until you call method {@link #stop} 118 * Start background thread to consumer wheel timer, it will run until you call method {@link #stop}
113 */ 119 */
114 public void start() { 120 public void start() {
  121 + if (!start){
115 logger.info("delay task is starting"); 122 logger.info("delay task is starting");
116 Thread job = new Thread(new TriggerJob()); 123 Thread job = new Thread(new TriggerJob());
117 job.setName("consumer RingBuffer thread"); 124 job.setName("consumer RingBuffer thread");
118 job.start(); 125 job.start();
  126 + start = true ;
  127 + }
119 } 128 }
120 129
121 /** 130 /**
1 package com.crossoverjie.cim.common.data.construct; 1 package com.crossoverjie.cim.common.data.construct;
2 2
3 -import org.junit.Test;  
4 import org.slf4j.Logger; 3 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory; 4 import org.slf4j.LoggerFactory;
6 5
@@ -13,7 +12,7 @@ public class RingBufferWheelTest { @@ -13,7 +12,7 @@ public class RingBufferWheelTest {
13 private static Logger logger = LoggerFactory.getLogger(RingBufferWheelTest.class) ; 12 private static Logger logger = LoggerFactory.getLogger(RingBufferWheelTest.class) ;
14 13
15 public static void main(String[] args) throws InterruptedException { 14 public static void main(String[] args) throws InterruptedException {
16 - test5(); 15 + test1();
17 16
18 return; 17 return;
19 } 18 }
@@ -30,8 +29,6 @@ public class RingBufferWheelTest { @@ -30,8 +29,6 @@ public class RingBufferWheelTest {
30 task.setKey(74); 29 task.setKey(74);
31 wheel.addTask(task) ; 30 wheel.addTask(task) ;
32 31
33 - wheel.start();  
34 -  
35 while (true){ 32 while (true){
36 logger.info("task size={}" , wheel.taskSize()); 33 logger.info("task size={}" , wheel.taskSize());
37 TimeUnit.SECONDS.sleep(1); 34 TimeUnit.SECONDS.sleep(1);
@@ -117,8 +114,6 @@ public class RingBufferWheelTest { @@ -117,8 +114,6 @@ public class RingBufferWheelTest {
117 wheel.addTask(task); 114 wheel.addTask(task);
118 } 115 }
119 116
120 - wheel.start();  
121 -  
122 logger.info("task size={}",wheel.taskSize()); 117 logger.info("task size={}",wheel.taskSize());
123 118
124 wheel.stop(false); 119 wheel.stop(false);
@@ -134,6 +129,32 @@ public class RingBufferWheelTest { @@ -134,6 +129,32 @@ public class RingBufferWheelTest {
134 logger.info("================"); 129 logger.info("================");
135 } 130 }
136 } 131 }
  132 + private static void cuncrrentTest6() throws InterruptedException {
  133 + ExecutorService executorService = Executors.newFixedThreadPool(10) ;
  134 +
  135 + RingBufferWheel wheel = new RingBufferWheel(executorService) ;
  136 +
  137 + for (int i = 0; i < 10; i++) {
  138 +
  139 + RingBufferWheel.Task task = new Job(i) ;
  140 + task.setKey(i);
  141 + wheel.addTask(task);
  142 + }
  143 +
  144 + wheel.start();
  145 +
  146 + TimeUnit.SECONDS.sleep(10);
  147 + RingBufferWheel.Task task = new Job(15) ;
  148 + task.setKey(15);
  149 + wheel.addTask(task);
  150 + wheel.start();
  151 +
  152 + logger.info("task size={}",wheel.taskSize());
  153 +
  154 + wheel.stop(false);
  155 +
  156 +
  157 + }
137 158
138 private static class Job extends RingBufferWheel.Task{ 159 private static class Job extends RingBufferWheel.Task{
139 160