Skip to content

Commit a15421b

Browse files
committed
delete HandshakerServiceChannel.
1 parent 927d215 commit a15421b

2 files changed

Lines changed: 17 additions & 179 deletions

File tree

s2a/src/main/java/io/grpc/s2a/internal/channel/S2AHandshakerServiceChannel.java

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@
1919
import static com.google.common.base.Preconditions.checkNotNull;
2020
import static java.util.concurrent.TimeUnit.SECONDS;
2121

22-
import com.google.common.annotations.VisibleForTesting;
23-
import io.grpc.CallOptions;
2422
import io.grpc.Channel;
2523
import io.grpc.ChannelCredentials;
26-
import io.grpc.ClientCall;
2724
import io.grpc.ManagedChannel;
28-
import io.grpc.MethodDescriptor;
2925
import io.grpc.internal.SharedResourceHolder.Resource;
3026
import io.grpc.netty.NettyChannelBuilder;
3127
import java.time.Duration;
@@ -55,6 +51,8 @@
5551
@ThreadSafe
5652
public final class S2AHandshakerServiceChannel {
5753
private static final Duration CHANNEL_SHUTDOWN_TIMEOUT = Duration.ofSeconds(10);
54+
private static final Logger logger =
55+
Logger.getLogger(S2AHandshakerServiceChannel.class.getName());
5856

5957
/**
6058
* Returns a {@link SharedResourceHolder.Resource} instance for managing channels to an S2A server
@@ -86,75 +84,34 @@ public ChannelResource(String targetAddress, ChannelCredentials channelCredentia
8684
}
8785

8886
/**
89-
* Creates a {@code HandshakerServiceChannel} instance to the service running at {@code
87+
* Creates a {@code ManagedChannel} instance to the service running at {@code
9088
* targetAddress}.
9189
*/
9290
@Override
9391
public Channel create() {
94-
ManagedChannel channel =
95-
NettyChannelBuilder.forTarget(targetAddress, channelCredentials)
92+
return NettyChannelBuilder.forTarget(targetAddress, channelCredentials)
9693
.directExecutor()
9794
.build();
98-
return HandshakerServiceChannel.create(channel);
9995
}
10096

101-
/** Destroys a {@code HandshakerServiceChannel} instance. */
97+
/** Destroys a {@code ManagedChannel} instance. */
10298
@Override
10399
public void close(Channel instanceChannel) {
104100
checkNotNull(instanceChannel);
105-
HandshakerServiceChannel channel = (HandshakerServiceChannel) instanceChannel;
106-
channel.close();
107-
}
108-
109-
@Override
110-
public String toString() {
111-
return "grpc-s2a-channel";
112-
}
113-
}
114-
115-
/**
116-
* Manages a channel using a {@link ManagedChannel} instance.
117-
*/
118-
@VisibleForTesting
119-
static class HandshakerServiceChannel extends Channel {
120-
private static final Logger logger =
121-
Logger.getLogger(S2AHandshakerServiceChannel.class.getName());
122-
private final ManagedChannel delegate;
123-
124-
static HandshakerServiceChannel create(ManagedChannel delegate) {
125-
checkNotNull(delegate);
126-
return new HandshakerServiceChannel(delegate);
127-
}
128-
129-
private HandshakerServiceChannel(ManagedChannel delegate) {
130-
this.delegate = delegate;
131-
}
132-
133-
/**
134-
* Returns the address of the service to which the {@code delegate} channel connects, which is
135-
* typically of the form {@code host:port}.
136-
*/
137-
@Override
138-
public String authority() {
139-
return delegate.authority();
140-
}
141-
142-
/** Creates a {@link ClientCall} that invokes the operations in {@link MethodDescriptor}. */
143-
@Override
144-
public <ReqT, RespT> ClientCall<ReqT, RespT> newCall(
145-
MethodDescriptor<ReqT, RespT> methodDescriptor, CallOptions options) {
146-
return delegate.newCall(methodDescriptor, options);
147-
}
148-
149-
@SuppressWarnings("FutureReturnValueIgnored")
150-
public void close() {
151-
delegate.shutdownNow();
101+
ManagedChannel channel = (ManagedChannel) instanceChannel;
102+
channel.shutdownNow();
152103
try {
153-
delegate.awaitTermination(CHANNEL_SHUTDOWN_TIMEOUT.getSeconds(), SECONDS);
104+
channel.awaitTermination(CHANNEL_SHUTDOWN_TIMEOUT.getSeconds(), SECONDS);
154105
} catch (InterruptedException e) {
155106
Thread.currentThread().interrupt();
156107
logger.log(Level.WARNING, "Channel to S2A was not shutdown.");
157108
}
109+
110+
}
111+
112+
@Override
113+
public String toString() {
114+
return "grpc-s2a-channel";
158115
}
159116
}
160117

s2a/src/test/java/io/grpc/s2a/internal/channel/S2AHandshakerServiceChannelTest.java

Lines changed: 3 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@
2020
import static com.google.common.truth.extensions.proto.ProtoTruth.assertThat;
2121
import static org.junit.Assert.assertThrows;
2222

23-
import io.grpc.CallOptions;
2423
import io.grpc.Channel;
2524
import io.grpc.ChannelCredentials;
26-
import io.grpc.ClientCall;
2725
import io.grpc.InsecureChannelCredentials;
2826
import io.grpc.ManagedChannel;
29-
import io.grpc.MethodDescriptor;
3027
import io.grpc.Server;
3128
import io.grpc.ServerBuilder;
3229
import io.grpc.ServerCredentials;
@@ -36,14 +33,12 @@
3633
import io.grpc.benchmarks.Utils;
3734
import io.grpc.internal.SharedResourceHolder.Resource;
3835
import io.grpc.netty.NettyServerBuilder;
39-
import io.grpc.s2a.internal.channel.S2AHandshakerServiceChannel.HandshakerServiceChannel;
4036
import io.grpc.stub.StreamObserver;
4137
import io.grpc.testing.GrpcCleanupRule;
4238
import io.grpc.testing.protobuf.SimpleRequest;
4339
import io.grpc.testing.protobuf.SimpleResponse;
4440
import io.grpc.testing.protobuf.SimpleServiceGrpc;
4541
import java.io.File;
46-
import java.util.concurrent.TimeUnit;
4742
import org.junit.Before;
4843
import org.junit.ClassRule;
4944
import org.junit.Test;
@@ -183,72 +178,7 @@ public void close_mtlsSuccess() throws Exception {
183178
}
184179

185180
/**
186-
* Verifies that an {@code HandshakerServiceChannel}'s {@code newCall} method can be used to
187-
* perform a simple RPC.
188-
*/
189-
@Test
190-
public void newCall_performSimpleRpcSuccess() {
191-
Resource<Channel> resource =
192-
S2AHandshakerServiceChannel.getChannelResource(
193-
"localhost:" + plaintextServer.getPort(),
194-
InsecureChannelCredentials.create());
195-
Channel channel = resource.create();
196-
assertThat(channel).isInstanceOf(HandshakerServiceChannel.class);
197-
assertThat(
198-
SimpleServiceGrpc.newBlockingStub(channel).unaryRpc(SimpleRequest.getDefaultInstance()))
199-
.isEqualToDefaultInstance();
200-
}
201-
202-
/** Same as newCall_performSimpleRpcSuccess, but use mTLS. */
203-
@Test
204-
public void newCall_mtlsPerformSimpleRpcSuccess() throws Exception {
205-
Resource<Channel> resource =
206-
S2AHandshakerServiceChannel.getChannelResource(
207-
"localhost:" + mtlsServer.getPort(), getTlsChannelCredentials());
208-
Channel channel = resource.create();
209-
assertThat(channel).isInstanceOf(HandshakerServiceChannel.class);
210-
assertThat(
211-
SimpleServiceGrpc.newBlockingStub(channel).unaryRpc(SimpleRequest.getDefaultInstance()))
212-
.isEqualToDefaultInstance();
213-
}
214-
215-
/** Creates a {@code HandshakerServiceChannel} instance and verifies its authority. */
216-
@Test
217-
public void authority_success() throws Exception {
218-
ManagedChannel channel = new FakeManagedChannel(true);
219-
HandshakerServiceChannel eventLoopHoldingChannel =
220-
HandshakerServiceChannel.create(channel);
221-
assertThat(eventLoopHoldingChannel.authority()).isEqualTo("FakeManagedChannel");
222-
}
223-
224-
/**
225-
* Creates and closes a {@code HandshakerServiceChannel} when its {@code ManagedChannel}
226-
* terminates successfully.
227-
*/
228-
@Test
229-
public void close_withDelegateTerminatedSuccess() throws Exception {
230-
ManagedChannel channel = new FakeManagedChannel(true);
231-
HandshakerServiceChannel eventLoopHoldingChannel =
232-
HandshakerServiceChannel.create(channel);
233-
eventLoopHoldingChannel.close();
234-
assertThat(channel.isShutdown()).isTrue();
235-
}
236-
237-
/**
238-
* Creates and closes a {@code HandshakerServiceChannel} when its {@code ManagedChannel} does not
239-
* terminate successfully.
240-
*/
241-
@Test
242-
public void close_withDelegateTerminatedFailure() throws Exception {
243-
ManagedChannel channel = new FakeManagedChannel(false);
244-
HandshakerServiceChannel eventLoopHoldingChannel =
245-
HandshakerServiceChannel.create(channel);
246-
eventLoopHoldingChannel.close();
247-
assertThat(channel.isShutdown()).isTrue();
248-
}
249-
250-
/**
251-
* Creates and closes a {@code HandshakerServiceChannel}, creates a new channel from the same
181+
* Creates and closes a {@code ManagedChannel}, creates a new channel from the same
252182
* resource, and verifies that this second channel is useable.
253183
*/
254184
@Test
@@ -261,7 +191,7 @@ public void create_succeedsAfterCloseIsCalledOnce() throws Exception {
261191
resource.close(channelOne);
262192

263193
Channel channelTwo = resource.create();
264-
assertThat(channelTwo).isInstanceOf(HandshakerServiceChannel.class);
194+
assertThat(channelTwo).isInstanceOf(ManagedChannel.class);
265195
assertThat(
266196
SimpleServiceGrpc.newBlockingStub(channelTwo)
267197
.unaryRpc(SimpleRequest.getDefaultInstance()))
@@ -279,7 +209,7 @@ public void create_mtlsSucceedsAfterCloseIsCalledOnce() throws Exception {
279209
resource.close(channelOne);
280210

281211
Channel channelTwo = resource.create();
282-
assertThat(channelTwo).isInstanceOf(HandshakerServiceChannel.class);
212+
assertThat(channelTwo).isInstanceOf(ManagedChannel.class);
283213
assertThat(
284214
SimpleServiceGrpc.newBlockingStub(channelTwo)
285215
.unaryRpc(SimpleRequest.getDefaultInstance()))
@@ -325,53 +255,4 @@ public void unaryRpc(SimpleRequest request, StreamObserver<SimpleResponse> strea
325255
streamObserver.onCompleted();
326256
}
327257
}
328-
329-
private static class FakeManagedChannel extends ManagedChannel {
330-
private final boolean isDelegateTerminatedSuccess;
331-
private boolean isShutdown = false;
332-
333-
FakeManagedChannel(boolean isDelegateTerminatedSuccess) {
334-
this.isDelegateTerminatedSuccess = isDelegateTerminatedSuccess;
335-
}
336-
337-
@Override
338-
public String authority() {
339-
return "FakeManagedChannel";
340-
}
341-
342-
@Override
343-
public <ReqT, RespT> ClientCall<ReqT, RespT> newCall(
344-
MethodDescriptor<ReqT, RespT> methodDescriptor, CallOptions options) {
345-
throw new UnsupportedOperationException("This method should not be called.");
346-
}
347-
348-
@Override
349-
public ManagedChannel shutdown() {
350-
throw new UnsupportedOperationException("This method should not be called.");
351-
}
352-
353-
@Override
354-
public boolean isShutdown() {
355-
return isShutdown;
356-
}
357-
358-
@Override
359-
public boolean isTerminated() {
360-
throw new UnsupportedOperationException("This method should not be called.");
361-
}
362-
363-
@Override
364-
public ManagedChannel shutdownNow() {
365-
isShutdown = true;
366-
return null;
367-
}
368-
369-
@Override
370-
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
371-
if (isDelegateTerminatedSuccess) {
372-
return true;
373-
}
374-
throw new InterruptedException("Await termination was interrupted.");
375-
}
376-
}
377258
}

0 commit comments

Comments
 (0)