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.jupiter.api.Assertions.assertTrue; 021 022import java.util.concurrent.atomic.AtomicBoolean; 023import org.apache.hadoop.hbase.testclassification.MiscTests; 024import org.apache.hadoop.hbase.testclassification.SmallTests; 025import org.junit.jupiter.api.Tag; 026import org.junit.jupiter.api.Test; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029 030@Tag(MiscTests.TAG) 031@Tag(SmallTests.TAG) 032public class TestThreads { 033 034 private static final Logger LOG = LoggerFactory.getLogger(TestThreads.class); 035 036 private static final int SLEEP_TIME_MS = 3000; 037 private static final int TOLERANCE_MS = (int) (0.10 * SLEEP_TIME_MS); 038 039 private final AtomicBoolean wasInterrupted = new AtomicBoolean(false); 040 041 @Test 042 public void testSleepWithoutInterrupt() throws InterruptedException { 043 Thread sleeper = new Thread(new Runnable() { 044 @Override 045 public void run() { 046 LOG.debug("Sleeper thread: sleeping for " + SLEEP_TIME_MS); 047 Threads.sleepWithoutInterrupt(SLEEP_TIME_MS); 048 LOG.debug("Sleeper thread: finished sleeping"); 049 wasInterrupted.set(Thread.currentThread().isInterrupted()); 050 } 051 }); 052 LOG.debug("Starting sleeper thread (" + SLEEP_TIME_MS + " ms)"); 053 sleeper.start(); 054 long startTime = EnvironmentEdgeManager.currentTime(); 055 LOG.debug("Main thread: sleeping for 200 ms"); 056 Threads.sleep(200); 057 058 LOG.debug("Interrupting the sleeper thread and sleeping for 500 ms"); 059 sleeper.interrupt(); 060 Threads.sleep(500); 061 062 LOG.debug("Interrupting the sleeper thread and sleeping for 800 ms"); 063 sleeper.interrupt(); 064 Threads.sleep(800); 065 066 LOG.debug("Interrupting the sleeper thread again"); 067 sleeper.interrupt(); 068 sleeper.join(); 069 070 assertTrue(wasInterrupted.get(), 071 "sleepWithoutInterrupt did not preserve the thread's " + "interrupted status"); 072 073 long timeElapsed = EnvironmentEdgeManager.currentTime() - startTime; 074 // We expect to wait at least SLEEP_TIME_MS, but we can wait more if there is a GC. 075 assertTrue(SLEEP_TIME_MS - timeElapsed < TOLERANCE_MS, "Elapsed time " + timeElapsed 076 + " ms is out of the expected " + " sleep time of " + SLEEP_TIME_MS); 077 LOG.debug("Target sleep time: " + SLEEP_TIME_MS + ", time elapsed: " + timeElapsed); 078 } 079}