2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore;
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;
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;
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);
39 private static SchemaContext SCHEMA_CONTEXT;
42 private ActorUtils actorUtils;
45 private DatastoreContext datastoreContext;
48 private Timeout shardElectionTimeout;
51 public static void beforeClass() {
52 SCHEMA_CONTEXT = TestModel.createTestContext();
56 public static void afterClass() {
57 SCHEMA_CONTEXT = null;
62 MockitoAnnotations.initMocks(this);
64 doReturn(SCHEMA_CONTEXT).when(actorUtils).getSchemaContext();
65 doReturn(DatastoreContext.newBuilder().build()).when(actorUtils).getDatastoreContext();
69 public void testRateLimitingUsedInReadWriteTxCreation() {
70 try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorUtils, UNKNOWN_ID)) {
72 distributedDataStore.newReadWriteTransaction();
74 verify(actorUtils, times(1)).acquireTxCreationPermit();
79 public void testRateLimitingUsedInWriteOnlyTxCreation() {
80 try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorUtils, UNKNOWN_ID)) {
82 distributedDataStore.newWriteOnlyTransaction();
84 verify(actorUtils, times(1)).acquireTxCreationPermit();
89 public void testRateLimitingNotUsedInReadOnlyTxCreation() {
90 try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorUtils, UNKNOWN_ID)) {
92 distributedDataStore.newReadOnlyTransaction();
93 distributedDataStore.newReadOnlyTransaction();
94 distributedDataStore.newReadOnlyTransaction();
96 verify(actorUtils, times(0)).acquireTxCreationPermit();
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)) {
107 long start = System.currentTimeMillis();
109 distributedDataStore.waitTillReady();
111 long end = System.currentTimeMillis();
113 assertTrue("Expected to be blocked for 50 millis", end - start >= 50);
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();
124 Executors.newSingleThreadExecutor().submit(() -> {
125 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
126 distributedDataStore.getWaitTillReadyCountDownLatch().countDown();
129 long start = System.currentTimeMillis();
131 distributedDataStore.waitTillReady();
133 long end = System.currentTimeMillis();
135 assertTrue("Expected to be released in 500 millis", end - start < 5000);