Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardTestKit.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.fail;
12
13 import akka.actor.ActorRef;
14 import akka.actor.ActorSystem;
15 import akka.pattern.Patterns;
16 import akka.testkit.javadsl.EventFilter;
17 import akka.testkit.javadsl.TestKit;
18 import akka.util.Timeout;
19 import com.google.common.util.concurrent.Uninterruptibles;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22 import org.opendaylight.controller.cluster.raft.client.messages.FindLeader;
23 import org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import scala.concurrent.Await;
27 import scala.concurrent.Future;
28 import scala.concurrent.duration.FiniteDuration;
29
30 public class ShardTestKit extends TestKit {
31     private static final Logger LOG = LoggerFactory.getLogger(ShardTestKit.class);
32
33     public ShardTestKit(final ActorSystem actorSystem) {
34         super(actorSystem);
35     }
36
37     public void waitForLogMessage(final Class<?> logLevel, final ActorRef subject, final String logMessage) {
38         // Wait for a specific log message to show up
39         final Boolean result = new EventFilter(logLevel, getSystem()).from(subject.path().toString())
40                 .message(logMessage).occurrences(1).intercept(() -> Boolean.TRUE);
41         assertEquals(Boolean.TRUE, result);
42     }
43
44     @SuppressWarnings("checkstyle:IllegalCatch")
45     public static String waitUntilLeader(final ActorRef shard) {
46         FiniteDuration duration = FiniteDuration.create(100, TimeUnit.MILLISECONDS);
47         for (int i = 0; i < 20 * 5; i++) {
48             Future<Object> future = Patterns.ask(shard, FindLeader.INSTANCE, new Timeout(duration));
49             try {
50                 final var maybeLeader = ((FindLeaderReply) Await.result(future, duration)).getLeaderActor();
51                 if (maybeLeader.isPresent()) {
52                     return maybeLeader.orElseThrow();
53                 }
54             } catch (TimeoutException e) {
55                 LOG.trace("FindLeader timed out", e);
56             } catch (Exception e) {
57                 LOG.error("FindLeader failed", e);
58             }
59
60             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
61         }
62
63         fail("Leader not found for shard " + shard.path());
64         return null;
65     }
66
67     @SuppressWarnings("checkstyle:IllegalCatch")
68     public void waitUntilNoLeader(final ActorRef shard) {
69         FiniteDuration duration = FiniteDuration.create(100, TimeUnit.MILLISECONDS);
70         Object lastResponse = null;
71         for (int i = 0; i < 20 * 5; i++) {
72             Future<Object> future = Patterns.ask(shard, FindLeader.INSTANCE, new Timeout(duration));
73             try {
74                 final var maybeLeader = ((FindLeaderReply) Await.result(future, duration)).getLeaderActor();
75                 if (!maybeLeader.isPresent()) {
76                     return;
77                 }
78
79                 lastResponse = maybeLeader.orElseThrow();
80             } catch (TimeoutException e) {
81                 lastResponse = e;
82             } catch (Exception e) {
83                 LOG.error("FindLeader failed", e);
84                 lastResponse = e;
85             }
86
87             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
88         }
89
90         if (lastResponse instanceof Throwable) {
91             throw (AssertionError)new AssertionError(
92                     String.format("Unexpected error occurred from FindLeader for shard %s", shard.path()))
93                             .initCause((Throwable)lastResponse);
94         }
95
96         fail(String.format("Unexpected leader %s found for shard %s", lastResponse, shard.path()));
97     }
98 }