Skip to content

Commit

Permalink
Shorten toString for AbstractFuture implementations inside util.concu…
Browse files Browse the repository at this point in the history
…rrent.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=322682481
  • Loading branch information
cpovirk committed Jul 23, 2020
1 parent f07b954 commit 3a3ebc9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2619,37 +2619,37 @@ public ListenableFuture<String> call() throws Exception {
// Waiting on backing futures
assertThat(futureResult.toString())
.matches(
"\\S+CombinedFuture@\\w+\\[status=PENDING,"
+ " info=\\[futures=\\[\\S+SettableFuture@\\w+\\[status=PENDING],"
+ " \\S+SettableFuture@\\w+\\[status=PENDING]]]]");
"CombinedFuture@\\w+\\[status=PENDING,"
+ " info=\\[futures=\\[SettableFuture@\\w+\\[status=PENDING],"
+ " SettableFuture@\\w+\\[status=PENDING]]]]");
Integer integerPartial = 1;
futureInteger.set(integerPartial);
assertThat(futureResult.toString())
.matches(
"\\S+CombinedFuture@\\w+\\[status=PENDING,"
+ " info=\\[futures=\\[\\S+SettableFuture@\\w+\\[status=SUCCESS, result=\\[1]],"
+ " \\S+SettableFuture@\\w+\\[status=PENDING]]]]");
"CombinedFuture@\\w+\\[status=PENDING,"
+ " info=\\[futures=\\[SettableFuture@\\w+\\[status=SUCCESS, result=\\[1]],"
+ " SettableFuture@\\w+\\[status=PENDING]]]]");

// Backing futures complete
Boolean booleanPartial = true;
futureBoolean.set(booleanPartial);
// Once the backing futures are done there's a (brief) moment where we know nothing
assertThat(futureResult.toString()).matches("\\S+CombinedFuture@\\w+\\[status=PENDING]");
assertThat(futureResult.toString()).matches("CombinedFuture@\\w+\\[status=PENDING]");
callableBlocking.countDown();
// Need to wait for resultFuture to be returned.
assertTrue(executor.awaitTermination(10, SECONDS));
// But once the async function has returned a future we can include that in the toString
assertThat(futureResult.toString())
.matches(
"\\S+CombinedFuture@\\w+\\[status=PENDING,"
+ " setFuture=\\[\\S+SettableFuture@\\w+\\[status=PENDING]]]");
"CombinedFuture@\\w+\\[status=PENDING,"
+ " setFuture=\\[SettableFuture@\\w+\\[status=PENDING]]]");

// Future complete
resultOfCombiner.set(createCombinedResult(getDone(futureInteger), getDone(futureBoolean)));
String expectedResult = createCombinedResult(integerPartial, booleanPartial);
assertEquals(expectedResult, futureResult.get());
assertThat(futureResult.toString())
.matches("\\S+CombinedFuture@\\w+\\[status=SUCCESS, result=\\[" + expectedResult + "]]");
.matches("CombinedFuture@\\w+\\[status=SUCCESS, result=\\[" + expectedResult + "]]");
}

public void testWhenAllComplete_asyncError() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Throwables.throwIfUnchecked;
import static java.lang.Integer.toHexString;
import static java.lang.System.identityHashCode;
import static java.util.concurrent.atomic.AtomicReferenceFieldUpdater.newUpdater;

import com.google.common.annotations.Beta;
Expand Down Expand Up @@ -1082,7 +1084,14 @@ private Listener clearListeners(Listener onto) {
// TODO(user): move parts into a default method on ListenableFuture?
@Override
public String toString() {
StringBuilder builder = new StringBuilder().append(super.toString()).append("[status=");
// TODO(cpovirk): Presize to something plausible?
StringBuilder builder = new StringBuilder();
if (getClass().getName().startsWith("com.google.common.util.concurrent.")) {
builder.append(getClass().getSimpleName());
} else {
builder.append(getClass().getName());
}
builder.append('@').append(toHexString(identityHashCode(this))).append("[status=");
if (isCancelled()) {
builder.append("CANCELLED");
} else if (isDone()) {
Expand Down
20 changes: 10 additions & 10 deletions guava-tests/test/com/google/common/util/concurrent/FuturesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2619,37 +2619,37 @@ public ListenableFuture<String> call() throws Exception {
// Waiting on backing futures
assertThat(futureResult.toString())
.matches(
"\\S+CombinedFuture@\\w+\\[status=PENDING,"
+ " info=\\[futures=\\[\\S+SettableFuture@\\w+\\[status=PENDING],"
+ " \\S+SettableFuture@\\w+\\[status=PENDING]]]]");
"CombinedFuture@\\w+\\[status=PENDING,"
+ " info=\\[futures=\\[SettableFuture@\\w+\\[status=PENDING],"
+ " SettableFuture@\\w+\\[status=PENDING]]]]");
Integer integerPartial = 1;
futureInteger.set(integerPartial);
assertThat(futureResult.toString())
.matches(
"\\S+CombinedFuture@\\w+\\[status=PENDING,"
+ " info=\\[futures=\\[\\S+SettableFuture@\\w+\\[status=SUCCESS, result=\\[1]],"
+ " \\S+SettableFuture@\\w+\\[status=PENDING]]]]");
"CombinedFuture@\\w+\\[status=PENDING,"
+ " info=\\[futures=\\[SettableFuture@\\w+\\[status=SUCCESS, result=\\[1]],"
+ " SettableFuture@\\w+\\[status=PENDING]]]]");

// Backing futures complete
Boolean booleanPartial = true;
futureBoolean.set(booleanPartial);
// Once the backing futures are done there's a (brief) moment where we know nothing
assertThat(futureResult.toString()).matches("\\S+CombinedFuture@\\w+\\[status=PENDING]");
assertThat(futureResult.toString()).matches("CombinedFuture@\\w+\\[status=PENDING]");
callableBlocking.countDown();
// Need to wait for resultFuture to be returned.
assertTrue(executor.awaitTermination(10, SECONDS));
// But once the async function has returned a future we can include that in the toString
assertThat(futureResult.toString())
.matches(
"\\S+CombinedFuture@\\w+\\[status=PENDING,"
+ " setFuture=\\[\\S+SettableFuture@\\w+\\[status=PENDING]]]");
"CombinedFuture@\\w+\\[status=PENDING,"
+ " setFuture=\\[SettableFuture@\\w+\\[status=PENDING]]]");

// Future complete
resultOfCombiner.set(createCombinedResult(getDone(futureInteger), getDone(futureBoolean)));
String expectedResult = createCombinedResult(integerPartial, booleanPartial);
assertEquals(expectedResult, futureResult.get());
assertThat(futureResult.toString())
.matches("\\S+CombinedFuture@\\w+\\[status=SUCCESS, result=\\[" + expectedResult + "]]");
.matches("CombinedFuture@\\w+\\[status=SUCCESS, result=\\[" + expectedResult + "]]");
}

public void testWhenAllComplete_asyncError() throws Exception {
Expand Down
11 changes: 10 additions & 1 deletion guava/src/com/google/common/util/concurrent/AbstractFuture.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Throwables.throwIfUnchecked;
import static java.lang.Integer.toHexString;
import static java.lang.System.identityHashCode;
import static java.util.concurrent.atomic.AtomicReferenceFieldUpdater.newUpdater;

import com.google.common.annotations.Beta;
Expand Down Expand Up @@ -1081,7 +1083,14 @@ private Listener clearListeners(Listener onto) {
// TODO(user): move parts into a default method on ListenableFuture?
@Override
public String toString() {
StringBuilder builder = new StringBuilder().append(super.toString()).append("[status=");
// TODO(cpovirk): Presize to something plausible?
StringBuilder builder = new StringBuilder();
if (getClass().getName().startsWith("com.google.common.util.concurrent.")) {
builder.append(getClass().getSimpleName());
} else {
builder.append(getClass().getName());
}
builder.append('@').append(toHexString(identityHashCode(this))).append("[status=");
if (isCancelled()) {
builder.append("CANCELLED");
} else if (isDone()) {
Expand Down

0 comments on commit 3a3ebc9

Please sign in to comment.