1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.procedure2.util;
20
21
22 import java.util.Arrays;
23 import java.util.concurrent.TimeUnit;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.hbase.procedure2.util.TimeoutBlockingQueue.TimeoutRetriever;
28 import org.apache.hadoop.hbase.testclassification.SmallTests;
29
30 import org.junit.Assert;
31 import org.junit.Test;
32 import org.junit.experimental.categories.Category;
33
34 import static org.junit.Assert.assertEquals;
35 import static org.junit.Assert.assertFalse;
36 import static org.junit.Assert.assertTrue;
37 import static org.junit.Assert.fail;
38
39 @Category(SmallTests.class)
40 public class TestTimeoutBlockingQueue {
41 private static final Log LOG = LogFactory.getLog(TestTimeoutBlockingQueue.class);
42
43 static class TestObject {
44 private long timeout;
45 private int seqId;
46
47 public TestObject(int seqId, long timeout) {
48 this.timeout = timeout;
49 this.seqId = seqId;
50 }
51
52 public long getTimeout() {
53 return timeout;
54 }
55
56 public String toString() {
57 return String.format("(%03d, %03d)", seqId, timeout);
58 }
59 }
60
61 static class TestObjectTimeoutRetriever implements TimeoutRetriever<TestObject> {
62 @Override
63 public long getTimeout(TestObject obj) {
64 return obj.getTimeout();
65 }
66
67 @Override
68 public TimeUnit getTimeUnit(TestObject obj) {
69 return TimeUnit.MILLISECONDS;
70 }
71 }
72
73 @Test
74 public void testOrder() {
75 TimeoutBlockingQueue<TestObject> queue =
76 new TimeoutBlockingQueue<TestObject>(8, new TestObjectTimeoutRetriever());
77
78 long[] timeouts = new long[] {500, 200, 700, 300, 600, 600, 200, 800, 500};
79
80 for (int i = 0; i < timeouts.length; ++i) {
81 for (int j = 0; j <= i; ++j) {
82 queue.add(new TestObject(j, timeouts[j]));
83 queue.dump();
84 }
85
86 long prev = 0;
87 for (int j = 0; j <= i; ++j) {
88 TestObject obj = queue.poll();
89 assertTrue(obj.getTimeout() >= prev);
90 prev = obj.getTimeout();
91 queue.dump();
92 }
93 }
94 }
95
96 @Test
97 public void testTimeoutBlockingQueue() {
98 TimeoutBlockingQueue<TestObject> queue;
99
100 int[][] testArray = new int[][] {
101 {200, 400, 600},
102 {200, 400, 100},
103 {200, 400, 300},
104 };
105
106 for (int i = 0; i < testArray.length; ++i) {
107 int[] sortedArray = Arrays.copyOf(testArray[i], testArray[i].length);
108 Arrays.sort(sortedArray);
109
110
111 queue = new TimeoutBlockingQueue<TestObject>(2, new TestObjectTimeoutRetriever());
112 for (int j = 0; j < testArray[i].length; ++j) {
113 queue.add(new TestObject(j, testArray[i][j]));
114 queue.dump();
115 }
116
117 for (int j = 0; !queue.isEmpty(); ++j) {
118 assertEquals(sortedArray[j], queue.poll().getTimeout());
119 }
120
121 queue = new TimeoutBlockingQueue<TestObject>(2, new TestObjectTimeoutRetriever());
122 queue.add(new TestObject(0, 50));
123 assertEquals(50, queue.poll().getTimeout());
124
125
126 for (int j = 0; j < testArray[i].length; ++j) {
127 queue.add(new TestObject(j, testArray[i][j]));
128 queue.dump();
129 }
130
131 for (int j = 0; !queue.isEmpty(); ++j) {
132 assertEquals(sortedArray[j], queue.poll().getTimeout());
133 }
134 }
135 }
136 }