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.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;
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);
37 private SchemaContext schemaContext;
40 private ActorContext actorContext;
43 private DatastoreContext datastoreContext;
46 private Timeout shardElectionTimeout;
49 public void setUp() throws Exception {
50 MockitoAnnotations.initMocks(this);
52 schemaContext = TestModel.createTestContext();
54 doReturn(schemaContext).when(actorContext).getSchemaContext();
55 doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
59 public void testRateLimitingUsedInReadWriteTxCreation() {
60 try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext, UNKNOWN_ID)) {
62 distributedDataStore.newReadWriteTransaction();
64 verify(actorContext, times(1)).acquireTxCreationPermit();
69 public void testRateLimitingUsedInWriteOnlyTxCreation() {
70 try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext, UNKNOWN_ID)) {
72 distributedDataStore.newWriteOnlyTransaction();
74 verify(actorContext, times(1)).acquireTxCreationPermit();
79 public void testRateLimitingNotUsedInReadOnlyTxCreation() {
80 try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext, UNKNOWN_ID)) {
82 distributedDataStore.newReadOnlyTransaction();
83 distributedDataStore.newReadOnlyTransaction();
84 distributedDataStore.newReadOnlyTransaction();
86 verify(actorContext, times(0)).acquireTxCreationPermit();
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)) {
97 long start = System.currentTimeMillis();
99 distributedDataStore.waitTillReady();
101 long end = System.currentTimeMillis();
103 assertTrue("Expected to be blocked for 50 millis", end - start >= 50);
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();
114 Executors.newSingleThreadExecutor().submit(() -> {
115 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
116 distributedDataStore.getWaitTillReadyCountDownLatch().countDown();
119 long start = System.currentTimeMillis();
121 distributedDataStore.waitTillReady();
123 long end = System.currentTimeMillis();
125 assertTrue("Expected to be released in 500 millis", end - start < 5000);