Skip to content

Commit

Permalink
google-java-format binder/src/*/*.java (grpc#11297)
Browse files Browse the repository at this point in the history
https://github.com/grpc/grpc-java/blob/master/CONTRIBUTING.md#code-style

We've been talking about an incremental approach in grpc#1664 but google/google-java-format#1114 causes a bunch of manual formatting toil every time I run ~/google-java-format-diff.py because we have a LOT of lines >100 characters. Establishing a gjf-clean baseline will make things a lot easier for me.
  • Loading branch information
jdcormie authored and larry-safran committed Aug 13, 2024
1 parent 7563fe3 commit 611658b
Show file tree
Hide file tree
Showing 46 changed files with 523 additions and 593 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public final class BinderChannelSmokeTest {
private static final int SLIGHTLY_MORE_THAN_ONE_BLOCK = 16 * 1024 + 100;
private static final String MSG = "Some text which will be repeated many many times";
private static final String SERVER_TARGET_URI = "fake://server";
private static final Metadata.Key<PoisonParcelable> POISON_KEY = ParcelableUtils.metadataKey(
"poison-bin", PoisonParcelable.CREATOR);
private static final Metadata.Key<PoisonParcelable> POISON_KEY =
ParcelableUtils.metadataKey("poison-bin", PoisonParcelable.CREATOR);

final MethodDescriptor<String, String> method =
MethodDescriptor.newBuilder(StringMarshaller.INSTANCE, StringMarshaller.INSTANCE)
Expand Down Expand Up @@ -140,22 +140,27 @@ public void setUp() throws Exception {
AndroidComponentAddress serverAddress = HostServices.allocateService(appContext);
fakeNameResolverProvider = new FakeNameResolverProvider(SERVER_TARGET_URI, serverAddress);
NameResolverRegistry.getDefaultRegistry().register(fakeNameResolverProvider);
HostServices.configureService(serverAddress,
HostServices.configureService(
serverAddress,
HostServices.serviceParamsBuilder()
.setServerFactory((service, receiver) ->
BinderServerBuilder.forAddress(serverAddress, receiver)
.inboundParcelablePolicy(InboundParcelablePolicy.newBuilder()
.setAcceptParcelableMetadataValues(true)
.setServerFactory(
(service, receiver) ->
BinderServerBuilder.forAddress(serverAddress, receiver)
.inboundParcelablePolicy(
InboundParcelablePolicy.newBuilder()
.setAcceptParcelableMetadataValues(true)
.build())
.addService(serviceDef)
.build())
.addService(serviceDef)
.build())
.build());

channel = BinderChannelBuilder.forAddress(serverAddress, appContext)
.inboundParcelablePolicy(InboundParcelablePolicy.newBuilder()
.setAcceptParcelableMetadataValues(true)
.build())
.build();
channel =
BinderChannelBuilder.forAddress(serverAddress, appContext)
.inboundParcelablePolicy(
InboundParcelablePolicy.newBuilder()
.setAcceptParcelableMetadataValues(true)
.build())
.build();
}

@After
Expand Down Expand Up @@ -253,8 +258,8 @@ public void testUncaughtServerException() throws Exception {
Metadata extraHeadersToSend = new Metadata();
extraHeadersToSend.put(POISON_KEY, bad);
Channel interceptedChannel =
ClientInterceptors.intercept(channel,
MetadataUtils.newAttachHeadersInterceptor(extraHeadersToSend));
ClientInterceptors.intercept(
channel, MetadataUtils.newAttachHeadersInterceptor(extraHeadersToSend));
CallOptions callOptions = CallOptions.DEFAULT.withDeadlineAfter(5, SECONDS);
try {
ClientCalls.blockingUnaryCall(interceptedChannel, method, callOptions, "hello");
Expand Down Expand Up @@ -361,41 +366,43 @@ public void onCompleted() {

class AddParcelableServerInterceptor implements ServerInterceptor {
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
Metadata headers, ServerCallHandler<ReqT, RespT> next) {
return next.startCall(new SimpleForwardingServerCall<ReqT, RespT>(call) {
@Override
public void sendHeaders(Metadata headers) {
if (parcelableForResponseHeaders != null) {
headers.put(POISON_KEY, parcelableForResponseHeaders);
}
super.sendHeaders(headers);
}
}, headers);
public <ReqT, RespT> Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
return next.startCall(
new SimpleForwardingServerCall<ReqT, RespT>(call) {
@Override
public void sendHeaders(Metadata headers) {
if (parcelableForResponseHeaders != null) {
headers.put(POISON_KEY, parcelableForResponseHeaders);
}
super.sendHeaders(headers);
}
},
headers);
}
}

static class PoisonParcelable implements Parcelable {

public static final Creator<PoisonParcelable> CREATOR = new Parcelable.Creator<PoisonParcelable>() {
@Override
public PoisonParcelable createFromParcel(Parcel parcel) {
throw new RuntimeException("ouch");
}
public static final Creator<PoisonParcelable> CREATOR =
new Parcelable.Creator<PoisonParcelable>() {
@Override
public PoisonParcelable createFromParcel(Parcel parcel) {
throw new RuntimeException("ouch");
}

@Override
public PoisonParcelable[] newArray(int n) {
return new PoisonParcelable[n];
}
};
@Override
public PoisonParcelable[] newArray(int n) {
return new PoisonParcelable[n];
}
};

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel parcel, int flags) {
}
public void writeToParcel(Parcel parcel, int flags) {}
}
}
91 changes: 46 additions & 45 deletions binder/src/androidTest/java/io/grpc/binder/BinderSecurityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import javax.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -111,15 +110,14 @@ private void createChannel() throws Exception {
private void createChannel(ServerSecurityPolicy serverPolicy, SecurityPolicy channelPolicy)
throws Exception {
AndroidComponentAddress addr = HostServices.allocateService(appContext);
HostServices.configureService(addr,
HostServices.configureService(
addr,
HostServices.serviceParamsBuilder()
.setServerFactory((service, receiver) -> buildServer(addr, receiver, serverPolicy))
.build());
.setServerFactory((service, receiver) -> buildServer(addr, receiver, serverPolicy))
.build());

channel =
BinderChannelBuilder.forAddress(addr, appContext)
.securityPolicy(channelPolicy)
.build();
BinderChannelBuilder.forAddress(addr, appContext).securityPolicy(channelPolicy).build();
}

private Server buildServer(
Expand Down Expand Up @@ -149,7 +147,7 @@ private StatusRuntimeException assertCallFailure(
try {
ClientCalls.blockingUnaryCall(channel, method, CallOptions.DEFAULT, null);
fail("Expected call to " + method.getFullMethodName() + " to fail but it succeeded.");
throw new AssertionError(); // impossible
throw new AssertionError(); // impossible
} catch (StatusRuntimeException sre) {
assertThat(sre.getStatus().getCode()).isEqualTo(status.getCode());
return sre;
Expand Down Expand Up @@ -186,12 +184,14 @@ public void testFailedFuturesPropagateOriginalException() throws Exception {
IllegalStateException originalException = new IllegalStateException(errorMessage);
createChannel(
ServerSecurityPolicy.newBuilder()
.servicePolicy("foo", new AsyncSecurityPolicy() {
@Override
public ListenableFuture<Status> checkAuthorizationAsync(int uid) {
return Futures.immediateFailedFuture(originalException);
}
})
.servicePolicy(
"foo",
new AsyncSecurityPolicy() {
@Override
public ListenableFuture<Status> checkAuthorizationAsync(int uid) {
return Futures.immediateFailedFuture(originalException);
}
})
.build(),
SecurityPolicies.internalOnly());
MethodDescriptor<Empty, Empty> method = methods.get("foo/method0");
Expand All @@ -205,15 +205,17 @@ public void testFailedFuturesAreNotCachedPermanently() throws Exception {
AtomicReference<Boolean> firstAttempt = new AtomicReference<>(true);
createChannel(
ServerSecurityPolicy.newBuilder()
.servicePolicy("foo", new AsyncSecurityPolicy() {
@Override
public ListenableFuture<Status> checkAuthorizationAsync(int uid) {
if (firstAttempt.getAndSet(false)) {
return Futures.immediateFailedFuture(new IllegalStateException());
}
return Futures.immediateFuture(Status.OK);
}
})
.servicePolicy(
"foo",
new AsyncSecurityPolicy() {
@Override
public ListenableFuture<Status> checkAuthorizationAsync(int uid) {
if (firstAttempt.getAndSet(false)) {
return Futures.immediateFailedFuture(new IllegalStateException());
}
return Futures.immediateFuture(Status.OK);
}
})
.build(),
SecurityPolicies.internalOnly());
MethodDescriptor<Empty, Empty> method = methods.get("foo/method0");
Expand All @@ -227,15 +229,17 @@ public void testCancelledFuturesAreNotCachedPermanently() throws Exception {
AtomicReference<Boolean> firstAttempt = new AtomicReference<>(true);
createChannel(
ServerSecurityPolicy.newBuilder()
.servicePolicy("foo", new AsyncSecurityPolicy() {
@Override
public ListenableFuture<Status> checkAuthorizationAsync(int uid) {
if (firstAttempt.getAndSet(false)) {
return Futures.immediateCancelledFuture();
}
return Futures.immediateFuture(Status.OK);
}
})
.servicePolicy(
"foo",
new AsyncSecurityPolicy() {
@Override
public ListenableFuture<Status> checkAuthorizationAsync(int uid) {
if (firstAttempt.getAndSet(false)) {
return Futures.immediateCancelledFuture();
}
return Futures.immediateFuture(Status.OK);
}
})
.build(),
SecurityPolicies.internalOnly());
MethodDescriptor<Empty, Empty> method = methods.get("foo/method0");
Expand Down Expand Up @@ -275,11 +279,11 @@ public void testPerServicePolicy() throws Exception {
@Test
public void testPerServicePolicyAsync() throws Exception {
createChannel(
ServerSecurityPolicy.newBuilder()
.servicePolicy("foo", asyncPolicy((uid) -> Futures.immediateFuture(true)))
.servicePolicy("bar", asyncPolicy((uid) -> Futures.immediateFuture(false)))
.build(),
SecurityPolicies.internalOnly());
ServerSecurityPolicy.newBuilder()
.servicePolicy("foo", asyncPolicy((uid) -> Futures.immediateFuture(true)))
.servicePolicy("bar", asyncPolicy((uid) -> Futures.immediateFuture(false)))
.build(),
SecurityPolicies.internalOnly());

assertThat(methods).isNotEmpty();
for (MethodDescriptor<Empty, Empty> method : methods.values()) {
Expand Down Expand Up @@ -326,11 +330,10 @@ private static AsyncSecurityPolicy asyncPolicy(
return new AsyncSecurityPolicy() {
@Override
public ListenableFuture<Status> checkAuthorizationAsync(int uid) {
return Futures
.transform(
func.apply(uid),
allowed -> allowed ? Status.OK : Status.PERMISSION_DENIED,
MoreExecutors.directExecutor());
return Futures.transform(
func.apply(uid),
allowed -> allowed ? Status.OK : Status.PERMISSION_DENIED,
MoreExecutors.directExecutor());
}
};
}
Expand All @@ -340,9 +343,7 @@ private final class CountingServerInterceptor implements ServerInterceptor {

@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> call,
Metadata headers,
ServerCallHandler<ReqT, RespT> next) {
ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
numInterceptedCalls += 1;
return next.startCall(call, headers);
}
Expand Down
10 changes: 0 additions & 10 deletions binder/src/androidTest/java/io/grpc/binder/HostServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.grpc.binder;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static java.util.concurrent.TimeUnit.SECONDS;

Expand All @@ -30,20 +29,12 @@
import androidx.lifecycle.LifecycleService;
import com.google.auto.value.AutoValue;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import io.grpc.NameResolver;
import io.grpc.Server;
import io.grpc.ServerServiceDefinition;
import io.grpc.ServerStreamTracer;
import io.grpc.binder.AndroidComponentAddress;
import io.grpc.internal.InternalServer;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
Expand All @@ -64,7 +55,6 @@ public final class HostServices {
HostService1.class, HostService2.class,
};


public interface ServerFactory {
Server createServer(Service service, IBinderReceiver receiver);
}
Expand Down
Loading

0 comments on commit 611658b

Please sign in to comment.