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;
20  
21  import org.apache.hadoop.hbase.testclassification.SmallTests;
22  
23  import org.junit.Assert;
24  import org.junit.Test;
25  import org.junit.experimental.categories.Category;
26  
27  import static org.junit.Assert.assertEquals;
28  
29  @Category(SmallTests.class)
30  public class TestProcedureFairRunQueues {
31    private static class TestRunQueue implements ProcedureFairRunQueues.FairObject {
32      private final int priority;
33      private final String name;
34  
35      private boolean available = true;
36  
37      public TestRunQueue(String name, int priority) {
38        this.name = name;
39        this.priority = priority;
40      }
41  
42      @Override
43      public String toString() {
44        return name;
45      }
46  
47      private void setAvailable(boolean available) {
48        this.available = available;
49      }
50  
51      @Override
52      public boolean isAvailable() {
53        return available;
54      }
55  
56      @Override
57      public int getPriority() {
58        return priority;
59      }
60    }
61  
62    @Test
63    public void testEmptyFairQueues() throws Exception {
64      ProcedureFairRunQueues<String, TestRunQueue> fairq
65        = new ProcedureFairRunQueues<String, TestRunQueue>(1);
66      for (int i = 0; i < 3; ++i) {
67        assertEquals(null, fairq.poll());
68      }
69    }
70  
71    @Test
72    public void testFairQueues() throws Exception {
73      ProcedureFairRunQueues<String, TestRunQueue> fairq
74        = new ProcedureFairRunQueues<String, TestRunQueue>(1);
75      TestRunQueue a = fairq.add("A", new TestRunQueue("A", 1));
76      TestRunQueue b = fairq.add("B", new TestRunQueue("B", 1));
77      TestRunQueue m = fairq.add("M", new TestRunQueue("M", 2));
78  
79      for (int i = 0; i < 3; ++i) {
80        assertEquals(a, fairq.poll());
81        assertEquals(b, fairq.poll());
82        assertEquals(m, fairq.poll());
83        assertEquals(m, fairq.poll());
84      }
85    }
86  
87    @Test
88    public void testFairQueuesNotAvailable() throws Exception {
89      ProcedureFairRunQueues<String, TestRunQueue> fairq
90        = new ProcedureFairRunQueues<String, TestRunQueue>(1);
91      TestRunQueue a = fairq.add("A", new TestRunQueue("A", 1));
92      TestRunQueue b = fairq.add("B", new TestRunQueue("B", 1));
93      TestRunQueue m = fairq.add("M", new TestRunQueue("M", 2));
94  
95      // m is not available
96      m.setAvailable(false);
97      for (int i = 0; i < 3; ++i) {
98        assertEquals(a, fairq.poll());
99        assertEquals(b, fairq.poll());
100     }
101 
102     // m is available
103     m.setAvailable(true);
104     for (int i = 0; i < 3; ++i) {
105       assertEquals(m, fairq.poll());
106       assertEquals(m, fairq.poll());
107       assertEquals(a, fairq.poll());
108       assertEquals(b, fairq.poll());
109     }
110 
111     // b is not available
112     b.setAvailable(false);
113     for (int i = 0; i < 3; ++i) {
114       assertEquals(m, fairq.poll());
115       assertEquals(m, fairq.poll());
116       assertEquals(a, fairq.poll());
117     }
118 
119     assertEquals(m, fairq.poll());
120     m.setAvailable(false);
121     // m should be fetched next, but is no longer available
122     assertEquals(a, fairq.poll());
123     assertEquals(a, fairq.poll());
124     b.setAvailable(true);
125     for (int i = 0; i < 3; ++i) {
126       assertEquals(b, fairq.poll());
127       assertEquals(a, fairq.poll());
128     }
129   }
130 
131   @Test
132   public void testFairQueuesDelete() throws Exception {
133     ProcedureFairRunQueues<String, TestRunQueue> fairq
134       = new ProcedureFairRunQueues<String, TestRunQueue>(1);
135     TestRunQueue a = fairq.add("A", new TestRunQueue("A", 1));
136     TestRunQueue b = fairq.add("B", new TestRunQueue("B", 1));
137     TestRunQueue m = fairq.add("M", new TestRunQueue("M", 2));
138 
139     // Fetch A and then remove it
140     assertEquals(a, fairq.poll());
141     assertEquals(a, fairq.remove("A"));
142 
143     // Fetch B and then remove it
144     assertEquals(b, fairq.poll());
145     assertEquals(b, fairq.remove("B"));
146 
147     // Fetch M and then remove it
148     assertEquals(m, fairq.poll());
149     assertEquals(m, fairq.remove("M"));
150 
151     // nothing left
152     assertEquals(null, fairq.poll());
153   }
154 }