|
7 | 7 |
|
8 | 8 | package org.elasticsearch.compute.data; |
9 | 9 |
|
| 10 | +import org.elasticsearch.common.io.stream.BytesStreamOutput; |
| 11 | +import org.elasticsearch.compute.test.BlockTestUtils; |
10 | 12 | import org.elasticsearch.compute.test.ComputeTestCase; |
11 | 13 | import org.elasticsearch.compute.test.RandomBlock; |
12 | 14 | import org.elasticsearch.core.Releasables; |
13 | 15 | import org.elasticsearch.exponentialhistogram.ExponentialHistogram; |
14 | 16 | import org.elasticsearch.exponentialhistogram.ExponentialHistogramCircuitBreaker; |
15 | 17 |
|
| 18 | +import java.io.IOException; |
16 | 19 | import java.util.List; |
17 | 20 |
|
18 | 21 | import static org.hamcrest.CoreMatchers.not; |
19 | 22 | import static org.hamcrest.Matchers.equalTo; |
20 | 23 |
|
21 | | -public class ExponentialHistogramBlockEqualityTests extends ComputeTestCase { |
| 24 | +public class ExponentialHistogramBlockTests extends ComputeTestCase { |
22 | 25 |
|
23 | | - public void testEmptyBlock() { |
| 26 | + public void testPopulatedBlockSerialization() throws IOException { |
| 27 | + int elementCount = randomIntBetween(1, 100); |
| 28 | + ExponentialHistogramBlockBuilder builder = blockFactory().newExponentialHistogramBlockBuilder(elementCount); |
| 29 | + for (int i = 0; i < elementCount; i++) { |
| 30 | + if (randomBoolean()) { |
| 31 | + builder.appendNull(); |
| 32 | + } else { |
| 33 | + builder.append(BlockTestUtils.randomExponentialHistogram()); |
| 34 | + } |
| 35 | + } |
| 36 | + ExponentialHistogramBlock block = builder.build(); |
| 37 | + Block deserializedBlock = serializationRoundTrip(block); |
| 38 | + assertThat(deserializedBlock, equalTo(block)); |
| 39 | + Releasables.close(block, deserializedBlock); |
| 40 | + } |
| 41 | + |
| 42 | + public void testNullSerialization() throws IOException { |
| 43 | + // sub-blocks can be constant null, those should serialize correctly too |
| 44 | + int elementCount = randomIntBetween(1, 100); |
| 45 | + |
| 46 | + Block block = new ExponentialHistogramArrayBlock( |
| 47 | + (DoubleBlock) blockFactory().newConstantNullBlock(elementCount), |
| 48 | + (DoubleBlock) blockFactory().newConstantNullBlock(elementCount), |
| 49 | + (DoubleBlock) blockFactory().newConstantNullBlock(elementCount), |
| 50 | + (LongBlock) blockFactory().newConstantNullBlock(elementCount), |
| 51 | + (DoubleBlock) blockFactory().newConstantNullBlock(elementCount), |
| 52 | + (BytesRefBlock) blockFactory().newConstantNullBlock(elementCount) |
| 53 | + ); |
| 54 | + |
| 55 | + Block deserializedBlock = serializationRoundTrip(block); |
| 56 | + assertThat(deserializedBlock, equalTo(block)); |
| 57 | + Releasables.close(block, deserializedBlock); |
| 58 | + } |
| 59 | + |
| 60 | + private Block serializationRoundTrip(Block block) throws IOException { |
| 61 | + BytesStreamOutput out = new BytesStreamOutput(); |
| 62 | + Block.writeTypedBlock(block, out); |
| 63 | + try (BlockStreamInput input = new BlockStreamInput(out.bytes().streamInput(), blockFactory())) { |
| 64 | + return Block.readTypedBlock(input); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public void testEmptyBlockEquality() { |
24 | 69 | List<Block> blocks = List.of( |
25 | 70 | blockFactory().newConstantNullBlock(0), |
26 | 71 | blockFactory().newExponentialHistogramBlockBuilder(0).build(), |
|
0 commit comments