Skip to content

Commit 30d4f95

Browse files
committed
Deflake CallOptionsTest.withDeadlineNanoTime
Two prong approach: increase the tolerance and decrease code between nanoTime() calls. Fixes grpc#1645
1 parent 73a2b77 commit 30d4f95

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

core/src/test/java/io/grpc/CallOptionsTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,17 @@ public void toStringMatches_withDeadline() {
152152
@Test
153153
@SuppressWarnings("deprecation")
154154
public void withDeadlineNanoTime() {
155-
CallOptions opts = CallOptions.DEFAULT.withDeadlineNanoTime(System.nanoTime());
155+
// Create Deadline near calling System.nanoTime to reduce clock differences
156+
Deadline reference = Deadline.after(-1, NANOSECONDS);
157+
long rawDeadline = System.nanoTime() - 1;
158+
CallOptions opts = CallOptions.DEFAULT.withDeadlineNanoTime(rawDeadline);
156159
assertThat(opts.getDeadlineNanoTime()).isNotNull();
160+
// This is not technically correct, since nanoTime is permitted to overflow, but the chances of
161+
// that impacting this test are very remote.
157162
assertThat(opts.getDeadlineNanoTime()).isAtMost(System.nanoTime());
158163
assertThat(opts.getDeadline().isExpired()).isTrue();
159164

160-
assertAbout(deadline()).that(opts.getDeadline())
161-
.isWithin(20, MILLISECONDS).of(Deadline.after(0, SECONDS));
165+
assertAbout(deadline()).that(opts.getDeadline()).isWithin(50, MILLISECONDS).of(reference);
162166
}
163167

164168
// TODO(carl-mastrangelo): consider making a CallOptionsSubject for Truth.

0 commit comments

Comments
 (0)