Fix license header violations in sal-distributed-datastore
[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         DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext);
54
55         distributedDataStore.newReadWriteTransaction();
56
57         verify(actorContext, times(1)).acquireTxCreationPermit();
58     }
59
60     @Test
61     public void testRateLimitingUsedInWriteOnlyTxCreation(){
62         DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext);
63
64         distributedDataStore.newWriteOnlyTransaction();
65
66         verify(actorContext, times(1)).acquireTxCreationPermit();
67     }
68
69
70     @Test
71     public void testRateLimitingNotUsedInReadOnlyTxCreation(){
72         DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext);
73
74         distributedDataStore.newReadOnlyTransaction();
75         distributedDataStore.newReadOnlyTransaction();
76         distributedDataStore.newReadOnlyTransaction();
77
78         verify(actorContext, times(0)).acquireTxCreationPermit();
79     }
80
81     @Test
82     public void testWaitTillReadyBlocking(){
83         doReturn(datastoreContext).when(actorContext).getDatastoreContext();
84         doReturn(shardElectionTimeout).when(datastoreContext).getShardLeaderElectionTimeout();
85         doReturn(FiniteDuration.apply(50, TimeUnit.MILLISECONDS)).when(shardElectionTimeout).duration();
86         DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext);
87
88         long start = System.currentTimeMillis();
89
90         distributedDataStore.waitTillReady();
91
92         long end = System.currentTimeMillis();
93
94         assertTrue("Expected to be blocked for 50 millis", (end-start) >= 50);
95     }
96
97     @Test
98     public void testWaitTillReadyCountDown(){
99         final DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext);
100         doReturn(datastoreContext).when(actorContext).getDatastoreContext();
101         doReturn(shardElectionTimeout).when(datastoreContext).getShardLeaderElectionTimeout();
102         doReturn(FiniteDuration.apply(5000, TimeUnit.MILLISECONDS)).when(shardElectionTimeout).duration();
103
104         Executors.newSingleThreadExecutor().submit(new Runnable() {
105             @Override
106             public void run() {
107                 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
108                 distributedDataStore.getWaitTillReadyCountDownLatch().countDown();
109             }
110         });
111
112         long start = System.currentTimeMillis();
113
114         distributedDataStore.waitTillReady();
115
116         long end = System.currentTimeMillis();
117
118         assertTrue("Expected to be released in 500 millis", (end-start) < 5000);
119
120     }
121
122 }