All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.internal.lang.ThreadUtilsTest Maven / Gradle / Ivy

There is a newer version: 2.0-BETA
Show newest version
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You
 * may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License. See accompanying
 * LICENSE file.
 */

package com.gemstone.gemfire.internal.lang;

import static org.junit.Assert.*;

import java.lang.Thread.State;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;

import edu.umd.cs.mtc.MultithreadedTestCase;
import edu.umd.cs.mtc.TestFramework;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * The ThreadUtilsTest class is a test suite of test cases for testing the contract and functionality of the ThreadUtils
 * class.
 * 

* @author John Blum * @see com.gemstone.gemfire.internal.lang.ThreadUtils * @see org.jmock.Expectations * @see org.jmock.Mockery * @see org.junit.Assert * @see org.junit.Test * @since 7.0 */ public class ThreadUtilsTest { protected Mockery mockContext; @Before public void setup() { mockContext = new Mockery() {{ setImposteriser(ClassImposteriser.INSTANCE); }}; } @After public void tearDown() { mockContext.assertIsSatisfied(); } @Test public void testGetThreadNameWithNull() { assertNull(ThreadUtils.getThreadName(null)); } @Test public void testGetThreadNameWithThread() { assertNotNull(ThreadUtils.getThreadName(Thread.currentThread())); } @Test public void testInterruptWithNullThread() { ThreadUtils.interrupt(null); } @Test public void testInterruptWithNonNullThread() { final Thread mockThread = mockContext.mock(Thread.class, "Interrupted Thread"); mockContext.checking(new Expectations() {{ oneOf(mockThread).interrupt(); }}); ThreadUtils.interrupt(mockThread); } @Test public void testIsAlive() { assertTrue(ThreadUtils.isAlive(Thread.currentThread())); } @Test public void testIsAliveWithNullThread() { assertFalse(ThreadUtils.isAlive(null)); } @Test public void testIsAliveWithUnstartedThread() { final Thread thread = new Thread(new Runnable() { public void run() { } }); assertFalse(ThreadUtils.isAlive(thread)); } @Test public void testIsAliveWithStoppedThread() throws InterruptedException { final AtomicBoolean ran = new AtomicBoolean(false); final Thread thread = new Thread(new Runnable() { public void run() { ran.set(true); } }); thread.start(); thread.join(50); assertFalse(ThreadUtils.isAlive(thread)); assertTrue(ran.get()); } @Test public void testIsWaitingWithNullThread() { assertFalse(ThreadUtils.isWaiting(null)); } @Test public void testIsWaitingWithRunningThread() { final Thread runningThread = mockContext.mock(Thread.class, "Running Thread"); mockContext.checking(new Expectations() {{ oneOf(runningThread).getState(); will(returnValue(State.RUNNABLE)); }}); assertFalse(ThreadUtils.isWaiting(runningThread)); } @Test public void testIsWaitingWithWaitingThread() { final Thread waitingThread = mockContext.mock(Thread.class, "Waiting Thread"); mockContext.checking(new Expectations() {{ one(waitingThread).getState(); will(returnValue(State.WAITING)); }}); assertTrue(ThreadUtils.isWaiting(waitingThread)); } @Test public void testSleep() { final long t0 = System.currentTimeMillis(); final long sleepDuration = ThreadUtils.sleep(500); final long t1 = System.currentTimeMillis(); assertTrue((t1 - t0) >= 500); assertTrue(sleepDuration >= 500); } @Test public void testSleepWithInterrupt() throws Throwable { TestFramework.runOnce(new SleepInterruptedMultithreadedTestCase(5 * 1000)); } protected static final class SleepInterruptedMultithreadedTestCase extends MultithreadedTestCase { private final long expectedSleepDuration; private volatile long actualSleepDuration; private final CountDownLatch latch; private volatile Thread sleeperThread; public SleepInterruptedMultithreadedTestCase(final long expectedSleepDuration) { assert expectedSleepDuration > 0 : "The duration of sleep must be greater than equal to 0!"; this.expectedSleepDuration = expectedSleepDuration; this.latch = new CountDownLatch(1); } public void thread1() { assertTick(0); sleeperThread = Thread.currentThread(); this.latch.countDown(); sleeperThread.setName("Sleeper Thread"); actualSleepDuration = ThreadUtils.sleep(expectedSleepDuration); } public void thread2() throws Exception { assertTick(0); Thread.currentThread().setName("Interrupting Thread"); this.latch.await(); sleeperThread.interrupt(); } @Override public void finish() { assert(actualSleepDuration <= expectedSleepDuration); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy