001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.util; 019 020import static org.junit.Assert.assertTrue; 021 022import java.util.concurrent.atomic.AtomicBoolean; 023import org.apache.hadoop.hbase.HBaseClassTestRule; 024import org.apache.hadoop.hbase.testclassification.MiscTests; 025import org.apache.hadoop.hbase.testclassification.SmallTests; 026import org.junit.ClassRule; 027import org.junit.Test; 028import org.junit.experimental.categories.Category; 029import org.slf4j.Logger; 030import org.slf4j.LoggerFactory; 031 032@Category({ MiscTests.class, SmallTests.class }) 033public class TestThreads { 034 035 @ClassRule 036 public static final HBaseClassTestRule CLASS_RULE = 037 HBaseClassTestRule.forClass(TestThreads.class); 038 039 private static final Logger LOG = LoggerFactory.getLogger(TestThreads.class); 040 041 private static final int SLEEP_TIME_MS = 3000; 042 private static final int TOLERANCE_MS = (int) (0.10 * SLEEP_TIME_MS); 043 044 private final AtomicBoolean wasInterrupted = new AtomicBoolean(false); 045 046 @Test 047 public void testSleepWithoutInterrupt() throws InterruptedException { 048 Thread sleeper = new Thread(new Runnable() { 049 @Override 050 public void run() { 051 LOG.debug("Sleeper thread: sleeping for " + SLEEP_TIME_MS); 052 Threads.sleepWithoutInterrupt(SLEEP_TIME_MS); 053 LOG.debug("Sleeper thread: finished sleeping"); 054 wasInterrupted.set(Thread.currentThread().isInterrupted()); 055 } 056 }); 057 LOG.debug("Starting sleeper thread (" + SLEEP_TIME_MS + " ms)"); 058 sleeper.start(); 059 long startTime = EnvironmentEdgeManager.currentTime(); 060 LOG.debug("Main thread: sleeping for 200 ms"); 061 Threads.sleep(200); 062 063 LOG.debug("Interrupting the sleeper thread and sleeping for 500 ms"); 064 sleeper.interrupt(); 065 Threads.sleep(500); 066 067 LOG.debug("Interrupting the sleeper thread and sleeping for 800 ms"); 068 sleeper.interrupt(); 069 Threads.sleep(800); 070 071 LOG.debug("Interrupting the sleeper thread again"); 072 sleeper.interrupt(); 073 sleeper.join(); 074 075 assertTrue("sleepWithoutInterrupt did not preserve the thread's " + "interrupted status", 076 wasInterrupted.get()); 077 078 long timeElapsed = EnvironmentEdgeManager.currentTime() - startTime; 079 // We expect to wait at least SLEEP_TIME_MS, but we can wait more if there is a GC. 080 assertTrue("Elapsed time " + timeElapsed + " ms is out of the expected " + " sleep time of " 081 + SLEEP_TIME_MS, SLEEP_TIME_MS - timeElapsed < TOLERANCE_MS); 082 LOG.debug("Target sleep time: " + SLEEP_TIME_MS + ", time elapsed: " + timeElapsed); 083 } 084}