Skip to content

Commit

Permalink
Switch FutureCallbackTest to an explicit monitor for j2kt compatibility.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 651003834
  • Loading branch information
stefanhaustein authored and Google Java Core Libraries committed Jul 10, 2024
1 parent c28e652 commit 2f6d618
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void testCancel() {
SettableFuture<String> f = SettableFuture.create();
FutureCallback<String> callback =
new FutureCallback<String>() {
private final Object monitor = new Object();
private boolean called = false;

@Override
Expand All @@ -70,10 +71,12 @@ public void onSuccess(String result) {
}

@Override
public synchronized void onFailure(Throwable t) {
assertFalse(called);
assertThat(t).isInstanceOf(CancellationException.class);
called = true;
public void onFailure(Throwable t) {
synchronized (monitor) {
assertFalse(called);
assertThat(t).isInstanceOf(CancellationException.class);
called = true;
}
}
};
addCallback(f, callback, directExecutor());
Expand Down Expand Up @@ -180,6 +183,7 @@ private final class MockCallback implements FutureCallback<String> {
@Nullable private String value = null;
@Nullable private Throwable failure = null;
private boolean wasCalled = false;
private final Object monitor = new Object();

MockCallback(String expectedValue) {
this.value = expectedValue;
Expand All @@ -190,17 +194,21 @@ public MockCallback(Throwable expectedFailure) {
}

@Override
public synchronized void onSuccess(String result) {
assertFalse(wasCalled);
wasCalled = true;
assertEquals(value, result);
public void onSuccess(String result) {
synchronized (monitor) {
assertFalse(wasCalled);
wasCalled = true;
assertEquals(value, result);
}
}

@Override
public synchronized void onFailure(Throwable t) {
assertFalse(wasCalled);
wasCalled = true;
assertEquals(failure, t);
synchronized (monitor) {
assertFalse(wasCalled);
wasCalled = true;
assertEquals(failure, t);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void testCancel() {
SettableFuture<String> f = SettableFuture.create();
FutureCallback<String> callback =
new FutureCallback<String>() {
private final Object monitor = new Object();
private boolean called = false;

@Override
Expand All @@ -70,10 +71,12 @@ public void onSuccess(String result) {
}

@Override
public synchronized void onFailure(Throwable t) {
assertFalse(called);
assertThat(t).isInstanceOf(CancellationException.class);
called = true;
public void onFailure(Throwable t) {
synchronized (monitor) {
assertFalse(called);
assertThat(t).isInstanceOf(CancellationException.class);
called = true;
}
}
};
addCallback(f, callback, directExecutor());
Expand Down Expand Up @@ -180,6 +183,7 @@ private final class MockCallback implements FutureCallback<String> {
@Nullable private String value = null;
@Nullable private Throwable failure = null;
private boolean wasCalled = false;
private final Object monitor = new Object();

MockCallback(String expectedValue) {
this.value = expectedValue;
Expand All @@ -190,17 +194,21 @@ public MockCallback(Throwable expectedFailure) {
}

@Override
public synchronized void onSuccess(String result) {
assertFalse(wasCalled);
wasCalled = true;
assertEquals(value, result);
public void onSuccess(String result) {
synchronized (monitor) {
assertFalse(wasCalled);
wasCalled = true;
assertEquals(value, result);
}
}

@Override
public synchronized void onFailure(Throwable t) {
assertFalse(wasCalled);
wasCalled = true;
assertEquals(failure, t);
synchronized (monitor) {
assertFalse(wasCalled);
wasCalled = true;
assertEquals(failure, t);
}
}
}
}

0 comments on commit 2f6d618

Please sign in to comment.