Skip to content

Commit 25a8b7c

Browse files
hlx502ejona86
authored andcommitted
Support setting onReadyThreshold through AbstractStub
Add copy of the onReadyThreshold property when copying CallOptions(fix bug)
1 parent e7c3803 commit 25a8b7c

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

api/src/main/java/io/grpc/CallOptions.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ private static Builder toBuilder(CallOptions other) {
512512
builder.waitForReady = other.waitForReady;
513513
builder.maxInboundMessageSize = other.maxInboundMessageSize;
514514
builder.maxOutboundMessageSize = other.maxOutboundMessageSize;
515+
builder.onReadyThreshold = other.onReadyThreshold;
515516
return builder;
516517
}
517518

@@ -527,6 +528,7 @@ public String toString() {
527528
.add("waitForReady", isWaitForReady())
528529
.add("maxInboundMessageSize", maxInboundMessageSize)
529530
.add("maxOutboundMessageSize", maxOutboundMessageSize)
531+
.add("onReadyThreshold", onReadyThreshold)
530532
.add("streamTracerFactories", streamTracerFactories)
531533
.toString();
532534
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ public void withAndWithoutWaitForReady() {
8181
.isFalse();
8282
}
8383

84+
@Test
85+
public void withOnReadyThreshold() {
86+
int onReadyThreshold = 1024;
87+
CallOptions callOptions = CallOptions.DEFAULT.withOnReadyThreshold(onReadyThreshold);
88+
callOptions = callOptions.withWaitForReady();
89+
assertThat(callOptions.getOnReadyThreshold()).isEqualTo(onReadyThreshold);
90+
callOptions = callOptions.clearOnReadyThreshold();
91+
assertThat(callOptions.getOnReadyThreshold()).isNull();
92+
}
93+
8494
@Test
8595
public void allWiths() {
8696
assertThat(allSet.getAuthority()).isSameInstanceAs(sampleAuthority);
@@ -148,6 +158,7 @@ public void toStringMatches_noDeadline_default() {
148158
.withCallCredentials(null)
149159
.withMaxInboundMessageSize(44)
150160
.withMaxOutboundMessageSize(55)
161+
.withOnReadyThreshold(1024)
151162
.toString();
152163

153164
assertThat(actual).contains("deadline=null");
@@ -159,6 +170,7 @@ public void toStringMatches_noDeadline_default() {
159170
assertThat(actual).contains("waitForReady=true");
160171
assertThat(actual).contains("maxInboundMessageSize=44");
161172
assertThat(actual).contains("maxOutboundMessageSize=55");
173+
assertThat(actual).contains("onReadyThreshold=1024");
162174
assertThat(actual).contains("streamTracerFactories=[tracerFactory1, tracerFactory2]");
163175
}
164176

stub/src/main/java/io/grpc/stub/AbstractStub.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,16 @@ public final S withMaxOutboundMessageSize(int maxSize) {
252252
return build(channel, callOptions.withMaxOutboundMessageSize(maxSize));
253253
}
254254

255+
/**
256+
* Returns a new stub that limits the maximum number of bytes per stream in the queue.
257+
*
258+
* @since 1.1.0
259+
*/
260+
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/11021")
261+
public final S withOnReadyThreshold(int numBytes) {
262+
return build(channel, callOptions.withOnReadyThreshold(numBytes));
263+
}
264+
255265
/**
256266
* A factory class for stub.
257267
*

stub/src/test/java/io/grpc/stub/BaseAbstractStubTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.grpc.stub;
1818

19+
import static com.google.common.truth.Truth.assertThat;
1920
import static org.junit.Assert.assertEquals;
2021
import static org.junit.Assert.assertFalse;
2122
import static org.junit.Assert.assertNull;
@@ -90,4 +91,16 @@ public void withExecutor() {
9091

9192
assertEquals(callOptions.getExecutor(), executor);
9293
}
94+
95+
@Test
96+
public void withOnReadyThreshold() {
97+
T stub = create(channel);
98+
CallOptions callOptions = stub.getCallOptions();
99+
assertNull(callOptions.getOnReadyThreshold());
100+
101+
int onReadyThreshold = 1024;
102+
stub = stub.withOnReadyThreshold(onReadyThreshold);
103+
callOptions = stub.getCallOptions();
104+
assertThat(callOptions.getOnReadyThreshold()).isEqualTo(onReadyThreshold);
105+
}
93106
}

0 commit comments

Comments
 (0)