Skip to content

Commit

Permalink
okhttp:Use a locally specified value instead of Segment.SIZE in okhttp (
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
larry-safran authored Feb 14, 2025
1 parent 57af63a commit c1d7035
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
}
}

0 comments on commit c1d7035

Please sign in to comment.