cronet: Update to Java-8 API's and tighten the scopes

This commit is contained in:
Ashok Varma 2024-04-07 07:09:51 -07:00 committed by Terry Wilson
parent c703a1ee07
commit 163efa3716
5 changed files with 19 additions and 37 deletions

View File

@ -84,7 +84,7 @@ public final class CronetChannelBuilder extends ForwardingChannelBuilder2<Cronet
private final CronetEngine cronetEngine;
private final ManagedChannelImplBuilder managedChannelImplBuilder;
private TransportTracer.Factory transportTracerFactory = TransportTracer.getDefaultFactory();
private final TransportTracer.Factory transportTracerFactory = TransportTracer.getDefaultFactory();
private boolean alwaysUsePut = false;
@ -132,7 +132,7 @@ public final class CronetChannelBuilder extends ForwardingChannelBuilder2<Cronet
* Sets the maximum message size allowed to be received on the channel. If not called,
* defaults to {@link io.grpc.internal.GrpcUtil#DEFAULT_MAX_MESSAGE_SIZE}.
*/
public final CronetChannelBuilder maxMessageSize(int maxMessageSize) {
public CronetChannelBuilder maxMessageSize(int maxMessageSize) {
checkArgument(maxMessageSize >= 0, "maxMessageSize must be >= 0");
this.maxMessageSize = maxMessageSize;
return this;
@ -141,7 +141,7 @@ public final class CronetChannelBuilder extends ForwardingChannelBuilder2<Cronet
/**
* Sets the Cronet channel to always use PUT instead of POST. Defaults to false.
*/
public final CronetChannelBuilder alwaysUsePut(boolean enable) {
public CronetChannelBuilder alwaysUsePut(boolean enable) {
this.alwaysUsePut = enable;
return this;
}
@ -163,7 +163,7 @@ public final class CronetChannelBuilder extends ForwardingChannelBuilder2<Cronet
* application.
* @return the builder to facilitate chaining.
*/
final CronetChannelBuilder setTrafficStatsTag(int tag) {
CronetChannelBuilder setTrafficStatsTag(int tag) {
trafficStatsTagSet = true;
trafficStatsTag = tag;
return this;
@ -184,7 +184,7 @@ public final class CronetChannelBuilder extends ForwardingChannelBuilder2<Cronet
* @param uid the UID to attribute socket traffic caused by this channel.
* @return the builder to facilitate chaining.
*/
final CronetChannelBuilder setTrafficStatsUid(int uid) {
CronetChannelBuilder setTrafficStatsUid(int uid) {
trafficStatsUidSet = true;
trafficStatsUid = uid;
return this;
@ -200,7 +200,7 @@ public final class CronetChannelBuilder extends ForwardingChannelBuilder2<Cronet
*
* @since 1.12.0
*/
public final CronetChannelBuilder scheduledExecutorService(
public CronetChannelBuilder scheduledExecutorService(
ScheduledExecutorService scheduledExecutorService) {
this.scheduledExecutorService =
checkNotNull(scheduledExecutorService, "scheduledExecutorService");

View File

@ -247,7 +247,7 @@ class CronetClientStream extends AbstractClientStream {
class TransportState extends Http2ClientStreamTransportState {
private final Object lock;
@GuardedBy("lock")
private Collection<PendingData> pendingData = new ArrayList<PendingData>();
private final Collection<PendingData> pendingData = new ArrayList<>();
@GuardedBy("lock")
private boolean streamReady;
@GuardedBy("lock")

View File

@ -56,7 +56,7 @@ class CronetClientTransport implements ConnectionClientTransport {
private final Object lock = new Object();
@GuardedBy("lock")
private final Set<CronetClientStream> streams = Collections.newSetFromMap(
new IdentityHashMap<CronetClientStream, Boolean>());
new IdentityHashMap<>());
private final Executor executor;
private final int maxMessageSize;
private final boolean alwaysUsePut;
@ -64,6 +64,7 @@ class CronetClientTransport implements ConnectionClientTransport {
private Attributes attrs;
private final boolean useGetForSafeMethods;
private final boolean usePutForIdempotentMethods;
private final StreamBuilderFactory streamFactory;
// Indicates the transport is in go-away state: no new streams will be processed,
// but existing streams may continue.
@GuardedBy("lock")
@ -79,7 +80,6 @@ class CronetClientTransport implements ConnectionClientTransport {
@GuardedBy("lock")
// Whether this transport has started.
private boolean started;
private StreamBuilderFactory streamFactory;
CronetClientTransport(
StreamBuilderFactory streamFactory,
@ -205,9 +205,9 @@ class CronetClientTransport implements ConnectionClientTransport {
// streams.remove()
streamsCopy = new ArrayList<>(streams);
}
for (int i = 0; i < streamsCopy.size(); i++) {
for (CronetClientStream cronetClientStream : streamsCopy) {
// Avoid deadlock by calling into stream without lock held
streamsCopy.get(i).cancel(status);
cronetClientStream.cancel(status);
}
stopIfNecessary();
}
@ -255,7 +255,7 @@ class CronetClientTransport implements ConnectionClientTransport {
*/
void stopIfNecessary() {
synchronized (lock) {
if (goAway && !stopped && streams.size() == 0) {
if (goAway && !stopped && streams.isEmpty()) {
stopped = true;
} else {
return;

View File

@ -80,12 +80,7 @@ public final class CronetClientStreamTest {
@Mock private BidirectionalStream.Builder builder;
private final Object lock = new Object();
private final TransportTracer transportTracer = TransportTracer.getDefaultFactory().create();
private final Executor executor = new Executor() {
@Override
public void execute(Runnable r) {
r.run();
}
};
private final Executor executor = Runnable::run;
CronetClientStream clientStream;
private MethodDescriptor.Marshaller<Void> marshaller = TestMethodDescriptors.voidMarshaller();
@ -171,7 +166,7 @@ public final class CronetClientStreamTest {
String[] requests = new String[5];
WritableBuffer[] buffers = new WritableBuffer[5];
for (int i = 0; i < 5; ++i) {
requests[i] = new String("request" + String.valueOf(i));
requests[i] = "request" + i;
buffers[i] = allocator.allocate(requests[i].length());
buffers[i].write(requests[i].getBytes(Charset.forName("UTF-8")), 0, requests[i].length());
// The 3rd and 5th writeFrame calls have flush=true.
@ -206,27 +201,19 @@ public final class CronetClientStreamTest {
}
private static List<Map.Entry<String, String>> responseHeader(String status) {
Map<String, String> headers = new HashMap<String, String>();
Map<String, String> headers = new HashMap<>();
headers.put(":status", status);
headers.put("content-type", "application/grpc");
headers.put("test-key", "test-value");
List<Map.Entry<String, String>> headerList = new ArrayList<Map.Entry<String, String>>(3);
for (Map.Entry<String, String> entry : headers.entrySet()) {
headerList.add(entry);
}
return headerList;
return new ArrayList<>(headers.entrySet());
}
private static List<Map.Entry<String, String>> trailers(int status) {
Map<String, String> trailers = new HashMap<String, String>();
Map<String, String> trailers = new HashMap<>();
trailers.put("grpc-status", String.valueOf(status));
trailers.put("content-type", "application/grpc");
trailers.put("test-trailer-key", "test-trailer-value");
List<Map.Entry<String, String>> trailerList = new ArrayList<Map.Entry<String, String>>(3);
for (Map.Entry<String, String> entry : trailers.entrySet()) {
trailerList.add(entry);
}
return trailerList;
return new ArrayList<>(trailers.entrySet());
}
private static ByteBuffer createMessageFrame(byte[] bytes) {

View File

@ -71,12 +71,7 @@ public final class CronetClientTransportTest {
private MethodDescriptor<Void, Void> descriptor = TestMethodDescriptors.voidMethod();
@Mock private ManagedClientTransport.Listener clientTransportListener;
@Mock private BidirectionalStream.Builder builder;
private final Executor executor = new Executor() {
@Override
public void execute(Runnable r) {
r.run();
}
};
private final Executor executor = Runnable::run;
@Before
public void setUp() {