2 * Copyright (c) 2023 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.raft.behaviors;
10 import static org.junit.Assert.assertEquals;
12 import com.google.common.io.ByteSource;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.util.Arrays;
16 import java.util.Objects;
17 import org.junit.Test;
19 public class LeaderInstallSnapshotStateTest {
20 // Prime number on purpose
21 private static final int CHUNK_SIZE = 9_999_991;
22 // More than Integer.MAX_VALUE
23 private static final long SIZE = 4_294_967_294L;
26 public void testSnapshotLongerThanInteger() throws IOException {
27 try (var fts = new LeaderInstallSnapshotState(CHUNK_SIZE, "test")) {
28 fts.setSnapshotBytes(new MockByteSource(SIZE));
32 long expectedChunkSize = CHUNK_SIZE;
33 while (offset < SIZE) {
34 offset = offset + CHUNK_SIZE;
36 // We reached last chunk
37 expectedChunkSize = CHUNK_SIZE - (offset - SIZE);
41 final byte[] chunk = fts.getNextChunk();
42 assertEquals("byte size not matching for chunk:", expectedChunkSize, chunk.length);
43 assertEquals("chunk index not matching", chunkIndex, fts.getChunkIndex());
44 fts.markSendStatus(true);
45 if (!fts.isLastChunk(chunkIndex)) {
46 fts.incrementChunkIndex();
50 assertEquals("totalChunks not matching", chunkIndex, fts.getTotalChunks());
54 private static final class MockByteSource extends ByteSource {
55 private final long size;
57 private MockByteSource(final long size) {
67 public InputStream openStream() {
68 return new MockInputStream(size);
72 private static final class MockInputStream extends InputStream {
73 private long remaining;
75 MockInputStream(final long size) {
89 public int read(final byte[] bytes, final int off, final int len) {
90 Objects.checkFromIndexSize(off, len, bytes.length);
94 final int count = len <= remaining ? len : (int) remaining;
95 Arrays.fill(bytes, off, off + count, (byte) 0);