core: Move io.grpc to grpc-api

io.grpc has fewer dependencies than io.grpc.internal. Moving it to a
separate artifact lets users use the API without bringing in the deps.
If the library has an optional dependency on grpc, that can be quite
convenient.

We now version-pin both grpc-api and grpc-core, since both contain
internal APIs.

I had to change a few tests in grpc-api to avoid FakeClock. Moving
FakeClock to grpc-api was difficult because it uses
io.grpc.internal.TimeProvider, which can't be moved since it is a
production class. Having grpc-api's tests depend on grpc-core's test
classes would be weird and cause a circular dependincy. Having
grpc-api's tests depend on grpc-core is likely possible, but weird and
fairly unnecessary at this point. So instead I rewrote the tests to
avoid FakeClock.

Fixes #1447
This commit is contained in:
Eric Anderson 2019-04-12 17:37:13 -07:00
parent f3731eabb3
commit 80c3c992a6
157 changed files with 220 additions and 95 deletions

View File

@ -29,7 +29,7 @@ java_library(
name = "java_grpc_library_deps__do_not_reference", name = "java_grpc_library_deps__do_not_reference",
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
exports = [ exports = [
"//core", "//api",
"//protobuf", "//protobuf",
"//stub", "//stub",
"//stub:javax_annotation", "//stub:javax_annotation",
@ -43,7 +43,7 @@ java_library(
name = "java_lite_grpc_library_deps__do_not_reference", name = "java_lite_grpc_library_deps__do_not_reference",
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
exports = [ exports = [
"//core", "//api",
"//protobuf-lite", "//protobuf-lite",
"//stub", "//stub",
"//stub:javax_annotation", "//stub:javax_annotation",

View File

@ -11,6 +11,7 @@ buildscript {
} }
def subprojects = [ def subprojects = [
project(':grpc-api'),
project(':grpc-auth'), project(':grpc-auth'),
project(':grpc-core'), project(':grpc-core'),
project(':grpc-context'), project(':grpc-context'),

View File

@ -8,7 +8,7 @@ java_library(
deps = [ deps = [
":handshaker_java_grpc", ":handshaker_java_grpc",
":handshaker_java_proto", ":handshaker_java_proto",
"//core", "//api",
"//core:internal", "//core:internal",
"//netty", "//netty",
"//stub", "//stub",
@ -35,8 +35,8 @@ java_library(
deps = [ deps = [
":alts_internal", ":alts_internal",
":handshaker_java_grpc", ":handshaker_java_grpc",
"//api",
"//auth", "//auth",
"//core",
"//core:internal", "//core:internal",
"//netty", "//netty",
"@com_google_auth_google_auth_library_oauth2_http//jar", "@com_google_auth_google_auth_library_oauth2_http//jar",

14
api/BUILD.bazel Normal file
View File

@ -0,0 +1,14 @@
java_library(
name = "api",
srcs = glob([
"src/main/java/**/*.java",
]),
visibility = ["//visibility:public"],
deps = [
"//context",
"@com_google_code_findbugs_jsr305//jar",
"@com_google_guava_failureaccess//jar", # future transitive dep of Guava. See #5214
"@com_google_guava_guava//jar",
"@com_google_j2objc_j2objc_annotations//jar",
],
)

32
api/build.gradle Normal file
View File

@ -0,0 +1,32 @@
description = 'gRPC: API'
dependencies {
compile project(':grpc-context'),
libraries.errorprone,
libraries.jsr305,
libraries.animalsniffer_annotations
compile (libraries.guava) {
// prefer 2.3.2 from libraries instead of 2.1.3
exclude group: 'com.google.errorprone', module: 'error_prone_annotations'
// prefer 3.0.2 from libraries instead of 3.0.1
exclude group: 'com.google.code.findbugs', module: 'jsr305'
// prefer 1.17 from libraries instead of 1.14
exclude group: 'org.codehaus.mojo', module: 'animal-sniffer-annotations'
}
testCompile project(':grpc-context').sourceSets.test.output,
project(':grpc-testing'),
project(':grpc-grpclb'),
libraries.guava_testlib
jmh project(':grpc-core')
signature "org.codehaus.mojo.signature:java17:1.0@signature"
signature "net.sf.androidscents.signature:android-api-level-14:4.0_r4@signature"
}
javadoc {
// We want io.grpc.Internal, but not io.grpc.Internal*
exclude 'io/grpc/Internal?*.java'
exclude 'io/grpc/internal/**'
}

View File

@ -28,11 +28,13 @@ import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import io.grpc.internal.FakeClock; import com.google.common.util.concurrent.testing.TestingExecutors;
import io.grpc.internal.NoopServerCall; import io.grpc.internal.NoopServerCall;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import org.junit.Test; import org.junit.Test;
@ -213,11 +215,29 @@ public class ContextsTest {
@Test @Test
public void statusFromCancelled_TimeoutExceptionShouldMapToDeadlineExceeded() { public void statusFromCancelled_TimeoutExceptionShouldMapToDeadlineExceeded() {
FakeClock fakeClock = new FakeClock(); final long expectedDelay = 100;
final TimeUnit expectedUnit = TimeUnit.SECONDS;
class MockScheduledExecutorService extends ForwardingScheduledExecutorService {
private ScheduledExecutorService delegate = TestingExecutors.noOpScheduledExecutor();
Runnable command;
@Override public ScheduledExecutorService delegate() {
return delegate;
}
@Override public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
if (delay > unit.convert(expectedDelay, expectedUnit)) {
fail("Delay larger than expected: " + delay + " " + unit);
}
this.command = command;
return super.schedule(command, delay, unit);
}
}
MockScheduledExecutorService executorService = new MockScheduledExecutorService();
Context.CancellableContext cancellableContext = Context.current() Context.CancellableContext cancellableContext = Context.current()
.withDeadlineAfter(100, TimeUnit.NANOSECONDS, fakeClock.getScheduledExecutorService()); .withDeadlineAfter(expectedDelay, expectedUnit, executorService);
fakeClock.forwardTime(System.nanoTime(), TimeUnit.NANOSECONDS); executorService.command.run();
fakeClock.forwardNanos(100);
assertTrue(cancellableContext.isCancelled()); assertTrue(cancellableContext.isCancelled());
assertThat(cancellableContext.cancellationCause(), instanceOf(TimeoutException.class)); assertThat(cancellableContext.cancellationCause(), instanceOf(TimeoutException.class));

Some files were not shown because too many files have changed in this diff Show More