forked from grpc/grpc-java
/
UdsNettyChannelProviderTest.java
195 lines (172 loc) · 6.49 KB
/
UdsNettyChannelProviderTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright 2015 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.grpc.netty;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import io.grpc.Grpc;
import io.grpc.InsecureChannelCredentials;
import io.grpc.InternalServiceProviders;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.ManagedChannelProvider;
import io.grpc.ManagedChannelProvider.NewChannelBuilderResult;
import io.grpc.ManagedChannelRegistryAccessor;
import io.grpc.TlsChannelCredentials;
import io.grpc.stub.StreamObserver;
import io.grpc.testing.GrpcCleanupRule;
import io.grpc.testing.protobuf.SimpleRequest;
import io.grpc.testing.protobuf.SimpleResponse;
import io.grpc.testing.protobuf.SimpleServiceGrpc;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerDomainSocketChannel;
import io.netty.channel.unix.DomainSocketAddress;
import java.io.IOException;
import org.junit.After;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link UdsNettyChannelProvider}. */
@RunWith(JUnit4.class)
public class UdsNettyChannelProviderTest {
@Rule public TemporaryFolder tempFolder = new TemporaryFolder();
private static final String TEST_SOCKET = "/test.socket";
@Rule
public final GrpcCleanupRule cleanupRule = new GrpcCleanupRule();
private UdsNettyChannelProvider provider = new UdsNettyChannelProvider();
private EventLoopGroup elg;
private EventLoopGroup boss;
@After
public void tearDown() {
if (elg != null) {
elg.shutdownGracefully();
}
if (boss != null) {
boss.shutdownGracefully();
}
}
@Test
public void provided() {
for (ManagedChannelProvider current
: InternalServiceProviders.getCandidatesViaServiceLoader(
ManagedChannelProvider.class, getClass().getClassLoader())) {
if (current instanceof UdsNettyChannelProvider) {
return;
}
}
fail("ServiceLoader unable to load UdsNettyChannelProvider");
}
@Test
public void providedHardCoded() {
for (Class<?> current : ManagedChannelRegistryAccessor.getHardCodedClasses()) {
if (current == UdsNettyChannelProvider.class) {
return;
}
}
fail("Hard coded unable to load UdsNettyChannelProvider");
}
@Test
public void basicMethods() {
assertTrue(provider.isAvailable());
assertEquals(3, provider.priority());
}
@Test
public void builderForTarget() {
assertThat(provider.builderForTarget("unix:sock.sock")).isInstanceOf(NettyChannelBuilder.class);
}
@Test
public void builderForTarget_badScheme() {
try {
provider.builderForTarget("dns:sock.sock");
fail("exception expected");
} catch (IllegalArgumentException e) {
assertThat(e).hasMessageThat().isEqualTo("scheme must be unix");
}
}
@Test
public void newChannelBuilder_success() {
NewChannelBuilderResult result =
provider.newChannelBuilder("unix:sock.sock", TlsChannelCredentials.create());
assertThat(result.getChannelBuilder()).isInstanceOf(NettyChannelBuilder.class);
}
@Test
public void newChannelBuilder_badScheme() {
try {
provider.newChannelBuilder("dns:sock.sock", InsecureChannelCredentials.create());
fail("exception expected");
} catch (IllegalArgumentException e) {
assertThat(e).hasMessageThat().isEqualTo("scheme must be unix");
}
}
@Test
public void managedChannelRegistry_newChannelBuilder() {
Assume.assumeTrue(Utils.isEpollAvailable());
ManagedChannelBuilder<?> managedChannelBuilder
= Grpc.newChannelBuilder("unix:///sock.sock", InsecureChannelCredentials.create());
assertThat(managedChannelBuilder).isNotNull();
ManagedChannel channel = managedChannelBuilder.build();
assertThat(channel).isNotNull();
assertThat(channel.authority()).isEqualTo("/sock.sock");
channel.shutdownNow();
}
@Test
public void udsClientServerTestUsingProvider() throws IOException {
Assume.assumeTrue(Utils.isEpollAvailable());
String socketPath = tempFolder.getRoot().getAbsolutePath() + TEST_SOCKET;
createUdsServer(socketPath);
ManagedChannelBuilder<?> channelBuilder =
Grpc.newChannelBuilder("unix://" + socketPath, InsecureChannelCredentials.create());
SimpleServiceGrpc.SimpleServiceBlockingStub stub =
SimpleServiceGrpc.newBlockingStub(cleanupRule.register(channelBuilder.build()));
assertThat(unaryRpc("buddy", stub)).isEqualTo("Hello buddy");
}
/** Say hello to server. */
private static String unaryRpc(
String requestMessage, SimpleServiceGrpc.SimpleServiceBlockingStub blockingStub) {
SimpleRequest request = SimpleRequest.newBuilder().setRequestMessage(requestMessage).build();
SimpleResponse response = blockingStub.unaryRpc(request);
return response.getResponseMessage();
}
private void createUdsServer(String name) throws IOException {
elg = new EpollEventLoopGroup();
boss = new EpollEventLoopGroup(1);
cleanupRule.register(
NettyServerBuilder.forAddress(new DomainSocketAddress(name))
.bossEventLoopGroup(boss)
.workerEventLoopGroup(elg)
.channelType(EpollServerDomainSocketChannel.class)
.addService(new SimpleServiceImpl())
.directExecutor()
.build()
.start());
}
private static class SimpleServiceImpl extends SimpleServiceGrpc.SimpleServiceImplBase {
@Override
public void unaryRpc(SimpleRequest req, StreamObserver<SimpleResponse> responseObserver) {
SimpleResponse response =
SimpleResponse.newBuilder()
.setResponseMessage("Hello " + req.getRequestMessage())
.build();
responseObserver.onNext(response);
responseObserver.onCompleted();
}
}
}