d2fe7dd4fe9c436c3954d3c63682653cf69f4271
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / ReplicatedLogImplTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Matchers.same;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.reset;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.verifyNoMoreInteractions;
16 import akka.japi.Procedure;
17 import java.util.Collections;
18 import org.hamcrest.BaseMatcher;
19 import org.hamcrest.Description;
20 import org.hamcrest.Matcher;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.Matchers;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.MockitoAnnotations;
28 import org.mockito.internal.matchers.Same;
29 import org.opendaylight.controller.cluster.DataPersistenceProvider;
30 import org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload;
31 import org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockReplicatedLogEntry;
32 import org.opendaylight.controller.cluster.raft.base.messages.DeleteEntries;
33 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Unit tests for ReplicatedLogImpl.
39  *
40  * @author Thomas Pantelis
41  */
42 public class ReplicatedLogImplTest {
43     private static final Logger LOG = LoggerFactory.getLogger(RaftActorRecoverySupportTest.class);
44
45     @Mock
46     private DataPersistenceProvider mockPersistence;
47
48     @Mock
49     private RaftActorBehavior mockBehavior;
50
51     private RaftActorContext context;
52     private final DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
53
54     @Before
55     public void setup() {
56         MockitoAnnotations.initMocks(this);
57
58         context = new RaftActorContextImpl(null, null, "test",
59                 new ElectionTermImpl(mockPersistence, "test", LOG),
60                 -1, -1, Collections.<String,String>emptyMap(), configParams, mockPersistence, LOG);
61     }
62
63     private void verifyPersist(Object message) throws Exception {
64         verifyPersist(message, new Same(message));
65     }
66
67     @SuppressWarnings({ "unchecked", "rawtypes" })
68     private void verifyPersist(Object message, Matcher<?> matcher) throws Exception {
69         ArgumentCaptor<Procedure> procedure = ArgumentCaptor.forClass(Procedure.class);
70         verify(mockPersistence).persist(Matchers.argThat(matcher), procedure.capture());
71
72         procedure.getValue().apply(message);
73     }
74
75     @SuppressWarnings("unchecked")
76     @Test
77     public void testAppendAndPersistExpectingNoCapture() throws Exception {
78         ReplicatedLog log = ReplicatedLogImpl.newInstance(context);
79
80         MockReplicatedLogEntry logEntry1 = new MockReplicatedLogEntry(1, 1, new MockPayload("1"));
81
82         log.appendAndPersist(logEntry1);
83
84         verifyPersist(logEntry1);
85
86         assertEquals("size", 1, log.size());
87
88         reset(mockPersistence);
89
90         MockReplicatedLogEntry logEntry2 = new MockReplicatedLogEntry(1, 2, new MockPayload("2"));
91         Procedure<ReplicatedLogEntry> mockCallback = Mockito.mock(Procedure.class);
92         log.appendAndPersist(logEntry2, mockCallback);
93
94         verifyPersist(logEntry2);
95
96         verify(mockCallback).apply(same(logEntry2));
97
98         assertEquals("size", 2, log.size());
99     }
100
101     @SuppressWarnings("unchecked")
102     @Test
103     public void testAppendAndPersisWithDuplicateEntry() throws Exception {
104         ReplicatedLog log = ReplicatedLogImpl.newInstance(context);
105
106         Procedure<ReplicatedLogEntry> mockCallback = Mockito.mock(Procedure.class);
107         MockReplicatedLogEntry logEntry = new MockReplicatedLogEntry(1, 1, new MockPayload("1"));
108
109         log.appendAndPersist(logEntry, mockCallback);
110
111         verifyPersist(logEntry);
112
113         assertEquals("size", 1, log.size());
114
115         reset(mockPersistence, mockCallback);
116
117         log.appendAndPersist(logEntry, mockCallback);
118
119         verifyNoMoreInteractions(mockPersistence, mockCallback);
120
121         assertEquals("size", 1, log.size());
122     }
123
124     @Test
125     public void testAppendAndPersistExpectingCaptureDueToJournalCount() throws Exception {
126         configParams.setSnapshotBatchCount(2);
127
128         doReturn(1L).when(mockBehavior).getReplicatedToAllIndex();
129
130         ReplicatedLog log = ReplicatedLogImpl.newInstance(context);
131
132         MockReplicatedLogEntry logEntry1 = new MockReplicatedLogEntry(1, 2, new MockPayload("2"));
133         MockReplicatedLogEntry logEntry2 = new MockReplicatedLogEntry(1, 3, new MockPayload("3"));
134
135         log.appendAndPersist(logEntry1);
136         verifyPersist(logEntry1);
137
138         reset(mockPersistence);
139
140         log.appendAndPersist(logEntry2);
141         verifyPersist(logEntry2);
142
143
144         assertEquals("size", 2, log.size());
145     }
146
147     @Test
148     public void testAppendAndPersistExpectingCaptureDueToDataSize() throws Exception {
149         doReturn(1L).when(mockBehavior).getReplicatedToAllIndex();
150
151         context.setTotalMemoryRetriever(() -> 100);
152
153         ReplicatedLog log = ReplicatedLogImpl.newInstance(context);
154
155         int dataSize = 600;
156         MockReplicatedLogEntry logEntry = new MockReplicatedLogEntry(1, 2, new MockPayload("2", dataSize));
157
158         log.appendAndPersist(logEntry);
159         verifyPersist(logEntry);
160
161         reset(mockPersistence);
162
163         logEntry = new MockReplicatedLogEntry(1, 3, new MockPayload("3", 5));
164
165         log.appendAndPersist(logEntry);
166         verifyPersist(logEntry);
167
168         assertEquals("size", 2, log.size());
169     }
170
171     @Test
172     public void testRemoveFromAndPersist() throws Exception {
173
174         ReplicatedLog log = ReplicatedLogImpl.newInstance(context);
175
176         log.append(new MockReplicatedLogEntry(1, 0, new MockPayload("0")));
177         log.append(new MockReplicatedLogEntry(1, 1, new MockPayload("1")));
178         log.append(new MockReplicatedLogEntry(1, 2, new MockPayload("2")));
179
180         log.removeFromAndPersist(1);
181
182         DeleteEntries deleteEntries = new DeleteEntries(1);
183         verifyPersist(deleteEntries, match(deleteEntries));
184
185         assertEquals("size", 1, log.size());
186
187         reset(mockPersistence);
188
189         log.removeFromAndPersist(1);
190
191         verifyNoMoreInteractions(mockPersistence);
192     }
193
194     public Matcher<DeleteEntries> match(final DeleteEntries actual){
195         return new BaseMatcher<DeleteEntries>() {
196             @Override
197             public boolean matches(Object o) {
198                 DeleteEntries other = (DeleteEntries) o;
199                 return actual.getFromIndex() == other.getFromIndex();
200             }
201
202             @Override
203             public void describeTo(Description description) {
204                 description.appendText("DeleteEntries: fromIndex: " + actual.getFromIndex());
205             }
206         };
207     }
208 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.