Skip to content

Commit 9a18305

Browse files
committed
Shade Netty transport, with tcnative
The entire transport is shaded because we wouldn't want to collide with the unshaded form of grpc-netty.
1 parent 182164e commit 9a18305

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed

netty/shaded/build.gradle

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
buildscript {
2+
repositories {
3+
jcenter()
4+
}
5+
dependencies {
6+
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1'
7+
}
8+
}
9+
10+
apply plugin: 'com.github.johnrengelman.shadow'
11+
12+
description = "gRPC: Netty Shaded"
13+
14+
sourceSets {
15+
testShadow {}
16+
}
17+
18+
dependencies {
19+
compile project(':grpc-netty')
20+
runtime libraries.netty_tcnative
21+
testShadowCompile files(shadowJar),
22+
configurations.shadow,
23+
project(':grpc-testing-proto'),
24+
project(':grpc-testing')
25+
shadow project(':grpc-core')
26+
}
27+
28+
artifacts {
29+
// We want uploadArchives to handle the shadowJar; we don't care about
30+
// uploadShadow
31+
archives shadowJar
32+
}
33+
34+
shadowJar {
35+
classifier = null
36+
dependencies {
37+
include(project(':grpc-netty'))
38+
include(dependency('io.netty:'))
39+
}
40+
relocate 'io.grpc.netty', 'io.grpc.netty.shaded.io.grpc.netty'
41+
relocate 'io.netty', 'io.grpc.netty.shaded.io.netty'
42+
// We have to be careful with these replacements as they must not match any
43+
// string in NativeLibraryLoader, else they cause corruption. Note that
44+
// this includes concatenation of string literals and constants.
45+
relocate 'META-INF/native/libnetty', 'META-INF/native/libio-grpc-netty-shaded-netty'
46+
relocate 'META-INF/native/netty', 'META-INF/native/io-grpc-netty-shaded-netty'
47+
mergeServiceFiles()
48+
}
49+
50+
// This is a hack to have shadow plugin modify the uploadArchives POM's
51+
// dependencies. If we delete the uploadShadow task, then the plugin will no
52+
// longer modify the POM. This probably can break horribly with parallel build,
53+
// but that's broken anyway with install/uploadArchives
54+
uploadShadow.repositories.addAll(uploadArchives.repositories)
55+
// And then we use a further hack to share that POM with install
56+
install.repositories.mavenInstaller.pom = uploadArchives.repositories.mavenDeployer.pom
57+
58+
task testShadow(type: Test) {
59+
testClassesDirs = sourceSets.testShadow.output.classesDirs
60+
classpath = sourceSets.testShadow.runtimeClasspath
61+
}
62+
compileTestShadowJava.options.compilerArgs = compileTestJava.options.compilerArgs
63+
compileTestShadowJava.options.encoding = compileTestJava.options.encoding
64+
65+
test.dependsOn testShadow
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright 2017, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.grpc.netty.shaded;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import io.grpc.ManagedChannel;
22+
import io.grpc.ManagedChannelBuilder;
23+
import io.grpc.Server;
24+
import io.grpc.ServerBuilder;
25+
import io.grpc.internal.testing.TestUtils;
26+
import io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts;
27+
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
28+
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;
29+
import io.grpc.netty.shaded.io.netty.handler.ssl.SslContextBuilder;
30+
import io.grpc.netty.shaded.io.netty.handler.ssl.SslProvider;
31+
import io.grpc.stub.StreamObserver;
32+
import io.grpc.testing.protobuf.SimpleRequest;
33+
import io.grpc.testing.protobuf.SimpleResponse;
34+
import io.grpc.testing.protobuf.SimpleServiceGrpc;
35+
import io.grpc.testing.protobuf.SimpleServiceGrpc.SimpleServiceBlockingStub;
36+
import io.grpc.testing.protobuf.SimpleServiceGrpc.SimpleServiceImplBase;
37+
import java.util.concurrent.TimeUnit;
38+
import org.junit.After;
39+
import org.junit.Test;
40+
import org.junit.runner.RunWith;
41+
import org.junit.runners.JUnit4;
42+
43+
/** Unit tests for {@link Shading}. */
44+
@RunWith(JUnit4.class)
45+
public final class ShadingTest {
46+
private ManagedChannel channel;
47+
private Server server;
48+
49+
@After
50+
public void tearDown() throws Exception {
51+
if (channel != null) {
52+
channel.shutdownNow();
53+
channel.awaitTermination(1, TimeUnit.SECONDS);
54+
}
55+
if (server != null) {
56+
server.shutdownNow();
57+
server.awaitTermination(1, TimeUnit.SECONDS);
58+
}
59+
}
60+
61+
/** Verify that normal Netty didn't leak into the test runtime. */
62+
@Test(expected = ClassNotFoundException.class)
63+
public void noNormalNetty() throws Exception {
64+
Class.forName("io.grpc.netty.NettyServerBuilder");
65+
}
66+
67+
@Test
68+
public void serviceLoaderFindsNetty() throws Exception {
69+
assertThat(ServerBuilder.forPort(0)).isInstanceOf(NettyServerBuilder.class);
70+
assertThat(ManagedChannelBuilder.forAddress("localhost", 1234))
71+
.isInstanceOf(NettyChannelBuilder.class);
72+
}
73+
74+
@Test
75+
public void basic() throws Exception {
76+
server = ServerBuilder.forPort(0)
77+
.addService(new SimpleServiceImpl())
78+
.build().start();
79+
channel = ManagedChannelBuilder
80+
.forAddress("localhost", server.getPort())
81+
.usePlaintext(true)
82+
.build();
83+
SimpleServiceBlockingStub stub = SimpleServiceGrpc.newBlockingStub(channel);
84+
assertThat(SimpleResponse.getDefaultInstance())
85+
.isEqualTo(stub.unaryRpc(SimpleRequest.getDefaultInstance()));
86+
}
87+
88+
@Test
89+
public void tcnative() throws Exception {
90+
server = NettyServerBuilder.forPort(0)
91+
.useTransportSecurity(TestUtils.loadCert("server1.pem"), TestUtils.loadCert("server1.key"))
92+
.addService(new SimpleServiceImpl())
93+
.build().start();
94+
channel = NettyChannelBuilder
95+
.forAddress("localhost", server.getPort())
96+
.sslContext(
97+
GrpcSslContexts.configure(SslContextBuilder.forClient(), SslProvider.OPENSSL)
98+
.trustManager(TestUtils.loadCert("ca.pem")).build())
99+
.overrideAuthority("foo.test.google.fr")
100+
.build();
101+
SimpleServiceBlockingStub stub = SimpleServiceGrpc.newBlockingStub(channel);
102+
assertThat(SimpleResponse.getDefaultInstance())
103+
.isEqualTo(stub.unaryRpc(SimpleRequest.getDefaultInstance()));
104+
}
105+
106+
private static class SimpleServiceImpl extends SimpleServiceImplBase {
107+
@Override public void unaryRpc(SimpleRequest req, StreamObserver<SimpleResponse> obs) {
108+
obs.onNext(SimpleResponse.getDefaultInstance());
109+
obs.onCompleted();
110+
}
111+
}
112+
}

settings.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ include ":grpc-protobuf"
88
include ":grpc-protobuf-lite"
99
include ":grpc-protobuf-nano"
1010
include ":grpc-netty"
11+
include ":grpc-netty-shaded"
1112
include ":grpc-grpclb"
1213
include ":grpc-testing"
1314
include ":grpc-testing-proto"
@@ -26,6 +27,7 @@ project(':grpc-protobuf').projectDir = "$rootDir/protobuf" as File
2627
project(':grpc-protobuf-lite').projectDir = "$rootDir/protobuf-lite" as File
2728
project(':grpc-protobuf-nano').projectDir = "$rootDir/protobuf-nano" as File
2829
project(':grpc-netty').projectDir = "$rootDir/netty" as File
30+
project(':grpc-netty-shaded').projectDir = "$rootDir/netty/shaded" as File
2931
project(':grpc-grpclb').projectDir = "$rootDir/grpclb" as File
3032
project(':grpc-testing').projectDir = "$rootDir/testing" as File
3133
project(':grpc-testing-proto').projectDir = "$rootDir/testing-proto" as File

0 commit comments

Comments
 (0)