2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.io;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
13 import java.io.IOException;
14 import java.util.List;
15 import org.junit.Test;
17 public class ChunkedOutputStreamTest {
18 private static final int INITIAL_SIZE = 256;
19 private static final int MAX_ARRAY_SIZE = 256 * 1024;
21 private final ChunkedOutputStream stream = new ChunkedOutputStream(INITIAL_SIZE, MAX_ARRAY_SIZE);
24 public void testBasicWrite() throws IOException {
25 for (int i = 0; i < INITIAL_SIZE; ++i) {
29 final byte[] chunk = assertFinishedStream(INITIAL_SIZE, 1).get(0);
30 assertEquals(INITIAL_SIZE, chunk.length);
31 for (int i = 0; i < INITIAL_SIZE; ++i) {
32 assertEquals((byte) i, chunk[i]);
37 public void testBasicLargeWrite() throws IOException {
38 final byte[] array = createArray(INITIAL_SIZE);
40 final byte[] chunk = assertFinishedStream(INITIAL_SIZE, 1).get(0);
41 assertArrayEquals(array, chunk);
45 public void testGrowWrite() throws IOException {
46 for (int i = 0; i < INITIAL_SIZE * 2; ++i) {
50 final byte[] chunk = assertFinishedStream(INITIAL_SIZE * 2, 1).get(0);
51 assertEquals(INITIAL_SIZE * 2, chunk.length);
52 for (int i = 0; i < INITIAL_SIZE * 2; ++i) {
53 assertEquals((byte) i, chunk[i]);
58 public void testGrowLargeWrite() throws IOException {
59 final byte[] array = createArray(INITIAL_SIZE * 2);
61 final byte[] chunk = assertFinishedStream(INITIAL_SIZE * 2, 1).get(0);
62 assertArrayEquals(array, chunk);
66 public void testTwoChunksWrite() throws IOException {
67 int size = MAX_ARRAY_SIZE + 1;
68 for (int i = 0; i < size; ++i) {
73 for (byte[] chunk: assertFinishedStream(size, 2)) {
74 for (byte actual: chunk) {
75 assertEquals((byte) counter++, actual);
80 private List<byte[]> assertFinishedStream(final int expectedSize, final int expectedChunks) {
82 final ChunkedByteArray array = stream.toChunkedByteArray();
83 assertEquals(expectedSize, array.size());
85 final List<byte[]> chunks = array.getChunks();
86 assertEquals(expectedChunks, chunks.size());
90 private static byte[] createArray(final int size) {
91 final byte[] array = new byte[size];
92 for (int i = 0; i < size; ++i) {