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.common.Empty;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import scala.concurrent.duration.FiniteDuration;
36 public class DistributedDataStoreTest extends AbstractActorTest {
37 private static final ClientIdentifier UNKNOWN_ID = ClientIdentifier.create(
38 FrontendIdentifier.create(MemberName.forName("local"), FrontendType.forName("unknown")), 0);
40 private static SchemaContext SCHEMA_CONTEXT;
43 private ActorUtils actorUtils;
46 private DatastoreContext datastoreContext;
49 private Timeout shardElectionTimeout;
52 public static void beforeClass() {
53 SCHEMA_CONTEXT = TestModel.createTestContext();
57 public static void afterClass() {
58 SCHEMA_CONTEXT = null;
63 MockitoAnnotations.initMocks(this);
65 doReturn(SCHEMA_CONTEXT).when(actorUtils).getSchemaContext();
66 doReturn(DatastoreContext.newBuilder().build()).when(actorUtils).getDatastoreContext();
70 public void testRateLimitingUsedInReadWriteTxCreation() {
71 try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorUtils, UNKNOWN_ID)) {
73 distributedDataStore.newReadWriteTransaction();
75 verify(actorUtils, times(1)).acquireTxCreationPermit();
80 public void testRateLimitingUsedInWriteOnlyTxCreation() {
81 try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorUtils, UNKNOWN_ID)) {
83 distributedDataStore.newWriteOnlyTransaction();
85 verify(actorUtils, times(1)).acquireTxCreationPermit();
90 public void testRateLimitingNotUsedInReadOnlyTxCreation() {
91 try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorUtils, UNKNOWN_ID)) {
93 distributedDataStore.newReadOnlyTransaction();
94 distributedDataStore.newReadOnlyTransaction();
95 distributedDataStore.newReadOnlyTransaction();
97 verify(actorUtils, times(0)).acquireTxCreationPermit();
102 public void testWaitTillReadyBlocking() {
103 doReturn(datastoreContext).when(actorUtils).getDatastoreContext();
104 doReturn(shardElectionTimeout).when(datastoreContext).getShardLeaderElectionTimeout();
105 doReturn(1).when(datastoreContext).getInitialSettleTimeoutMultiplier();
106 doReturn(FiniteDuration.apply(50, TimeUnit.MILLISECONDS)).when(shardElectionTimeout).duration();
107 try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorUtils, UNKNOWN_ID)) {
109 long start = System.currentTimeMillis();
111 distributedDataStore.waitTillReady();
113 long end = System.currentTimeMillis();
115 assertTrue("Expected to be blocked for 50 millis", end - start >= 50);
120 public void testWaitTillReadyCountDown() {
121 try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorUtils, UNKNOWN_ID)) {
122 doReturn(datastoreContext).when(actorUtils).getDatastoreContext();
123 doReturn(shardElectionTimeout).when(datastoreContext).getShardLeaderElectionTimeout();
124 doReturn(FiniteDuration.apply(5000, TimeUnit.MILLISECONDS)).when(shardElectionTimeout).duration();
126 Executors.newSingleThreadExecutor().submit(() -> {
127 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
128 distributedDataStore.readinessFuture().set(Empty.value());
131 long start = System.currentTimeMillis();
133 distributedDataStore.waitTillReady();
135 long end = System.currentTimeMillis();
137 assertTrue("Expected to be released in 500 millis", end - start < 5000);