1 /* 2 * Copyright (C) 2017 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.util.concurrent; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static com.google.common.util.concurrent.Futures.immediateFuture; 21 import static com.google.common.util.concurrent.MoreExecutors.directExecutor; 22 import static com.google.common.util.concurrent.Uninterruptibles.getUninterruptibly; 23 import static org.junit.Assert.assertThrows; 24 25 import com.google.common.util.concurrent.ClosingFuture.ClosingCallable; 26 import com.google.common.util.concurrent.ClosingFuture.DeferredCloser; 27 import java.io.Closeable; 28 import java.util.concurrent.ExecutionException; 29 30 /** Tests for {@link ClosingFuture} that exercise {@link ClosingFuture#finishToFuture()}. */ 31 public class ClosingFutureFinishToFutureTest extends AbstractClosingFutureTest { testFinishToFuture_throwsIfCalledTwice()32 public void testFinishToFuture_throwsIfCalledTwice() throws Exception { 33 ClosingFuture<Closeable> closingFuture = 34 ClosingFuture.submit( 35 new ClosingCallable<Closeable>() { 36 @Override 37 public Closeable call(DeferredCloser closer) throws Exception { 38 return closer.eventuallyClose(mockCloseable, executor); 39 } 40 }, 41 executor); 42 FluentFuture<Closeable> unused = closingFuture.finishToFuture(); 43 assertThrows( 44 IllegalStateException.class, 45 () -> { 46 FluentFuture<Closeable> unused2 = closingFuture.finishToFuture(); 47 }); 48 } 49 testFinishToFuture_throwsAfterCallingFinishToValueAndCloser()50 public void testFinishToFuture_throwsAfterCallingFinishToValueAndCloser() throws Exception { 51 ClosingFuture<Closeable> closingFuture = 52 ClosingFuture.submit( 53 new ClosingCallable<Closeable>() { 54 @Override 55 public Closeable call(DeferredCloser closer) throws Exception { 56 return closer.eventuallyClose(mockCloseable, executor); 57 } 58 }, 59 executor); 60 closingFuture.finishToValueAndCloser(new NoOpValueAndCloserConsumer<>(), directExecutor()); 61 assertThrows( 62 IllegalStateException.class, 63 () -> { 64 FluentFuture<Closeable> unused = closingFuture.finishToFuture(); 65 }); 66 } 67 testFinishToFuture_preventsFurtherDerivation()68 public void testFinishToFuture_preventsFurtherDerivation() { 69 ClosingFuture<String> closingFuture = ClosingFuture.from(immediateFuture("value1")); 70 FluentFuture<String> unused = closingFuture.finishToFuture(); 71 assertDerivingThrowsIllegalStateException(closingFuture); 72 } 73 74 @Override getFinalValue(ClosingFuture<T> closingFuture)75 <T> T getFinalValue(ClosingFuture<T> closingFuture) throws ExecutionException { 76 return getUninterruptibly(closingFuture.finishToFuture()); 77 } 78 79 @Override assertFinallyFailsWithException(ClosingFuture<?> closingFuture)80 void assertFinallyFailsWithException(ClosingFuture<?> closingFuture) { 81 assertThatFutureFailsWithException(closingFuture.finishToFuture()); 82 } 83 84 @Override assertBecomesCanceled(ClosingFuture<?> closingFuture)85 void assertBecomesCanceled(ClosingFuture<?> closingFuture) throws ExecutionException { 86 assertThatFutureBecomesCancelled(closingFuture.finishToFuture()); 87 } 88 89 @Override cancelFinalStepAndWait(ClosingFuture<TestCloseable> closingFuture)90 void cancelFinalStepAndWait(ClosingFuture<TestCloseable> closingFuture) { 91 assertThat(closingFuture.finishToFuture().cancel(false)).isTrue(); 92 waitUntilClosed(closingFuture); 93 futureCancelled.countDown(); 94 } 95 } 96