Skip to content

Commit c1d7035

Browse files
authored
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.
1 parent 57af63a commit c1d7035

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

okhttp/src/main/java/io/grpc/okhttp/OkHttpWritableBufferAllocator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import io.grpc.internal.WritableBuffer;
2020
import io.grpc.internal.WritableBufferAllocator;
2121
import okio.Buffer;
22-
import okio.Segment;
2322

2423
/**
2524
* The default allocator for {@link OkHttpWritableBuffer}s used by the OkHttp transport. OkHttp
@@ -30,6 +29,7 @@ class OkHttpWritableBufferAllocator implements WritableBufferAllocator {
3029

3130
// Set the maximum buffer size to 1MB
3231
private static final int MAX_BUFFER = 1024 * 1024;
32+
public static final int SEGMENT_SIZE_COPY = 8192; // Should equal Segment.SIZE
3333

3434
/**
3535
* Construct a new instance.
@@ -45,7 +45,7 @@ class OkHttpWritableBufferAllocator implements WritableBufferAllocator {
4545
public WritableBuffer allocate(int capacityHint) {
4646
// okio buffer uses fixed size Segments, round capacityHint up
4747
capacityHint = Math.min(MAX_BUFFER,
48-
(capacityHint + Segment.SIZE - 1) / Segment.SIZE * Segment.SIZE);
48+
(capacityHint + SEGMENT_SIZE_COPY - 1) / SEGMENT_SIZE_COPY * SEGMENT_SIZE_COPY);
4949
return new OkHttpWritableBuffer(new Buffer(), capacityHint);
5050
}
5151
}

okhttp/src/test/java/io/grpc/okhttp/OkHttpWritableBufferAllocatorTest.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.grpc.okhttp;
1818

19+
import static io.grpc.okhttp.OkHttpWritableBufferAllocator.SEGMENT_SIZE_COPY;
1920
import static org.junit.Assert.assertEquals;
2021

2122
import io.grpc.internal.WritableBuffer;
@@ -39,11 +40,12 @@ protected WritableBufferAllocator allocator() {
3940
return allocator;
4041
}
4142

43+
@SuppressWarnings("KotlinInternal")
4244
@Test
4345
public void testCapacity() {
4446
WritableBuffer buffer = allocator().allocate(4096);
4547
assertEquals(0, buffer.readableBytes());
46-
assertEquals(Segment.SIZE, buffer.writableBytes());
48+
assertEquals(SEGMENT_SIZE_COPY, buffer.writableBytes());
4749
}
4850

4951
@Test
@@ -55,8 +57,14 @@ public void testInitialCapacityHasMaximum() {
5557

5658
@Test
5759
public void testIsExactBelowMaxCapacity() {
58-
WritableBuffer buffer = allocator().allocate(Segment.SIZE + 1);
60+
WritableBuffer buffer = allocator().allocate(SEGMENT_SIZE_COPY + 1);
5961
assertEquals(0, buffer.readableBytes());
60-
assertEquals(Segment.SIZE * 2, buffer.writableBytes());
62+
assertEquals(SEGMENT_SIZE_COPY * 2, buffer.writableBytes());
63+
}
64+
65+
@SuppressWarnings("KotlinInternal")
66+
@Test
67+
public void testSegmentSizeMatchesKotlin() {
68+
assertEquals(Segment.SIZE, SEGMENT_SIZE_COPY);
6169
}
6270
}

0 commit comments

Comments
 (0)