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