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