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