Skip to content

Commit

Permalink
test: add test example of fillFrom properly handling 0 byte reads (#2545
Browse files Browse the repository at this point in the history
)
  • Loading branch information
BenWhitehead authored May 22, 2024
1 parent f0f5914 commit 3380834
Showing 1 changed file with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@

package com.google.cloud.storage;

import static com.google.cloud.storage.TestUtils.assertAll;
import static com.google.cloud.storage.TestUtils.xxd;
import static com.google.common.truth.Truth.assertThat;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.security.SecureRandom;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;

public final class BuffersTest {
Expand Down Expand Up @@ -72,4 +77,87 @@ public void allocateAligned_evenlyDivisible_capacityGtAlignment() {
ByteBuffer b1 = Buffers.allocateAligned(8, 4);
assertThat(b1.capacity()).isEqualTo(8);
}

@Test
public void fillFrom_handles_0SizeRead_someBytesRead() throws Exception {
byte[] bytes = new byte[14];
ByteBuffer buf = ByteBuffer.wrap(bytes);

byte[] expected =
new byte[] {
(byte) 'A',
(byte) 'B',
(byte) 'C',
(byte) 'A',
(byte) 'B',
(byte) 'A',
(byte) 'A',
(byte) 'A',
(byte) 'B',
(byte) 'A',
(byte) 'B',
(byte) 'C',
(byte) 0,
(byte) 0
};

int[] acceptSequence = new int[] {3, 2, 1, 0, 0, 1, 2, 3};
AtomicInteger readCount = new AtomicInteger(0);

ReadableByteChannel c =
new ReadableByteChannel() {
@Override
public int read(ByteBuffer dst) throws IOException {
int i = readCount.getAndIncrement();
if (i == acceptSequence.length) {
return -1;
}
int bytesToRead = acceptSequence[i];
if (bytesToRead > 0) {
long copy =
Buffers.copy(DataGenerator.base64Characters().genByteBuffer(bytesToRead), dst);
assertThat(copy).isEqualTo(bytesToRead);
}

return bytesToRead;
}

@Override
public boolean isOpen() {
return true;
}

@Override
public void close() throws IOException {}
};
int filled = Buffers.fillFrom(buf, c);

assertAll(
() -> assertThat(filled).isEqualTo(12),
() -> assertThat(xxd(bytes)).isEqualTo(xxd(expected)));
}

@Test
public void fillFrom_handles_0SizeRead_noBytesRead() throws Exception {
ByteBuffer buf = ByteBuffer.allocate(3);

ReadableByteChannel c =
new ReadableByteChannel() {
@Override
public int read(ByteBuffer dst) throws IOException {
return -1;
}

@Override
public boolean isOpen() {
return true;
}

@Override
public void close() throws IOException {}
};
int filled = Buffers.fillFrom(buf, c);

assertThat(filled).isEqualTo(-1);
}
}

0 comments on commit 3380834

Please sign in to comment.