Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / behaviors / SnapshotTrackerTest.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. 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 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.spy;
14 import static org.mockito.Mockito.verify;
15
16 import akka.protobuf.ByteString;
17 import com.google.common.io.ByteSource;
18 import java.io.IOException;
19 import java.util.Arrays;
20 import java.util.HashMap;
21 import java.util.OptionalInt;
22 import org.apache.commons.lang3.SerializationUtils;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.opendaylight.controller.cluster.io.FileBackedOutputStream;
29 import org.opendaylight.controller.cluster.io.FileBackedOutputStreamFactory;
30 import org.opendaylight.controller.cluster.raft.RaftActorContext;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 @RunWith(MockitoJUnitRunner.StrictStubs.class)
35 public class SnapshotTrackerTest {
36     private static final Logger LOG = LoggerFactory.getLogger(SnapshotTrackerTest.class);
37
38     private final HashMap<String, String> data = new HashMap<>();
39
40     @Mock
41     private RaftActorContext mockContext;
42     private FileBackedOutputStream fbos;
43     private ByteString byteString;
44     private byte[] chunk1;
45     private byte[] chunk2;
46     private byte[] chunk3;
47
48     @Before
49     public void setup() {
50         data.put("key1", "value1");
51         data.put("key2", "value2");
52         data.put("key3", "value3");
53
54         byteString = ByteString.copyFrom(SerializationUtils.serialize(data));
55         chunk1 = getNextChunk(byteString, 0, 10);
56         chunk2 = getNextChunk(byteString, 10, 10);
57         chunk3 = getNextChunk(byteString, 20, byteString.size());
58
59         fbos = spy(new FileBackedOutputStream(100000000, "target"));
60         FileBackedOutputStreamFactory mockFactory = mock(FileBackedOutputStreamFactory.class);
61         doReturn(fbos).when(mockFactory).newInstance();
62         doReturn(mockFactory).when(mockContext).getFileBackedOutputStreamFactory();
63     }
64
65     @Test
66     public void testAddChunks() throws IOException {
67         try (SnapshotTracker tracker = new SnapshotTracker(LOG, 3, "leader", mockContext)) {
68             tracker.addChunk(1, chunk1, OptionalInt.of(LeaderInstallSnapshotState.INITIAL_LAST_CHUNK_HASH_CODE));
69             tracker.addChunk(2, chunk2, OptionalInt.of(Arrays.hashCode(chunk1)));
70             tracker.addChunk(3, chunk3, OptionalInt.of(Arrays.hashCode(chunk2)));
71
72             ByteSource snapshotBytes = tracker.getSnapshotBytes();
73             assertEquals("Deserialized", data, SerializationUtils.deserialize(snapshotBytes.read()));
74         }
75
76         verify(fbos).cleanup();
77     }
78
79     @Test(expected = SnapshotTracker.InvalidChunkException.class)
80     public void testAddChunkWhenAlreadySealed() throws IOException {
81         try (SnapshotTracker tracker = new SnapshotTracker(LOG, 2, "leader", mockContext)) {
82             tracker.addChunk(1, chunk1, OptionalInt.empty());
83             tracker.addChunk(2, chunk2, OptionalInt.empty());
84             tracker.addChunk(3, chunk3, OptionalInt.empty());
85         }
86     }
87
88     @Test(expected = SnapshotTracker.InvalidChunkException.class)
89     public void testInvalidFirstChunkIndex() throws IOException {
90         try (SnapshotTracker tracker = new SnapshotTracker(LOG, 2, "leader", mockContext)) {
91             tracker.addChunk(LeaderInstallSnapshotState.FIRST_CHUNK_INDEX - 1, chunk1, OptionalInt.empty());
92         }
93     }
94
95     @Test(expected = SnapshotTracker.InvalidChunkException.class)
96     public void testOutOfSequenceChunk() throws IOException {
97         try (SnapshotTracker tracker = new SnapshotTracker(LOG, 2, "leader", mockContext)) {
98             tracker.addChunk(1, chunk1, OptionalInt.empty());
99             tracker.addChunk(3, chunk3, OptionalInt.empty());
100         }
101     }
102
103     @Test(expected = SnapshotTracker.InvalidChunkException.class)
104     public void testInvalidLastChunkHashCode() throws IOException {
105         try (SnapshotTracker tracker = new SnapshotTracker(LOG, 2, "leader", mockContext)) {
106             tracker.addChunk(1, chunk1, OptionalInt.of(LeaderInstallSnapshotState.INITIAL_LAST_CHUNK_HASH_CODE));
107             tracker.addChunk(2, chunk2, OptionalInt.of(1));
108         }
109     }
110
111     @Test(expected = IllegalStateException.class)
112     public void testGetSnapshotBytesWhenNotSealed() throws IOException {
113         try (SnapshotTracker tracker = new SnapshotTracker(LOG, 2, "leader", mockContext)) {
114             tracker.addChunk(1, chunk1, OptionalInt.empty());
115             tracker.getSnapshotBytes();
116         }
117     }
118
119     private static byte[] getNextChunk(final ByteString bs, final int offset, int size) {
120         int snapshotLength = bs.size();
121         int start = offset;
122         if (size > snapshotLength) {
123             size = snapshotLength;
124         } else if (start + size > snapshotLength) {
125             size = snapshotLength - start;
126         }
127
128         byte[] nextChunk = new byte[size];
129         bs.copyTo(nextChunk, start, 0, size);
130         return nextChunk;
131     }
132 }