summaryrefslogtreecommitdiffstats
path: root/simple/simple-common/src/test/java/org/simpleframework/common/thread/SchedulerTest.java
blob: 78fe8026378f34b725728744de41032724f3c1e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package org.simpleframework.common.thread;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import org.simpleframework.common.thread.ConcurrentScheduler;

import junit.framework.TestCase;

public class SchedulerTest extends TestCase {
   
   private static final int ITERATIONS = 10000;
   
   public void testScheduler() throws Exception {
      ConcurrentScheduler queue = new ConcurrentScheduler(Runnable.class, 10);
      LinkedBlockingQueue<Timer> list = new LinkedBlockingQueue<Timer>();
      
      for(int i = 0; i < ITERATIONS; i++) {
         queue.execute(new Task(list, new Timer(i)), i, TimeUnit.MILLISECONDS);
      }      
      for(Timer timer = list.take(); timer.getValue() < ITERATIONS - 10; timer = list.take()) {
         System.err.println("value=["+timer.getValue()+"] delay=["+timer.getDelay()+"] expect=["+timer.getExpectation()+"]");
      }      
   }
   
   public class Timer {
      
      private Integer value;
      
      private long time;
      
      public Timer(Integer value) {
         this.time = System.currentTimeMillis();
         this.value = value;
      }
      
      public Integer getValue() {
         return value;
      }
      
      public long getDelay() {
         return System.currentTimeMillis() - time;
      }
      
      public int getExpectation() {
         return value.intValue();
      }
   }
   
   public class Task implements Runnable {
      
      private LinkedBlockingQueue<Timer> queue;
      
      private Timer timer;
      
      public Task(LinkedBlockingQueue<Timer> queue, Timer timer) {
         this.queue = queue;
         this.timer = timer;
      }
      
      public void run() {
         queue.offer(timer);
      }     
   }
}