1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.io.IOException;
22  import java.util.concurrent.atomic.AtomicBoolean;
23  
24  import org.apache.hadoop.hbase.MediumTests;
25  import org.apache.hadoop.hbase.RegionTooBusyException;
26  import org.apache.hadoop.hbase.client.Get;
27  import org.apache.hadoop.hbase.util.Bytes;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  /**
32   * TestHRegion with hbase.busy.wait.duration set to 1000 (1 second).
33   * We can't use parameterized test since TestHRegion is old fashion.
34   */
35  @Category(MediumTests.class)
36  @SuppressWarnings("deprecation")
37  public class TestHRegionBusyWait extends TestHRegion {
38    public TestHRegionBusyWait() {
39      conf.set("hbase.busy.wait.duration", "1000");
40    }
41  
42    /**
43     * Test RegionTooBusyException thrown when region is busy
44     */
45    @Test (timeout=2000)
46    public void testRegionTooBusy() throws IOException {
47      String method = "testRegionTooBusy";
48      byte[] tableName = Bytes.toBytes(method);
49      byte[] family = Bytes.toBytes("family");
50      region = initHRegion(tableName, method, conf, family);
51      final AtomicBoolean stopped = new AtomicBoolean(true);
52      Thread t = new Thread(new Runnable() {
53        @Override
54        public void run() {
55          try {
56            region.lock.writeLock().lock();
57            stopped.set(false);
58            while (!stopped.get()) {
59              Thread.sleep(100);
60            }
61          } catch (InterruptedException ie) {
62          } finally {
63            region.lock.writeLock().unlock();
64          }
65        }
66      });
67      t.start();
68      Get get = new Get(row);
69      try {
70        while (stopped.get()) {
71          Thread.sleep(100);
72        }
73        region.get(get, null);
74        fail("Should throw RegionTooBusyException");
75      } catch (InterruptedException ie) {
76        fail("test interrupted");
77      } catch (RegionTooBusyException e) {
78        // Good, expected
79      } finally {
80        stopped.set(true);
81        try {
82          t.join();
83        } catch (Throwable e) {
84        }
85  
86        HRegion.closeHRegion(region);
87        region = null;
88      }
89    }
90  }