Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Fokko committed Feb 12, 2024
1 parent 33b9461 commit 1b08260
Show file tree
Hide file tree
Showing 4 changed files with 525 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class BlockingDirectBinaryEncoder extends DirectBinaryEncoder {

private int depth = 0;

private long blockItemCount;
private final ArrayDeque<Long> blockItemCounts;

/**
* Create a writer that sends its output to the underlying stream
Expand All @@ -66,6 +66,7 @@ public BlockingDirectBinaryEncoder(OutputStream out) {
super(out);
this.buffers = new ArrayList<>();
this.stashedBuffers = new ArrayDeque<>();
this.blockItemCounts = new ArrayDeque<>();
}

private void startBlock() {
Expand All @@ -86,6 +87,7 @@ private void endBlock() {
this.depth -= 1;
out = stashedBuffers.pop();
BufferOutputStream buffer = this.buffers.get(depth);
long blockItemCount = blockItemCounts.pop();
if (blockItemCount > 0) {
try {
// Make it negative, so the reader knows that the number of bytes is coming
Expand All @@ -100,7 +102,7 @@ private void endBlock() {

@Override
public void setItemCount(long itemCount) throws IOException {
blockItemCount = itemCount;
blockItemCounts.push(itemCount);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.message.BinaryMessageDecoder;
import org.apache.avro.specific.TestRecordWithMapsAndArrays;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
Expand All @@ -31,13 +32,35 @@
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class TestBlockingDirectBinaryEncoder {

private void writeToArray(BinaryEncoder encoder, int[] numbers) throws IOException {
encoder.writeArrayStart();
encoder.setItemCount(numbers.length);
for (int number : numbers) {
encoder.startItem();
encoder.writeString(Integer.toString(number));
}
encoder.writeArrayEnd();
}

private void writeToMap(BinaryEncoder encoder, long[] numbers) throws IOException {
encoder.writeMapStart();
encoder.setItemCount(numbers.length);
for (long number : numbers) {
encoder.startItem();
encoder.writeString(Long.toString(number));
encoder.writeLong(number);
}
encoder.writeMapEnd();
}

@Test
void blockingDirectBinaryEncoder() throws IOException, NoSuchAlgorithmException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Expand All @@ -49,35 +72,57 @@ void blockingDirectBinaryEncoder() throws IOException, NoSuchAlgorithmException
encoder.writeFixed(new byte[] { (byte) 0xC3, (byte) 0x01 });
encoder.writeFixed(SchemaNormalization.parsingFingerprint("CRC-64-AVRO", TestRecordWithMapsAndArrays.SCHEMA$));

int len = 5;
// Array
this.writeToArray(encoder, new int[] { 1, 2, 3, 4, 5 });

// Map
writeToMap(encoder, new long[] { 1L, 2L, 3L, 4L, 5L });

// Nested Array

encoder.writeArrayStart();
encoder.setItemCount(len);
for (int i = 0; i < len; i++) {
encoder.startItem();
encoder.writeString(Integer.toString(i));
}
encoder.setItemCount(2);
this.writeToArray(encoder, new int[] { 1, 2 });
this.writeToArray(encoder, new int[] { 3, 4, 5 });
encoder.writeArrayEnd();

// Nested Map

encoder.writeMapStart();
encoder.setItemCount(len);
for (long i = 0; i < len; i++) {
encoder.startItem();
encoder.writeString(Long.toString(i));
encoder.writeLong(i);
}
encoder.setItemCount(2);
encoder.writeString("first");
this.writeToMap(encoder, new long[] { 1L, 2L });
encoder.writeString("second");
this.writeToMap(encoder, new long[] { 3L, 4L, 5L });
encoder.writeMapEnd();

// Read

encoder.flush();

BinaryMessageDecoder<TestRecordWithMapsAndArrays> decoder = TestRecordWithMapsAndArrays.getDecoder();
TestRecordWithMapsAndArrays r = decoder.decode(baos.toByteArray());

assertThat(r.getArr(), is(Arrays.asList("0", "1", "2", "3", "4")));
assertThat(r.getArr(), is(Arrays.asList("1", "2", "3", "4", "5")));
Map<String, Long> map = r.getMap();
assertThat(map.size(), is(5));
for (long i = 0; i < len; i++) {
for (long i = 1; i <= 5; i++) {
assertThat(map.get(Long.toString(i)), is(i));
}

assertThat(r.getNestedArr(), is(Arrays.asList(Arrays.asList("1", "2"), Arrays.asList("3", "4", "5"))));

Map<String, Map<String, Long>> nestedMap = r.getNestedMap();
assertThat(nestedMap.size(), is(2));

assertThat(nestedMap.get("first").size(), is(2));
assertThat(nestedMap.get("first").get("1"), is(1L));
assertThat(nestedMap.get("first").get("2"), is(2L));

assertThat(nestedMap.get("second").size(), is(3));
assertThat(nestedMap.get("second").get("3"), is(3L));
assertThat(nestedMap.get("second").get("4"), is(4L));
assertThat(nestedMap.get("second").get("5"), is(5L));
}

@Test
Expand All @@ -93,8 +138,8 @@ void testSkippingUsingBlocks() throws IOException, NoSuchAlgorithmException {
in.read(null, mockDecoder);
}

verify(mockDecoder, times(1)).skipMap();
verify(mockDecoder, times(1)).skipArray();
verify(mockDecoder, times(2)).skipMap();
verify(mockDecoder, times(2)).skipArray();
verify(mockDecoder, times(0)).readString();
verify(mockDecoder, times(0)).readLong();
}
Expand Down
Loading

0 comments on commit 1b08260

Please sign in to comment.