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 try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext)) {
55 distributedDataStore.newReadWriteTransaction();
57 verify(actorContext, times(1)).acquireTxCreationPermit();
62 public void testRateLimitingUsedInWriteOnlyTxCreation(){
63 try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext)) {
65 distributedDataStore.newWriteOnlyTransaction();
67 verify(actorContext, times(1)).acquireTxCreationPermit();
72 public void testRateLimitingNotUsedInReadOnlyTxCreation(){
73 try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext)) {
75 distributedDataStore.newReadOnlyTransaction();
76 distributedDataStore.newReadOnlyTransaction();
77 distributedDataStore.newReadOnlyTransaction();
79 verify(actorContext, times(0)).acquireTxCreationPermit();
84 public void testWaitTillReadyBlocking(){
85 doReturn(datastoreContext).when(actorContext).getDatastoreContext();
86 doReturn(shardElectionTimeout).when(datastoreContext).getShardLeaderElectionTimeout();
87 doReturn(FiniteDuration.apply(50, TimeUnit.MILLISECONDS)).when(shardElectionTimeout).duration();
88 try (DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext)) {
90 long start = System.currentTimeMillis();
92 distributedDataStore.waitTillReady();
94 long end = System.currentTimeMillis();
96 assertTrue("Expected to be blocked for 50 millis", (end - start) >= 50);
101 public void testWaitTillReadyCountDown(){
102 try (final DistributedDataStore distributedDataStore = new DistributedDataStore(actorContext)) {
103 doReturn(datastoreContext).when(actorContext).getDatastoreContext();
104 doReturn(shardElectionTimeout).when(datastoreContext).getShardLeaderElectionTimeout();
105 doReturn(FiniteDuration.apply(5000, TimeUnit.MILLISECONDS)).when(shardElectionTimeout).duration();
107 Executors.newSingleThreadExecutor().submit(new Runnable() {
110 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
111 distributedDataStore.getWaitTillReadyCountDownLatch().countDown();
115 long start = System.currentTimeMillis();
117 distributedDataStore.waitTillReady();
119 long end = System.currentTimeMillis();
121 assertTrue("Expected to be released in 500 millis", (end - start) < 5000);