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 @Override
57 public String toString() {
58 return String.format("(%03d, %03d)", seqId, timeout);
59 }
60 }
61
62 static class TestObjectTimeoutRetriever implements TimeoutRetriever<TestObject> {
63 @Override
64 public long getTimeout(TestObject obj) {
65 return obj.getTimeout();
66 }
67
68 @Override
69 public TimeUnit getTimeUnit(TestObject obj) {
70 return TimeUnit.MILLISECONDS;
71 }
72 }
73
74 @Test
75 public void testOrder() {
76 TimeoutBlockingQueue<TestObject> queue =
77 new TimeoutBlockingQueue<TestObject>(8, new TestObjectTimeoutRetriever());
78
79 long[] timeouts = new long[] {500, 200, 700, 300, 600, 600, 200, 800, 500};
80
81 for (int i = 0; i < timeouts.length; ++i) {
82 for (int j = 0; j <= i; ++j) {
83 queue.add(new TestObject(j, timeouts[j]));
84 queue.dump();
85 }
86
87 long prev = 0;
88 for (int j = 0; j <= i; ++j) {
89 TestObject obj = queue.poll();
90 assertTrue(obj.getTimeout() >= prev);
91 prev = obj.getTimeout();
92 queue.dump();
93 }
94 }
95 }
96
97 @Test
98 public void testTimeoutBlockingQueue() {
99 TimeoutBlockingQueue<TestObject> queue;
100
101 int[][] testArray = new int[][] {
102 {200, 400, 600},
103 {200, 400, 100},
104 {200, 400, 300},
105 };
106
107 for (int i = 0; i < testArray.length; ++i) {
108 int[] sortedArray = Arrays.copyOf(testArray[i], testArray[i].length);
109 Arrays.sort(sortedArray);
110
111
112 queue = new TimeoutBlockingQueue<TestObject>(2, new TestObjectTimeoutRetriever());
113 for (int j = 0; j < testArray[i].length; ++j) {
114 queue.add(new TestObject(j, testArray[i][j]));
115 queue.dump();
116 }
117
118 for (int j = 0; !queue.isEmpty(); ++j) {
119 assertEquals(sortedArray[j], queue.poll().getTimeout());
120 }
121
122 queue = new TimeoutBlockingQueue<TestObject>(2, new TestObjectTimeoutRetriever());
123 queue.add(new TestObject(0, 50));
124 assertEquals(50, queue.poll().getTimeout());
125
126
127 for (int j = 0; j < testArray[i].length; ++j) {
128 queue.add(new TestObject(j, testArray[i][j]));
129 queue.dump();
130 }
131
132 for (int j = 0; !queue.isEmpty(); ++j) {
133 assertEquals(sortedArray[j], queue.poll().getTimeout());
134 }
135 }
136 }
137
138 @Test
139 public void testRemove() {
140 TimeoutBlockingQueue<TestObject> queue =
141 new TimeoutBlockingQueue<TestObject>(2, new TestObjectTimeoutRetriever());
142
143 final int effectiveLen = 5;
144 TestObject[] objs = new TestObject[6];
145 for (int i = 0; i < effectiveLen; ++i) {
146 objs[i] = new TestObject(0, i * 10);
147 queue.add(objs[i]);
148 }
149 objs[effectiveLen] = new TestObject(0, effectiveLen * 10);
150 queue.dump();
151
152 for (int i = 0; i < effectiveLen; i += 2) {
153 assertTrue(queue.remove(objs[i]));
154 }
155 assertTrue(!queue.remove(objs[effectiveLen]));
156
157 for (int i = 0; i < effectiveLen; ++i) {
158 TestObject x = queue.poll();
159 assertEquals((i % 2) == 0 ? null : objs[i], x);
160 }
161 }
162 }