Fixup checkstyle
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DistributedDataStoreTest.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
9 package org.opendaylight.controller.cluster.datastore;
10
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15
16 import akka.util.Timeout;
17 import com.google.common.util.concurrent.Uninterruptibles;
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.TimeUnit;
20 import org.junit.AfterClass;
21 import org.junit.Before;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
27 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
28 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
29 import org.opendaylight.controller.cluster.access.concepts.MemberName;
30 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
31 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
32 import org.opendaylight.yangtools.yang.common.Empty;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import scala.concurrent.duration.FiniteDuration;
35
36 public class DistributedDataStoreTest extends AbstractActorTest {
37     private static final ClientIdentifier UNKNOWN_ID = ClientIdentifier.create(
38             FrontendIdentifier.create(MemberName.forName("local"), FrontendType.forName("unknown")), 0);
39
40     private static SchemaContext SCHEMA_CONTEXT;
41
42     @Mock
43     private ActorUtils actorUtils;
44
45     @Mock
46     private DatastoreContext datastoreContext;
47
48     @Mock
49     private Timeout shardElectionTimeout;
50
51     @BeforeClass
52     public static void beforeClass() {
53         SCHEMA_CONTEXT = TestModel.createTestContext();
54     }
55
56     @AfterClass
57     public static void afterClass() {
58         SCHEMA_CONTEXT = null;
59     }
60
61     @Before
62     public void setUp() {
63         MockitoAnnotations.initMocks(this);
64
65         doReturn(SCHEMA_CONTEXT).when(actorUtils).getSchemaContext();
66         doReturn(DatastoreContext.newBuilder().build()).when(actorUtils).getDatastoreContext();
67     }
68
69     @Test
70     public void testRateLimitingUsedInReadWriteTxCreation() {
71         try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorUtils, UNKNOWN_ID)) {
72
73             distributedDataStore.newReadWriteTransaction();
74
75             verify(actorUtils, times(1)).acquireTxCreationPermit();
76         }
77     }
78
79     @Test
80     public void testRateLimitingUsedInWriteOnlyTxCreation() {
81         try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorUtils, UNKNOWN_ID)) {
82
83             distributedDataStore.newWriteOnlyTransaction();
84
85             verify(actorUtils, times(1)).acquireTxCreationPermit();
86         }
87     }
88
89     @Test
90     public void testRateLimitingNotUsedInReadOnlyTxCreation() {
91         try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorUtils, UNKNOWN_ID)) {
92
93             distributedDataStore.newReadOnlyTransaction();
94             distributedDataStore.newReadOnlyTransaction();
95             distributedDataStore.newReadOnlyTransaction();
96
97             verify(actorUtils, times(0)).acquireTxCreationPermit();
98         }
99     }
100
101     @Test
102     public void testWaitTillReadyBlocking() {
103         doReturn(datastoreContext).when(actorUtils).getDatastoreContext();
104         doReturn(shardElectionTimeout).when(datastoreContext).getShardLeaderElectionTimeout();
105         doReturn(1).when(datastoreContext).getInitialSettleTimeoutMultiplier();
106         doReturn(FiniteDuration.apply(50, TimeUnit.MILLISECONDS)).when(shardElectionTimeout).duration();
107         try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorUtils, UNKNOWN_ID)) {
108
109             long start = System.currentTimeMillis();
110
111             distributedDataStore.waitTillReady();
112
113             long end = System.currentTimeMillis();
114
115             assertTrue("Expected to be blocked for 50 millis", end - start >= 50);
116         }
117     }
118
119     @Test
120     public void testWaitTillReadyCountDown() {
121         try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorUtils, UNKNOWN_ID)) {
122             doReturn(datastoreContext).when(actorUtils).getDatastoreContext();
123             doReturn(shardElectionTimeout).when(datastoreContext).getShardLeaderElectionTimeout();
124             doReturn(FiniteDuration.apply(5000, TimeUnit.MILLISECONDS)).when(shardElectionTimeout).duration();
125
126             Executors.newSingleThreadExecutor().submit(() -> {
127                 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
128                 distributedDataStore.readinessFuture().set(Empty.value());
129             });
130
131             long start = System.currentTimeMillis();
132
133             distributedDataStore.waitTillReady();
134
135             long end = System.currentTimeMillis();
136
137             assertTrue("Expected to be released in 500 millis", end - start < 5000);
138         }
139     }
140 }