Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / behaviors / LeaderInstallSnapshotStateTest.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.raft.behaviors;
9
10 import static org.junit.Assert.assertEquals;
11
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;
18
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;
24
25     @Test
26     public void testSnapshotLongerThanInteger() throws IOException {
27         try (var fts = new LeaderInstallSnapshotState(CHUNK_SIZE, "test")) {
28             fts.setSnapshotBytes(new MockByteSource(SIZE));
29
30             int chunkIndex = 0;
31             long offset = 0;
32             long expectedChunkSize = CHUNK_SIZE;
33             while (offset < SIZE) {
34                 offset = offset + CHUNK_SIZE;
35                 if (offset > SIZE) {
36                     // We reached last chunk
37                     expectedChunkSize = CHUNK_SIZE - (offset - SIZE);
38                     offset = SIZE;
39                 }
40                 chunkIndex ++;
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();
47                 }
48             }
49
50             assertEquals("totalChunks not matching", chunkIndex, fts.getTotalChunks());
51         }
52     }
53
54     private static final class MockByteSource extends ByteSource {
55         private final long size;
56
57         private MockByteSource(final long size) {
58             this.size = size;
59         }
60
61         @Override
62         public long size() {
63             return size;
64         }
65
66         @Override
67         public InputStream openStream() {
68             return new MockInputStream(size);
69         }
70     }
71
72     private static final class MockInputStream extends InputStream {
73         private long remaining;
74
75         MockInputStream(final long size) {
76             remaining = size;
77         }
78
79         @Override
80         public int read() {
81             if (remaining > 0) {
82                 remaining--;
83                 return 0;
84             }
85             return -1;
86         }
87
88         @Override
89         public int read(final byte[] bytes, final int off, final int len) {
90             Objects.checkFromIndexSize(off, len, bytes.length);
91             if (remaining <= 0) {
92                 return -1;
93             }
94             final int count = len <= remaining ? len : (int) remaining;
95             Arrays.fill(bytes, off, off + count, (byte) 0);
96             remaining -= count;
97             return count;
98         }
99     }
100 }