mirror of https://github.com/grpc/grpc-java.git
context: Implement equals and hashCode for Deadline (grpc#6075)
This commit is contained in:
parent
daad3bcdbf
commit
39dc883b34
|
|
@ -1,7 +1,7 @@
|
||||||
description = 'gRPC: Context'
|
description = 'gRPC: Context'
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
testCompile libraries.jsr305
|
testCompile libraries.jsr305, libraries.guava_testlib
|
||||||
signature "org.codehaus.mojo.signature:java17:1.0@signature"
|
signature "org.codehaus.mojo.signature:java17:1.0@signature"
|
||||||
signature "net.sf.androidscents.signature:android-api-level-14:4.0_r4@signature"
|
signature "net.sf.androidscents.signature:android-api-level-14:4.0_r4@signature"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package io.grpc;
|
package io.grpc;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
@ -231,6 +232,30 @@ public final class Deadline implements Comparable<Deadline> {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Arrays.asList(this.ticker, this.deadlineNanos).hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(final Object o) {
|
||||||
|
if (o == this) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!(o instanceof Deadline)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
final Deadline other = (Deadline) o;
|
||||||
|
if (this.ticker == null ? other.ticker != null : this.ticker != other.ticker) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (this.deadlineNanos != other.deadlineNanos) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Time source representing nanoseconds since fixed but arbitrary point in time.
|
* Time source representing nanoseconds since fixed but arbitrary point in time.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ import static org.mockito.ArgumentMatchers.eq;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import com.google.common.testing.EqualsTester;
|
||||||
import com.google.common.truth.Truth;
|
import com.google.common.truth.Truth;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
|
|
@ -310,6 +311,20 @@ public class DeadlineTest {
|
||||||
assertEquals("0.000012000s from now (ticker=FAKE_TICKER)", d.toString());
|
assertEquals("0.000012000s from now (ticker=FAKE_TICKER)", d.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void equality() {
|
||||||
|
final Deadline d1 = Deadline.after(12, TimeUnit.MICROSECONDS, ticker);
|
||||||
|
final Deadline d2 = Deadline.after(12, TimeUnit.MICROSECONDS, ticker);
|
||||||
|
final Deadline d3 = Deadline.after(12, TimeUnit.MICROSECONDS, new FakeTicker());
|
||||||
|
final Deadline d4 = Deadline.after(10, TimeUnit.MICROSECONDS, ticker);
|
||||||
|
|
||||||
|
new EqualsTester()
|
||||||
|
.addEqualityGroup(d1, d2)
|
||||||
|
.addEqualityGroup(d3)
|
||||||
|
.addEqualityGroup(d4)
|
||||||
|
.testEquals();
|
||||||
|
}
|
||||||
|
|
||||||
private static class FakeTicker extends Deadline.Ticker {
|
private static class FakeTicker extends Deadline.Ticker {
|
||||||
private long time;
|
private long time;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -296,7 +296,7 @@ final class ClientCallImpl<ReqT, RespT> extends ClientCall<ReqT, RespT> {
|
||||||
context.addListener(cancellationListener, directExecutor());
|
context.addListener(cancellationListener, directExecutor());
|
||||||
if (effectiveDeadline != null
|
if (effectiveDeadline != null
|
||||||
// If the context has the effective deadline, we don't need to schedule an extra task.
|
// If the context has the effective deadline, we don't need to schedule an extra task.
|
||||||
&& context.getDeadline() != effectiveDeadline
|
&& !effectiveDeadline.equals(context.getDeadline())
|
||||||
// If the channel has been terminated, we don't need to schedule an extra task.
|
// If the channel has been terminated, we don't need to schedule an extra task.
|
||||||
&& deadlineCancellationExecutor != null) {
|
&& deadlineCancellationExecutor != null) {
|
||||||
deadlineCancellationFuture = startDeadlineTimer(effectiveDeadline);
|
deadlineCancellationFuture = startDeadlineTimer(effectiveDeadline);
|
||||||
|
|
@ -314,7 +314,7 @@ final class ClientCallImpl<ReqT, RespT> extends ClientCall<ReqT, RespT> {
|
||||||
Deadline effectiveDeadline, @Nullable Deadline outerCallDeadline,
|
Deadline effectiveDeadline, @Nullable Deadline outerCallDeadline,
|
||||||
@Nullable Deadline callDeadline) {
|
@Nullable Deadline callDeadline) {
|
||||||
if (!log.isLoggable(Level.FINE) || effectiveDeadline == null
|
if (!log.isLoggable(Level.FINE) || effectiveDeadline == null
|
||||||
|| outerCallDeadline != effectiveDeadline) {
|
|| !effectiveDeadline.equals(outerCallDeadline)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue