Fix Eclipse compilation warnings.
[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 import akka.util.Timeout;
16 import com.google.common.util.concurrent.Uninterruptibles;
17 import java.util.concurrent.Executors;
18 import java.util.concurrent.TimeUnit;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
24 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import scala.concurrent.duration.FiniteDuration;
27
28 public class DistributedDataStoreTest extends AbstractActorTest {
29
30     private SchemaContext schemaContext;
31
32     @Mock
33     private ActorContext actorContext;
34
35     @Mock
36     private DatastoreContext datastoreContext;
37
38     @Mock
39     private Timeout shardElectionTimeout;
40
41     @Before
42     public void setUp() throws Exception {
43         MockitoAnnotations.initMocks(this);
44
45         schemaContext = TestModel.createTestContext();
46
47         doReturn(schemaContext).when(actorContext).getSchemaContext();
48         doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
49     }
50
51     @Test
52     public void testRateLimitingUsedInReadWriteTxCreation(){
53         try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext)) {
54
55             distributedDataStore.newReadWriteTransaction();
56
57             verify(actorContext, times(1)).acquireTxCreationPermit();
58         }
59     }
60
61     @Test
62     public void testRateLimitingUsedInWriteOnlyTxCreation(){
63         try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext)) {
64
65             distributedDataStore.newWriteOnlyTransaction();
66
67             verify(actorContext, times(1)).acquireTxCreationPermit();
68         }
69     }
70
71     @Test
72     public void testRateLimitingNotUsedInReadOnlyTxCreation(){
73         try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext)) {
74
75             distributedDataStore.newReadOnlyTransaction();
76             distributedDataStore.newReadOnlyTransaction();
77             distributedDataStore.newReadOnlyTransaction();
78
79             verify(actorContext, times(0)).acquireTxCreationPermit();
80         }
81     }
82
83     @Test
84     public void testWaitTillReadyBlocking(){
85         doReturn(datastoreContext).when(actorContext).getDatastoreContext();
86         doReturn(shardElectionTimeout).when(datastoreContext).getShardLeaderElectionTimeout();
87         doReturn(FiniteDuration.apply(50, TimeUnit.MILLISECONDS)).when(shardElectionTimeout).duration();
88         try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext)) {
89
90             long start = System.currentTimeMillis();
91
92             distributedDataStore.waitTillReady();
93
94             long end = System.currentTimeMillis();
95
96             assertTrue("Expected to be blocked for 50 millis", (end - start) >= 50);
97         }
98     }
99
100     @Test
101     public void testWaitTillReadyCountDown(){
102         try (final DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext)) {
103             doReturn(datastoreContext).when(actorContext).getDatastoreContext();
104             doReturn(shardElectionTimeout).when(datastoreContext).getShardLeaderElectionTimeout();
105             doReturn(FiniteDuration.apply(5000, TimeUnit.MILLISECONDS)).when(shardElectionTimeout).duration();
106
107             Executors.newSingleThreadExecutor().submit(new Runnable() {
108                 @Override
109                 public void run() {
110                     Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
111                     distributedDataStore.getWaitTillReadyCountDownLatch().countDown();
112                 }
113             });
114
115             long start = System.currentTimeMillis();
116
117             distributedDataStore.waitTillReady();
118
119             long end = System.currentTimeMillis();
120
121             assertTrue("Expected to be released in 500 millis", (end - start) < 5000);
122         }
123     }
124
125 }