mirror of https://github.com/grpc/grpc-java.git
context: make Deadline toString() more readable
This commit is contained in:
parent
28587b449b
commit
2d654496ee
|
|
@ -36,6 +36,7 @@ public final class Deadline implements Comparable<Deadline> {
|
||||||
// to prevent wraparound as long as process runs for less than ~100 years.
|
// to prevent wraparound as long as process runs for less than ~100 years.
|
||||||
private static final long MAX_OFFSET = TimeUnit.DAYS.toNanos(100 * 365);
|
private static final long MAX_OFFSET = TimeUnit.DAYS.toNanos(100 * 365);
|
||||||
private static final long MIN_OFFSET = -MAX_OFFSET;
|
private static final long MIN_OFFSET = -MAX_OFFSET;
|
||||||
|
private static final long NANOS_PER_SECOND = TimeUnit.SECONDS.toNanos(1);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a deadline that will expire at the specified offset from the current system clock.
|
* Create a deadline that will expire at the specified offset from the current system clock.
|
||||||
|
|
@ -89,6 +90,7 @@ public final class Deadline implements Comparable<Deadline> {
|
||||||
* Is {@code this} deadline before another.
|
* Is {@code this} deadline before another.
|
||||||
*/
|
*/
|
||||||
public boolean isBefore(Deadline other) {
|
public boolean isBefore(Deadline other) {
|
||||||
|
assert this.ticker == other.ticker : "Tickers don't match";
|
||||||
return this.deadlineNanos - other.deadlineNanos < 0;
|
return this.deadlineNanos - other.deadlineNanos < 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -97,6 +99,7 @@ public final class Deadline implements Comparable<Deadline> {
|
||||||
* @param other deadline to compare with {@code this}.
|
* @param other deadline to compare with {@code this}.
|
||||||
*/
|
*/
|
||||||
public Deadline minimum(Deadline other) {
|
public Deadline minimum(Deadline other) {
|
||||||
|
assert this.ticker == other.ticker : "Tickers don't match";
|
||||||
return isBefore(other) ? this : other;
|
return isBefore(other) ? this : other;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -142,11 +145,25 @@ public final class Deadline implements Comparable<Deadline> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return timeRemaining(TimeUnit.NANOSECONDS) + " ns from now";
|
long remainingNanos = timeRemaining(TimeUnit.NANOSECONDS);
|
||||||
|
long seconds = Math.abs(remainingNanos) / NANOS_PER_SECOND;
|
||||||
|
long nanos = Math.abs(remainingNanos) % NANOS_PER_SECOND;
|
||||||
|
|
||||||
|
StringBuilder buf = new StringBuilder();
|
||||||
|
if (remainingNanos < 0) {
|
||||||
|
buf.append('-');
|
||||||
|
}
|
||||||
|
buf.append(seconds);
|
||||||
|
if (nanos > 0) {
|
||||||
|
buf.append(String.format(".%09d", nanos));
|
||||||
|
}
|
||||||
|
buf.append("s from now");
|
||||||
|
return buf.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(Deadline that) {
|
public int compareTo(Deadline that) {
|
||||||
|
assert this.ticker == that.ticker : "Tickers don't match";
|
||||||
long diff = this.deadlineNanos - that.deadlineNanos;
|
long diff = this.deadlineNanos - that.deadlineNanos;
|
||||||
if (diff < 0) {
|
if (diff < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
|
|
|
||||||
|
|
@ -211,13 +211,25 @@ public class DeadlineTest {
|
||||||
@Test
|
@Test
|
||||||
public void toString_exact() {
|
public void toString_exact() {
|
||||||
Deadline d = Deadline.after(0, TimeUnit.MILLISECONDS, ticker);
|
Deadline d = Deadline.after(0, TimeUnit.MILLISECONDS, ticker);
|
||||||
assertEquals("0 ns from now", d.toString());
|
assertEquals("0s from now", d.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toString_after() {
|
public void toString_after() {
|
||||||
Deadline d = Deadline.after(-1, TimeUnit.MINUTES, ticker);
|
Deadline d;
|
||||||
assertEquals("-60000000000 ns from now", d.toString());
|
|
||||||
|
d = Deadline.after(-1, TimeUnit.MINUTES, ticker);
|
||||||
|
assertEquals("-60s from now", d.toString());
|
||||||
|
d = Deadline.after(-1, TimeUnit.MILLISECONDS, ticker);
|
||||||
|
assertEquals("-0.001000000s from now", d.toString());
|
||||||
|
d = Deadline.after(-500, TimeUnit.MILLISECONDS, ticker);
|
||||||
|
assertEquals("-0.500000000s from now", d.toString());
|
||||||
|
d = Deadline.after(-1000, TimeUnit.MILLISECONDS, ticker);
|
||||||
|
assertEquals("-1s from now", d.toString());
|
||||||
|
d = Deadline.after(-1500, TimeUnit.MILLISECONDS, ticker);
|
||||||
|
assertEquals("-1.500000000s from now", d.toString());
|
||||||
|
d = Deadline.after(-1023456789, TimeUnit.NANOSECONDS, ticker);
|
||||||
|
assertEquals("-1.023456789s from now", d.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -246,7 +258,7 @@ public class DeadlineTest {
|
||||||
@Test
|
@Test
|
||||||
public void toString_before() {
|
public void toString_before() {
|
||||||
Deadline d = Deadline.after(12, TimeUnit.MICROSECONDS, ticker);
|
Deadline d = Deadline.after(12, TimeUnit.MICROSECONDS, ticker);
|
||||||
assertEquals("12000 ns from now", d.toString());
|
assertEquals("0.000012000s from now", d.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class FakeTicker extends Deadline.Ticker {
|
private static class FakeTicker extends Deadline.Ticker {
|
||||||
|
|
|
||||||
|
|
@ -168,7 +168,7 @@ public class CallOptionsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toStringMatches_withDeadline() {
|
public void toStringMatches_withDeadline() {
|
||||||
assertThat(allSet.toString()).contains("1 ns from now");
|
assertThat(allSet.toString()).contains("0.000000001s from now");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue