Skip to content

Commit

Permalink
stub: add withExecutor API
Browse files Browse the repository at this point in the history
  • Loading branch information
dapengzhang0 authored Oct 24, 2017
1 parent fedef8f commit c90f27f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions stub/src/main/java/io/grpc/stub/AbstractStub.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.grpc.Deadline;
import io.grpc.ExperimentalApi;
import io.grpc.ManagedChannelBuilder;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;
Expand Down Expand Up @@ -120,6 +121,18 @@ public final S withDeadlineAfter(long duration, TimeUnit unit) {
return build(channel, callOptions.withDeadlineAfter(duration, unit));
}

/**
* Returns a new stub with the given executor that is to be used instead of the default one
* specified with {@link ManagedChannelBuilder#executor}. Note that setting this option may not
* take effect for blocking calls.
*
* @since 1.8.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/3605")
public final S withExecutor(Executor executor) {
return build(channel, callOptions.withExecutor(executor));
}

/**
* Set's the compressor name to use for the call. It is the responsibility of the application
* to make sure the server supports decoding the compressor picked by the client. To be clear,
Expand Down
18 changes: 18 additions & 0 deletions stub/src/test/java/io/grpc/stub/AbstractStubTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@

package io.grpc.stub;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

import io.grpc.CallOptions;
import io.grpc.Channel;
import java.util.concurrent.Executor;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -80,4 +84,18 @@ protected NoopStub build(Channel channel, CallOptions callOptions) {
return new NoopStub(channel, callOptions);
}
}

@Test
public void withExecutor() {
NoopStub stub = new NoopStub(channel);
CallOptions callOptions = stub.getCallOptions();

assertNull(callOptions.getExecutor());

Executor executor = mock(Executor.class);
stub = stub.withExecutor(executor);
callOptions = stub.getCallOptions();

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

0 comments on commit c90f27f

Please sign in to comment.