BUG-5626: remove CompositeModification(ByteString)Payload
[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 akka.actor.ActorRef;
11 import akka.actor.ActorSystem;
12 import akka.pattern.Patterns;
13 import akka.testkit.JavaTestKit;
14 import akka.util.Timeout;
15 import com.google.common.util.concurrent.Uninterruptibles;
16 import java.util.Optional;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.TimeoutException;
19 import org.junit.Assert;
20 import org.opendaylight.controller.cluster.raft.client.messages.FindLeader;
21 import org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import scala.concurrent.Await;
25 import scala.concurrent.Future;
26 import scala.concurrent.duration.Duration;
27 import scala.concurrent.duration.FiniteDuration;
28
29 public class ShardTestKit extends JavaTestKit {
30     private static final Logger LOG = LoggerFactory.getLogger(ShardTestKit.class);
31
32     public ShardTestKit(ActorSystem actorSystem) {
33         super(actorSystem);
34     }
35
36     public void waitForLogMessage(final Class<?> logLevel, ActorRef subject, String logMessage){
37         // Wait for a specific log message to show up
38         final boolean result =
39             new JavaTestKit.EventFilter<Boolean>(logLevel
40             ) {
41                 @Override
42                 protected Boolean run() {
43                     return true;
44                 }
45             }.from(subject.path().toString())
46                 .message(logMessage)
47                 .occurrences(1).exec();
48
49         Assert.assertEquals(true, result);
50
51     }
52
53     public static String waitUntilLeader(ActorRef shard) {
54         FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS);
55         for(int i = 0; i < 20 * 5; i++) {
56             Future<Object> future = Patterns.ask(shard, FindLeader.INSTANCE, new Timeout(duration));
57             try {
58                 final Optional<String> maybeLeader = ((FindLeaderReply)Await.result(future, duration)).getLeaderActor();
59                 if (maybeLeader.isPresent()) {
60                     return maybeLeader.get();
61                 }
62             } catch(TimeoutException e) {
63                 LOG.trace("FindLeader timed out", e);
64             } catch(Exception e) {
65                 LOG.error("FindLeader failed", e);
66             }
67
68             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
69         }
70
71         Assert.fail("Leader not found for shard " + shard.path());
72         return null;
73     }
74
75     public void waitUntilNoLeader(ActorRef shard) {
76         FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS);
77         Object lastResponse = null;
78         for(int i = 0; i < 20 * 5; i++) {
79             Future<Object> future = Patterns.ask(shard, FindLeader.INSTANCE, new Timeout(duration));
80             try {
81                 final Optional<String> maybeLeader = ((FindLeaderReply)Await.result(future, duration)).getLeaderActor();
82                 if (!maybeLeader.isPresent()) {
83                     return;
84                 }
85
86                 lastResponse = maybeLeader.get();
87             } catch(TimeoutException e) {
88                 lastResponse = e;
89             } catch(Exception e) {
90                 LOG.error("FindLeader failed", e);
91                 lastResponse = e;
92             }
93
94             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
95         }
96
97         if(lastResponse instanceof Throwable) {
98             throw (AssertionError)new AssertionError(
99                     String.format("Unexpected error occurred from FindLeader for shard %s", shard.path())).
100                             initCause((Throwable)lastResponse);
101         }
102
103         Assert.fail(String.format("Unexpected leader %s found for shard %s", lastResponse, shard.path()));
104     }
105 }