View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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},  // append
102       {200, 400, 100},  // prepend
103       {200, 400, 300},  // insert
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       // test with head == 0
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       // test with head > 0
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 }