Skip to content

Commit 3380834

Browse files
authored
test: add test example of fillFrom properly handling 0 byte reads (#2545)
1 parent f0f5914 commit 3380834

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

google-cloud-storage/src/test/java/com/google/cloud/storage/BuffersTest.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@
1616

1717
package com.google.cloud.storage;
1818

19+
import static com.google.cloud.storage.TestUtils.assertAll;
20+
import static com.google.cloud.storage.TestUtils.xxd;
1921
import static com.google.common.truth.Truth.assertThat;
2022

23+
import java.io.IOException;
2124
import java.nio.ByteBuffer;
25+
import java.nio.channels.ReadableByteChannel;
2226
import java.security.SecureRandom;
27+
import java.util.concurrent.atomic.AtomicInteger;
2328
import org.junit.Test;
2429

2530
public final class BuffersTest {
@@ -72,4 +77,87 @@ public void allocateAligned_evenlyDivisible_capacityGtAlignment() {
7277
ByteBuffer b1 = Buffers.allocateAligned(8, 4);
7378
assertThat(b1.capacity()).isEqualTo(8);
7479
}
80+
81+
@Test
82+
public void fillFrom_handles_0SizeRead_someBytesRead() throws Exception {
83+
byte[] bytes = new byte[14];
84+
ByteBuffer buf = ByteBuffer.wrap(bytes);
85+
86+
byte[] expected =
87+
new byte[] {
88+
(byte) 'A',
89+
(byte) 'B',
90+
(byte) 'C',
91+
(byte) 'A',
92+
(byte) 'B',
93+
(byte) 'A',
94+
(byte) 'A',
95+
(byte) 'A',
96+
(byte) 'B',
97+
(byte) 'A',
98+
(byte) 'B',
99+
(byte) 'C',
100+
(byte) 0,
101+
(byte) 0
102+
};
103+
104+
int[] acceptSequence = new int[] {3, 2, 1, 0, 0, 1, 2, 3};
105+
AtomicInteger readCount = new AtomicInteger(0);
106+
107+
ReadableByteChannel c =
108+
new ReadableByteChannel() {
109+
@Override
110+
public int read(ByteBuffer dst) throws IOException {
111+
int i = readCount.getAndIncrement();
112+
if (i == acceptSequence.length) {
113+
return -1;
114+
}
115+
int bytesToRead = acceptSequence[i];
116+
if (bytesToRead > 0) {
117+
long copy =
118+
Buffers.copy(DataGenerator.base64Characters().genByteBuffer(bytesToRead), dst);
119+
assertThat(copy).isEqualTo(bytesToRead);
120+
}
121+
122+
return bytesToRead;
123+
}
124+
125+
@Override
126+
public boolean isOpen() {
127+
return true;
128+
}
129+
130+
@Override
131+
public void close() throws IOException {}
132+
};
133+
int filled = Buffers.fillFrom(buf, c);
134+
135+
assertAll(
136+
() -> assertThat(filled).isEqualTo(12),
137+
() -> assertThat(xxd(bytes)).isEqualTo(xxd(expected)));
138+
}
139+
140+
@Test
141+
public void fillFrom_handles_0SizeRead_noBytesRead() throws Exception {
142+
ByteBuffer buf = ByteBuffer.allocate(3);
143+
144+
ReadableByteChannel c =
145+
new ReadableByteChannel() {
146+
@Override
147+
public int read(ByteBuffer dst) throws IOException {
148+
return -1;
149+
}
150+
151+
@Override
152+
public boolean isOpen() {
153+
return true;
154+
}
155+
156+
@Override
157+
public void close() throws IOException {}
158+
};
159+
int filled = Buffers.fillFrom(buf, c);
160+
161+
assertThat(filled).isEqualTo(-1);
162+
}
75163
}

0 commit comments

Comments
 (0)