Merge "(Fix: Bug 2711) - Match RPC actor system's Netty TCP settings with existing...
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / TestActorFactory.java
1 /*
2  * Copyright (c) 2014 Cisco 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
9 package org.opendaylight.controller.cluster.raft;
10
11 /*
12  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
13  *
14  * This program and the accompanying materials are made available under the
15  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
16  * and is available at http://www.eclipse.org/legal/epl-v10.html
17  */
18
19 import akka.actor.Actor;
20 import akka.actor.ActorRef;
21 import akka.actor.ActorSystem;
22 import akka.actor.PoisonPill;
23 import akka.actor.Props;
24 import akka.testkit.TestActorRef;
25 import java.util.LinkedList;
26 import java.util.List;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * TestActorFactory provides methods to create both normal and test actors and to kill them when the factory is closed
32  * The ideal usage for TestActorFactory is with try with resources, <br/>
33  * For example <br/>
34  * <pre>
35  *     try (TestActorFactory factory = new TestActorFactory(getSystem())){
36  *         factory.createActor(props);
37  *         factory.createTestActor(props);
38  *         factory.generateActorId("leader-");
39  *     }
40  * </pre>
41  */
42 public class TestActorFactory implements AutoCloseable {
43     private final ActorSystem system;
44     List<ActorRef> createdActors = new LinkedList<>();
45     Logger LOG = LoggerFactory.getLogger(getClass());
46     private static int actorCount = 1;
47
48     public TestActorFactory(ActorSystem system){
49         this.system = system;
50     }
51
52     /**
53      * Create a normal actor with an auto-generated name
54      *
55      * @param props
56      * @return
57      */
58     public ActorRef createActor(Props props){
59         ActorRef actorRef = system.actorOf(props);
60         createdActors.add(actorRef);
61         return actorRef;
62     }
63
64     /**
65      * Create a normal actor with the passed in name
66      * @param props
67      * @param actorId name of actor
68      * @return
69      */
70     public ActorRef createActor(Props props, String actorId){
71         ActorRef actorRef = system.actorOf(props, actorId);
72         createdActors.add(actorRef);
73         return actorRef;
74     }
75
76     /**
77      * Create a test actor with the passed in name
78      * @param props
79      * @param actorId
80      * @param <T>
81      * @return
82      */
83     public <T extends Actor> TestActorRef<T> createTestActor(Props props, String actorId){
84         TestActorRef<T> actorRef = TestActorRef.create(system, props, actorId);
85         createdActors.add(actorRef);
86         return actorRef;
87     }
88
89     /**
90      * Create a test actor with an auto-generated name
91      * @param props
92      * @param <T>
93      * @return
94      */
95     public <T extends Actor> TestActorRef<T> createTestActor(Props props){
96         TestActorRef<T> actorRef = TestActorRef.create(system, props);
97         createdActors.add(actorRef);
98         return actorRef;
99     }
100
101     /**
102      * Generate a friendly but unique actor id/name
103      * @param prefix
104      * @return
105      */
106     public String generateActorId(String prefix){
107         return prefix + actorCount++;
108     }
109
110     @Override
111     public void close() throws Exception {
112         for(ActorRef actor : createdActors){
113             LOG.info("Killing actor {}", actor);
114             actor.tell(PoisonPill.getInstance(), null);
115         }
116     }
117 }