Skip to content

Commit

Permalink
Write our own FutureCallback implementation instead of using a mock.
Browse files Browse the repository at this point in the history
This addresses [a class-loader mismatch under JDK20](#5801 (comment)). As a bonus, it makes the test work under GWT.

PiperOrigin-RevId: 488665105
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Nov 15, 2022
1 parent dafaa3e commit 166d8c0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import java.util.concurrent.CancellationException;
import java.util.concurrent.Executor;
import javax.annotation.CheckForNull;
import junit.framework.TestCase;
import org.mockito.Mockito;

/**
* Test for {@link FutureCallback}.
*
* @author Anthony Zana
*/
@GwtCompatible(emulated = true)
@GwtCompatible
public class FutureCallbackTest extends TestCase {
public void testSameThreadSuccess() {
SettableFuture<String> f = SettableFuture.create();
Expand Down Expand Up @@ -96,40 +94,62 @@ public void testRuntimeExceptionFromGet() {
addCallback(f, callback, directExecutor());
}

@GwtIncompatible // Mockito
public void testOnSuccessThrowsRuntimeException() throws Exception {
RuntimeException exception = new RuntimeException();
String result = "result";
SettableFuture<String> future = SettableFuture.create();
@SuppressWarnings("unchecked") // Safe for a mock
FutureCallback<String> callback = Mockito.mock(FutureCallback.class);
int[] successCalls = new int[1];
int[] failureCalls = new int[1];
FutureCallback<String> callback =
new FutureCallback<String>() {
@Override
public void onSuccess(String result) {
successCalls[0]++;
throw exception;
}

@Override
public void onFailure(Throwable t) {
failureCalls[0]++;
}
};
addCallback(future, callback, directExecutor());
Mockito.doThrow(exception).when(callback).onSuccess(result);
future.set(result);
assertEquals(result, future.get());
Mockito.verify(callback).onSuccess(result);
Mockito.verifyNoMoreInteractions(callback);
assertThat(successCalls[0]).isEqualTo(1);
assertThat(failureCalls[0]).isEqualTo(0);
}

@GwtIncompatible // Mockito
public void testOnSuccessThrowsError() throws Exception {
class TestError extends Error {}
TestError error = new TestError();
String result = "result";
SettableFuture<String> future = SettableFuture.create();
@SuppressWarnings("unchecked") // Safe for a mock
FutureCallback<String> callback = Mockito.mock(FutureCallback.class);
int[] successCalls = new int[1];
int[] failureCalls = new int[1];
FutureCallback<String> callback =
new FutureCallback<String>() {
@Override
public void onSuccess(String result) {
successCalls[0]++;
throw error;
}

@Override
public void onFailure(Throwable t) {
failureCalls[0]++;
}
};
addCallback(future, callback, directExecutor());
Mockito.doThrow(error).when(callback).onSuccess(result);
try {
future.set(result);
fail("Should have thrown");
} catch (TestError e) {
assertSame(error, e);
}
assertEquals(result, future.get());
Mockito.verify(callback).onSuccess(result);
Mockito.verifyNoMoreInteractions(callback);
assertThat(successCalls[0]).isEqualTo(1);
assertThat(failureCalls[0]).isEqualTo(0);
}

public void testWildcardFuture() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import java.util.concurrent.CancellationException;
import java.util.concurrent.Executor;
import junit.framework.TestCase;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.mockito.Mockito;

/**
* Test for {@link FutureCallback}.
*
* @author Anthony Zana
*/
@GwtCompatible(emulated = true)
@GwtCompatible
public class FutureCallbackTest extends TestCase {
public void testSameThreadSuccess() {
SettableFuture<String> f = SettableFuture.create();
Expand Down Expand Up @@ -96,40 +94,62 @@ public void testRuntimeExceptionFromGet() {
addCallback(f, callback, directExecutor());
}

@GwtIncompatible // Mockito
public void testOnSuccessThrowsRuntimeException() throws Exception {
RuntimeException exception = new RuntimeException();
String result = "result";
SettableFuture<String> future = SettableFuture.create();
@SuppressWarnings("unchecked") // Safe for a mock
FutureCallback<String> callback = Mockito.mock(FutureCallback.class);
int[] successCalls = new int[1];
int[] failureCalls = new int[1];
FutureCallback<String> callback =
new FutureCallback<String>() {
@Override
public void onSuccess(String result) {
successCalls[0]++;
throw exception;
}

@Override
public void onFailure(Throwable t) {
failureCalls[0]++;
}
};
addCallback(future, callback, directExecutor());
Mockito.doThrow(exception).when(callback).onSuccess(result);
future.set(result);
assertEquals(result, future.get());
Mockito.verify(callback).onSuccess(result);
Mockito.verifyNoMoreInteractions(callback);
assertThat(successCalls[0]).isEqualTo(1);
assertThat(failureCalls[0]).isEqualTo(0);
}

@GwtIncompatible // Mockito
public void testOnSuccessThrowsError() throws Exception {
class TestError extends Error {}
TestError error = new TestError();
String result = "result";
SettableFuture<String> future = SettableFuture.create();
@SuppressWarnings("unchecked") // Safe for a mock
FutureCallback<String> callback = Mockito.mock(FutureCallback.class);
int[] successCalls = new int[1];
int[] failureCalls = new int[1];
FutureCallback<String> callback =
new FutureCallback<String>() {
@Override
public void onSuccess(String result) {
successCalls[0]++;
throw error;
}

@Override
public void onFailure(Throwable t) {
failureCalls[0]++;
}
};
addCallback(future, callback, directExecutor());
Mockito.doThrow(error).when(callback).onSuccess(result);
try {
future.set(result);
fail("Should have thrown");
} catch (TestError e) {
assertSame(error, e);
}
assertEquals(result, future.get());
Mockito.verify(callback).onSuccess(result);
Mockito.verifyNoMoreInteractions(callback);
assertThat(successCalls[0]).isEqualTo(1);
assertThat(failureCalls[0]).isEqualTo(0);
}

public void testWildcardFuture() {
Expand Down

0 comments on commit 166d8c0

Please sign in to comment.