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;
15 import akka.util.Timeout;
16 import com.google.common.util.concurrent.Uninterruptibles;
17 import java.util.concurrent.Executors;
18 import java.util.concurrent.TimeUnit;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
24 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import scala.concurrent.duration.FiniteDuration;
28 public class DistributedDataStoreTest extends AbstractActorTest {
30 private SchemaContext schemaContext;
33 private ActorContext actorContext;
36 private DatastoreContext datastoreContext;
39 private Timeout shardElectionTimeout;
42 public void setUp() throws Exception {
43 MockitoAnnotations.initMocks(this);
45 schemaContext = TestModel.createTestContext();
47 doReturn(schemaContext).when(actorContext).getSchemaContext();
48 doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
52 public void testRateLimitingUsedInReadWriteTxCreation(){
53 DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext);
55 distributedDataStore.newReadWriteTransaction();
57 verify(actorContext, times(1)).acquireTxCreationPermit();
61 public void testRateLimitingUsedInWriteOnlyTxCreation(){
62 DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext);
64 distributedDataStore.newWriteOnlyTransaction();
66 verify(actorContext, times(1)).acquireTxCreationPermit();
71 public void testRateLimitingNotUsedInReadOnlyTxCreation(){
72 DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext);
74 distributedDataStore.newReadOnlyTransaction();
75 distributedDataStore.newReadOnlyTransaction();
76 distributedDataStore.newReadOnlyTransaction();
78 verify(actorContext, times(0)).acquireTxCreationPermit();
82 public void testWaitTillReadyBlocking(){
83 doReturn(datastoreContext).when(actorContext).getDatastoreContext();
84 doReturn(shardElectionTimeout).when(datastoreContext).getShardLeaderElectionTimeout();
85 doReturn(FiniteDuration.apply(50, TimeUnit.MILLISECONDS)).when(shardElectionTimeout).duration();
86 DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext);
88 long start = System.currentTimeMillis();
90 distributedDataStore.waitTillReady();
92 long end = System.currentTimeMillis();
94 assertTrue("Expected to be blocked for 50 millis", (end-start) >= 50);
98 public void testWaitTillReadyCountDown(){
99 final DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext);
100 doReturn(datastoreContext).when(actorContext).getDatastoreContext();
101 doReturn(shardElectionTimeout).when(datastoreContext).getShardLeaderElectionTimeout();
102 doReturn(FiniteDuration.apply(5000, TimeUnit.MILLISECONDS)).when(shardElectionTimeout).duration();
104 Executors.newSingleThreadExecutor().submit(new Runnable() {
107 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
108 distributedDataStore.getWaitTillReadyCountDownLatch().countDown();
112 long start = System.currentTimeMillis();
114 distributedDataStore.waitTillReady();
116 long end = System.currentTimeMillis();
118 assertTrue("Expected to be released in 500 millis", (end-start) < 5000);