001/* 002 * Copyright (C) 2008 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy 006 * of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 017package com.google.common.util.concurrent.testing; 018 019import static com.google.common.util.concurrent.MoreExecutors.directExecutor; 020 021import com.google.common.annotations.Beta; 022import com.google.common.annotations.GwtIncompatible; 023import com.google.common.util.concurrent.ListenableFuture; 024import java.util.concurrent.CountDownLatch; 025import java.util.concurrent.ExecutionException; 026import java.util.concurrent.TimeUnit; 027import junit.framework.Assert; 028 029/** 030 * A simple mock implementation of {@code Runnable} that can be used for testing ListenableFutures. 031 * 032 * @author Nishant Thakkar 033 * @since 10.0 034 */ 035@Beta 036@GwtIncompatible 037public class MockFutureListener implements Runnable { 038 private final CountDownLatch countDownLatch; 039 private final ListenableFuture<?> future; 040 041 public MockFutureListener(ListenableFuture<?> future) { 042 this.countDownLatch = new CountDownLatch(1); 043 this.future = future; 044 045 future.addListener(this, directExecutor()); 046 } 047 048 @Override 049 public void run() { 050 countDownLatch.countDown(); 051 } 052 053 /** 054 * Verify that the listener completes in a reasonable amount of time, and Asserts that the future 055 * returns the expected data. 056 * 057 * @throws Throwable if the listener isn't called or if it resulted in a throwable or if the 058 * result doesn't match the expected value. 059 */ 060 public void assertSuccess(Object expectedData) throws Throwable { 061 // Verify that the listener executed in a reasonable amount of time. 062 Assert.assertTrue(countDownLatch.await(1L, TimeUnit.SECONDS)); 063 064 try { 065 Assert.assertEquals(expectedData, future.get()); 066 } catch (ExecutionException e) { 067 throw e.getCause(); 068 } 069 } 070 071 /** 072 * Verify that the listener completes in a reasonable amount of time, and Asserts that the future 073 * throws an {@code ExecutableException} and that the cause of the {@code ExecutableException} is 074 * {@code expectedCause}. 075 */ 076 public void assertException(Throwable expectedCause) throws Exception { 077 // Verify that the listener executed in a reasonable amount of time. 078 Assert.assertTrue(countDownLatch.await(1L, TimeUnit.SECONDS)); 079 080 try { 081 future.get(); 082 Assert.fail("This call was supposed to throw an ExecutionException"); 083 } catch (ExecutionException expected) { 084 Assert.assertSame(expectedCause, expected.getCause()); 085 } 086 } 087 088 public void assertTimeout() throws Exception { 089 // Verify that the listener does not get called in a reasonable amount of 090 // time. 091 Assert.assertFalse(countDownLatch.await(1L, TimeUnit.SECONDS)); 092 } 093}