From c1d703546a8ed4a052769aa78203f44658a3083f Mon Sep 17 00:00:00 2001 From: Larry Safran Date: Fri, 14 Feb 2025 14:46:54 -0800 Subject: [PATCH] okhttp:Use a locally specified value instead of Segment.SIZE in okhttp (#11899) Switched to using 8192 which is the current value of Segment.SIZE and just have a test check that they are equal. The reason for doing this is that Segment.SIZE is Kotlin internal so shouldn't be used outside of its module. --- .../grpc/okhttp/OkHttpWritableBufferAllocator.java | 4 ++-- .../okhttp/OkHttpWritableBufferAllocatorTest.java | 14 +++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/okhttp/src/main/java/io/grpc/okhttp/OkHttpWritableBufferAllocator.java b/okhttp/src/main/java/io/grpc/okhttp/OkHttpWritableBufferAllocator.java index f5d490818ba..58896a5dbb0 100644 --- a/okhttp/src/main/java/io/grpc/okhttp/OkHttpWritableBufferAllocator.java +++ b/okhttp/src/main/java/io/grpc/okhttp/OkHttpWritableBufferAllocator.java @@ -19,7 +19,6 @@ import io.grpc.internal.WritableBuffer; import io.grpc.internal.WritableBufferAllocator; import okio.Buffer; -import okio.Segment; /** * The default allocator for {@link OkHttpWritableBuffer}s used by the OkHttp transport. OkHttp @@ -30,6 +29,7 @@ class OkHttpWritableBufferAllocator implements WritableBufferAllocator { // Set the maximum buffer size to 1MB private static final int MAX_BUFFER = 1024 * 1024; + public static final int SEGMENT_SIZE_COPY = 8192; // Should equal Segment.SIZE /** * Construct a new instance. @@ -45,7 +45,7 @@ class OkHttpWritableBufferAllocator implements WritableBufferAllocator { public WritableBuffer allocate(int capacityHint) { // okio buffer uses fixed size Segments, round capacityHint up capacityHint = Math.min(MAX_BUFFER, - (capacityHint + Segment.SIZE - 1) / Segment.SIZE * Segment.SIZE); + (capacityHint + SEGMENT_SIZE_COPY - 1) / SEGMENT_SIZE_COPY * SEGMENT_SIZE_COPY); return new OkHttpWritableBuffer(new Buffer(), capacityHint); } } diff --git a/okhttp/src/test/java/io/grpc/okhttp/OkHttpWritableBufferAllocatorTest.java b/okhttp/src/test/java/io/grpc/okhttp/OkHttpWritableBufferAllocatorTest.java index c444e0ee11d..c19224822a8 100644 --- a/okhttp/src/test/java/io/grpc/okhttp/OkHttpWritableBufferAllocatorTest.java +++ b/okhttp/src/test/java/io/grpc/okhttp/OkHttpWritableBufferAllocatorTest.java @@ -16,6 +16,7 @@ package io.grpc.okhttp; +import static io.grpc.okhttp.OkHttpWritableBufferAllocator.SEGMENT_SIZE_COPY; import static org.junit.Assert.assertEquals; import io.grpc.internal.WritableBuffer; @@ -39,11 +40,12 @@ protected WritableBufferAllocator allocator() { return allocator; } + @SuppressWarnings("KotlinInternal") @Test public void testCapacity() { WritableBuffer buffer = allocator().allocate(4096); assertEquals(0, buffer.readableBytes()); - assertEquals(Segment.SIZE, buffer.writableBytes()); + assertEquals(SEGMENT_SIZE_COPY, buffer.writableBytes()); } @Test @@ -55,8 +57,14 @@ public void testInitialCapacityHasMaximum() { @Test public void testIsExactBelowMaxCapacity() { - WritableBuffer buffer = allocator().allocate(Segment.SIZE + 1); + WritableBuffer buffer = allocator().allocate(SEGMENT_SIZE_COPY + 1); assertEquals(0, buffer.readableBytes()); - assertEquals(Segment.SIZE * 2, buffer.writableBytes()); + assertEquals(SEGMENT_SIZE_COPY * 2, buffer.writableBytes()); + } + + @SuppressWarnings("KotlinInternal") + @Test + public void testSegmentSizeMatchesKotlin() { + assertEquals(Segment.SIZE, SEGMENT_SIZE_COPY); } }