Skip to content

Commit 21e7f4c

Browse files
authored
core: make BaseWriteChannel surprise less (#2900)
Fixes #2895. The chunk size should round up, not down. Fixes #2896 by removing flush from capture. It is too surprising that capture might fail.
1 parent f2a2387 commit 21e7f4c

3 files changed

Lines changed: 18 additions & 16 deletions

File tree

google-cloud-core/src/main/java/com/google/cloud/BaseWriteChannel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ protected int getChunkSize() {
104104

105105
@Override
106106
public final void setChunkSize(int chunkSize) {
107-
chunkSize = (chunkSize / getMinChunkSize()) * getMinChunkSize();
108-
this.chunkSize = Math.max(getMinChunkSize(), chunkSize);
107+
int minSize = getMinChunkSize();
108+
109+
this.chunkSize = Math.max(minSize, (chunkSize + minSize - 1) / minSize * minSize);
109110
}
110111

111112
@InternalApi("This class should only be extended within google-cloud-java")
@@ -173,7 +174,6 @@ public final void close() throws IOException {
173174
public RestorableState<WriteChannel> capture() {
174175
byte[] bufferToSave = null;
175176
if (isOpen) {
176-
flush();
177177
bufferToSave = Arrays.copyOf(buffer, limit);
178178
}
179179
return stateBuilder()

google-cloud-core/src/main/java/com/google/cloud/WriteChannel.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@
2323
* A channel for writing data to Google Cloud services.
2424
*
2525
* <p>Implementations of this class may further buffer data internally to reduce remote calls.
26-
* Written data will only be visible after calling {@link #close()}. This interface implements
26+
* Written data might not be visible until calling {@link #close()}. This interface implements
2727
* {@link Restorable} to allow saving the writer's state to continue writing afterwards.
28-
* </p>
2928
*/
3029
public interface WriteChannel extends WritableByteChannel, Closeable, Restorable<WriteChannel> {
3130

32-
3331
/**
3432
* Sets the minimum size that will be written by a single RPC.
3533
* Written data will be buffered and only flushed upon reaching this size or closing the channel.

google-cloud-core/src/test/java/com/google/cloud/BaseWriteChannelTest.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,24 @@
1616

1717
package com.google.cloud;
1818

19+
import static com.google.common.truth.Truth.assertThat;
1920
import static junit.framework.TestCase.assertFalse;
2021
import static junit.framework.TestCase.assertTrue;
2122
import static org.junit.Assert.assertArrayEquals;
2223
import static org.junit.Assert.assertEquals;
2324
import static org.junit.Assert.assertNull;
2425

2526
import com.google.cloud.spi.ServiceRpcFactory;
26-
27-
import org.junit.Before;
28-
import org.junit.Rule;
29-
import org.junit.Test;
30-
import org.junit.rules.ExpectedException;
31-
3227
import java.io.IOException;
3328
import java.io.Serializable;
3429
import java.nio.ByteBuffer;
3530
import java.nio.channels.ClosedChannelException;
3631
import java.util.Arrays;
3732
import java.util.Random;
33+
import org.junit.Before;
34+
import org.junit.Rule;
35+
import org.junit.Test;
36+
import org.junit.rules.ExpectedException;
3837

3938
public class BaseWriteChannelTest {
4039

@@ -111,11 +110,16 @@ public void testValidateOpen() throws IOException {
111110
@Test
112111
public void testChunkSize() {
113112
channel.setChunkSize(42);
114-
assertEquals(MIN_CHUNK_SIZE, channel.getChunkSize());
113+
assertThat(channel.getChunkSize() >= MIN_CHUNK_SIZE).isTrue();
114+
assertThat(channel.getChunkSize() % MIN_CHUNK_SIZE).isEqualTo(0);
115+
115116
channel.setChunkSize(2 * MIN_CHUNK_SIZE);
116-
assertEquals(2 * MIN_CHUNK_SIZE, channel.getChunkSize());
117-
channel.setChunkSize(512 * 1025);
118-
assertEquals(2 * MIN_CHUNK_SIZE, channel.getChunkSize());
117+
assertThat(channel.getChunkSize() >= MIN_CHUNK_SIZE).isTrue();
118+
assertThat(channel.getChunkSize() % MIN_CHUNK_SIZE).isEqualTo(0);
119+
120+
channel.setChunkSize(2 * MIN_CHUNK_SIZE + 1);
121+
assertThat(channel.getChunkSize() >= MIN_CHUNK_SIZE).isTrue();
122+
assertThat(channel.getChunkSize() % MIN_CHUNK_SIZE).isEqualTo(0);
119123
}
120124

121125
@Test

0 commit comments

Comments
 (0)