Skip to content

Commit

Permalink
Support setting onReadyThreshold through AbstractStub
Browse files Browse the repository at this point in the history
Add copy of the onReadyThreshold property when copying CallOptions(fix bug)
  • Loading branch information
hlx502 authored and ejona86 committed Jun 27, 2024
1 parent e7c3803 commit 25a8b7c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/src/main/java/io/grpc/CallOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ private static Builder toBuilder(CallOptions other) {
builder.waitForReady = other.waitForReady;
builder.maxInboundMessageSize = other.maxInboundMessageSize;
builder.maxOutboundMessageSize = other.maxOutboundMessageSize;
builder.onReadyThreshold = other.onReadyThreshold;
return builder;
}

Expand All @@ -527,6 +528,7 @@ public String toString() {
.add("waitForReady", isWaitForReady())
.add("maxInboundMessageSize", maxInboundMessageSize)
.add("maxOutboundMessageSize", maxOutboundMessageSize)
.add("onReadyThreshold", onReadyThreshold)
.add("streamTracerFactories", streamTracerFactories)
.toString();
}
Expand Down
12 changes: 12 additions & 0 deletions api/src/test/java/io/grpc/CallOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ public void withAndWithoutWaitForReady() {
.isFalse();
}

@Test
public void withOnReadyThreshold() {
int onReadyThreshold = 1024;
CallOptions callOptions = CallOptions.DEFAULT.withOnReadyThreshold(onReadyThreshold);
callOptions = callOptions.withWaitForReady();
assertThat(callOptions.getOnReadyThreshold()).isEqualTo(onReadyThreshold);
callOptions = callOptions.clearOnReadyThreshold();
assertThat(callOptions.getOnReadyThreshold()).isNull();
}

@Test
public void allWiths() {
assertThat(allSet.getAuthority()).isSameInstanceAs(sampleAuthority);
Expand Down Expand Up @@ -148,6 +158,7 @@ public void toStringMatches_noDeadline_default() {
.withCallCredentials(null)
.withMaxInboundMessageSize(44)
.withMaxOutboundMessageSize(55)
.withOnReadyThreshold(1024)
.toString();

assertThat(actual).contains("deadline=null");
Expand All @@ -159,6 +170,7 @@ public void toStringMatches_noDeadline_default() {
assertThat(actual).contains("waitForReady=true");
assertThat(actual).contains("maxInboundMessageSize=44");
assertThat(actual).contains("maxOutboundMessageSize=55");
assertThat(actual).contains("onReadyThreshold=1024");
assertThat(actual).contains("streamTracerFactories=[tracerFactory1, tracerFactory2]");
}

Expand Down
10 changes: 10 additions & 0 deletions stub/src/main/java/io/grpc/stub/AbstractStub.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,16 @@ public final S withMaxOutboundMessageSize(int maxSize) {
return build(channel, callOptions.withMaxOutboundMessageSize(maxSize));
}

/**
* Returns a new stub that limits the maximum number of bytes per stream in the queue.
*
* @since 1.1.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/11021")
public final S withOnReadyThreshold(int numBytes) {
return build(channel, callOptions.withOnReadyThreshold(numBytes));
}

/**
* A factory class for stub.
*
Expand Down
13 changes: 13 additions & 0 deletions stub/src/test/java/io/grpc/stub/BaseAbstractStubTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.grpc.stub;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -90,4 +91,16 @@ public void withExecutor() {

assertEquals(callOptions.getExecutor(), executor);
}

@Test
public void withOnReadyThreshold() {
T stub = create(channel);
CallOptions callOptions = stub.getCallOptions();
assertNull(callOptions.getOnReadyThreshold());

int onReadyThreshold = 1024;
stub = stub.withOnReadyThreshold(onReadyThreshold);
callOptions = stub.getCallOptions();
assertThat(callOptions.getOnReadyThreshold()).isEqualTo(onReadyThreshold);
}
}

0 comments on commit 25a8b7c

Please sign in to comment.