2 * Copyright (c) 2016 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
8 package org.opendaylight.controller.cluster.datastore;
10 import akka.actor.ActorSystem;
11 import akka.testkit.javadsl.TestKit;
12 import com.typesafe.config.ConfigFactory;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.concurrent.CompletionStage;
16 import java.util.concurrent.atomic.AtomicLong;
17 import org.junit.After;
18 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
19 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
20 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
21 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
22 import org.opendaylight.controller.cluster.access.concepts.MemberName;
23 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
24 import scala.compat.java8.FutureConverters;
25 import scala.concurrent.Await;
26 import scala.concurrent.duration.Duration;
28 public abstract class AbstractTest {
29 protected static final MemberName MEMBER_NAME = MemberName.forName("member-1");
30 protected static final MemberName MEMBER_2_NAME = MemberName.forName("member-2");
32 private static final FrontendType FRONTEND_TYPE = FrontendType.forName(ShardTransactionTest.class.getSimpleName());
34 protected static final FrontendIdentifier FRONTEND_ID = FrontendIdentifier.create(MEMBER_NAME, FRONTEND_TYPE);
36 private static final ClientIdentifier CLIENT_ID = ClientIdentifier.create(FRONTEND_ID, 0);
37 private static final LocalHistoryIdentifier HISTORY_ID = new LocalHistoryIdentifier(CLIENT_ID, 0);
38 private static final AtomicLong HISTORY_COUNTER = new AtomicLong();
39 private static final AtomicLong TX_COUNTER = new AtomicLong();
41 private final Collection<ActorSystem> actorSystems = new ArrayList<>();
43 protected static void setUpStatic() {
44 HISTORY_COUNTER.set(1L);
48 protected static TransactionIdentifier nextTransactionId() {
49 return new TransactionIdentifier(HISTORY_ID, TX_COUNTER.getAndIncrement());
52 protected static LocalHistoryIdentifier nextHistoryId() {
53 return new LocalHistoryIdentifier(CLIENT_ID, HISTORY_COUNTER.incrementAndGet());
56 protected static <T> T waitOnAsyncTask(final CompletionStage<T> completionStage, final Duration timeout)
58 return Await.result(FutureConverters.toScala(completionStage), timeout);
62 public void actorSystemCleanup() {
63 for (final ActorSystem system : actorSystems) {
64 TestKit.shutdownActorSystem(system, Boolean.TRUE);
68 protected ActorSystem newActorSystem(final String name, final String config) {
69 ActorSystem system = ActorSystem.create(name, ConfigFactory.load().getConfig(config));
70 actorSystems.add(system);