|
25 | 25 | import java.io.ByteArrayOutputStream;
|
26 | 26 | import java.io.File;
|
27 | 27 | import java.io.IOException;
|
| 28 | +import java.io.OutputStream; |
28 | 29 | import java.util.ArrayList;
|
29 | 30 | import java.util.List;
|
30 | 31 | import java.util.Random;
|
| 32 | +import java.util.function.Function; |
31 | 33 | import java.util.stream.Stream;
|
32 | 34 |
|
33 | 35 | import org.apache.avro.file.CodecFactory;
|
|
40 | 42 | import org.apache.avro.generic.GenericData;
|
41 | 43 | import org.apache.avro.generic.GenericDatumReader;
|
42 | 44 | import org.apache.avro.generic.GenericDatumWriter;
|
| 45 | +import org.apache.avro.io.BinaryEncoder; |
43 | 46 | import org.apache.avro.io.DatumReader;
|
| 47 | +import org.apache.avro.io.EncoderFactory; |
44 | 48 | import org.apache.avro.util.RandomData;
|
45 | 49 |
|
46 | 50 | import org.junit.jupiter.api.Test;
|
@@ -93,22 +97,32 @@ private File makeFile(CodecFactory codec) {
|
93 | 97 | @ParameterizedTest
|
94 | 98 | @MethodSource("codecs")
|
95 | 99 | public void runTestsInOrder(CodecFactory codec) throws Exception {
|
96 |
| - LOG.info("Running with codec: " + codec); |
97 |
| - testGenericWrite(codec); |
98 |
| - testGenericRead(codec); |
99 |
| - testSplits(codec); |
100 |
| - testSyncDiscovery(codec); |
101 |
| - testGenericAppend(codec); |
102 |
| - testReadWithHeader(codec); |
103 |
| - testFSync(codec, false); |
104 |
| - testFSync(codec, true); |
| 100 | + // Run for both encoders, but the MethodSource didn't really like it, |
| 101 | + // so it is just a loop within the test |
| 102 | + List<Function<OutputStream, BinaryEncoder>> encoders = new ArrayList<>(); |
| 103 | + encoders.add(b -> new EncoderFactory().directBinaryEncoder(b, null)); |
| 104 | + encoders.add(b -> new EncoderFactory().blockingDirectBinaryEncoder(b, null)); |
| 105 | + |
| 106 | + for (Function<OutputStream, BinaryEncoder> encoder : encoders) { |
| 107 | + LOG.info("Running with codec: {}", codec); |
| 108 | + testGenericWrite(codec, encoder); |
| 109 | + testGenericRead(codec); |
| 110 | + testSplits(codec); |
| 111 | + testSyncDiscovery(codec); |
| 112 | + testGenericAppend(codec, encoder); |
| 113 | + testReadWithHeader(codec); |
| 114 | + testFSync(codec, encoder, false); |
| 115 | + testFSync(codec, encoder, true); |
| 116 | + } |
105 | 117 | }
|
106 | 118 |
|
107 |
| - private void testGenericWrite(CodecFactory codec) throws IOException { |
| 119 | + private void testGenericWrite(CodecFactory codec, Function<OutputStream, BinaryEncoder> encoderFunc) |
| 120 | + throws IOException { |
108 | 121 | DataFileWriter<Object> writer = new DataFileWriter<>(new GenericDatumWriter<>()).setSyncInterval(100);
|
109 | 122 | if (codec != null) {
|
110 | 123 | writer.setCodec(codec);
|
111 | 124 | }
|
| 125 | + writer.setEncoder(encoderFunc); |
112 | 126 | writer.create(SCHEMA, makeFile(codec));
|
113 | 127 | try {
|
114 | 128 | int count = 0;
|
@@ -210,10 +224,12 @@ private void testSyncDiscovery(CodecFactory codec) throws IOException {
|
210 | 224 | }
|
211 | 225 | }
|
212 | 226 |
|
213 |
| - private void testGenericAppend(CodecFactory codec) throws IOException { |
| 227 | + private void testGenericAppend(CodecFactory codec, Function<OutputStream, BinaryEncoder> encoderFunc) |
| 228 | + throws IOException { |
214 | 229 | File file = makeFile(codec);
|
215 | 230 | long start = file.length();
|
216 | 231 | try (DataFileWriter<Object> writer = new DataFileWriter<>(new GenericDatumWriter<>()).appendTo(file)) {
|
| 232 | + writer.setEncoder(encoderFunc); |
217 | 233 | for (Object datum : new RandomData(SCHEMA, COUNT, SEED + 1)) {
|
218 | 234 | writer.append(datum);
|
219 | 235 | }
|
@@ -254,11 +270,8 @@ private void testReadWithHeader(CodecFactory codec) throws IOException {
|
254 | 270 | assertEquals(validPos, sin.tell(), "Should not move from sync point on reopen");
|
255 | 271 | assertNotNull(readerFalse.next(), "Should be able to reopen at sync point");
|
256 | 272 | }
|
257 |
| - |
258 | 273 | }
|
259 |
| - |
260 | 274 | }
|
261 |
| - |
262 | 275 | }
|
263 | 276 |
|
264 | 277 | @Test
|
@@ -306,8 +319,10 @@ public void flushCount() throws IOException {
|
306 | 319 | assertTrue(out.flushCount < currentCount && out.flushCount >= flushCounter);
|
307 | 320 | }
|
308 | 321 |
|
309 |
| - private void testFSync(CodecFactory codec, boolean useFile) throws IOException { |
| 322 | + private void testFSync(CodecFactory codec, Function<OutputStream, BinaryEncoder> encoderFunc, boolean useFile) |
| 323 | + throws IOException { |
310 | 324 | try (DataFileWriter<Object> writer = new DataFileWriter<>(new GenericDatumWriter<>())) {
|
| 325 | + writer.setEncoder(encoderFunc); |
311 | 326 | writer.setFlushOnEveryBlock(false);
|
312 | 327 | TestingByteArrayOutputStream out = new TestingByteArrayOutputStream();
|
313 | 328 | if (useFile) {
|
|
0 commit comments