Skip to content

Commit

Permalink
cronet: Update to Java-8 API's and tighten the scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashok-Varma authored and temawi committed Apr 22, 2024
1 parent c703a1e commit 163efa3
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 37 deletions.
12 changes: 6 additions & 6 deletions cronet/src/main/java/io/grpc/cronet/CronetChannelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static CronetChannelBuilder forAddress(String name, int port) {

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;

Expand Down Expand Up @@ -132,7 +132,7 @@ protected ManagedChannelBuilder<?> delegate() {
* 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;
Expand All @@ -141,7 +141,7 @@ public final CronetChannelBuilder maxMessageSize(int maxMessageSize) {
/**
* 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;
}
Expand All @@ -163,7 +163,7 @@ public final CronetChannelBuilder alwaysUsePut(boolean enable) {
* application.
* @return the builder to facilitate chaining.
*/
final CronetChannelBuilder setTrafficStatsTag(int tag) {
CronetChannelBuilder setTrafficStatsTag(int tag) {
trafficStatsTagSet = true;
trafficStatsTag = tag;
return this;
Expand All @@ -184,7 +184,7 @@ final CronetChannelBuilder setTrafficStatsTag(int tag) {
* @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;
Expand All @@ -200,7 +200,7 @@ final CronetChannelBuilder setTrafficStatsUid(int uid) {
*
* @since 1.12.0
*/
public final CronetChannelBuilder scheduledExecutorService(
public CronetChannelBuilder scheduledExecutorService(
ScheduledExecutorService scheduledExecutorService) {
this.scheduledExecutorService =
checkNotNull(scheduledExecutorService, "scheduledExecutorService");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public void cancel(Status reason) {
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")
Expand Down
10 changes: 5 additions & 5 deletions cronet/src/main/java/io/grpc/cronet/CronetClientTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ 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;
private final TransportTracer transportTracer;
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")
Expand All @@ -79,7 +80,6 @@ class CronetClientTransport implements ConnectionClientTransport {
@GuardedBy("lock")
// Whether this transport has started.
private boolean started;
private StreamBuilderFactory streamFactory;

CronetClientTransport(
StreamBuilderFactory streamFactory,
Expand Down Expand Up @@ -205,9 +205,9 @@ public void shutdownNow(Status status) {
// 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();
}
Expand Down Expand Up @@ -255,7 +255,7 @@ public InternalLogId getLogId() {
*/
void stopIfNecessary() {
synchronized (lock) {
if (goAway && !stopped && streams.size() == 0) {
if (goAway && !stopped && streams.isEmpty()) {
stopped = true;
} else {
return;
Expand Down
25 changes: 6 additions & 19 deletions cronet/src/test/java/io/grpc/cronet/CronetClientStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -171,7 +166,7 @@ public void write() {
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.
Expand Down Expand Up @@ -206,27 +201,19 @@ public void write() {
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 163efa3

Please sign in to comment.