Merge "Do not override jsr305 version"
[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.JavaTestKit;
25 import akka.testkit.TestActorRef;
26 import java.util.LinkedList;
27 import java.util.List;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * TestActorFactory provides methods to create both normal and test actors and to kill them when the factory is closed
33  * The ideal usage for TestActorFactory is with try with resources, <br/>
34  * For example <br/>
35  * <pre>
36  *     try (TestActorFactory factory = new TestActorFactory(getSystem())){
37  *         factory.createActor(props);
38  *         factory.createTestActor(props);
39  *         factory.generateActorId("leader-");
40  *     }
41  * </pre>
42  */
43 public class TestActorFactory implements AutoCloseable {
44     private final ActorSystem system;
45     List<ActorRef> createdActors = new LinkedList<>();
46     Logger LOG = LoggerFactory.getLogger(getClass());
47     private static int actorCount = 1;
48
49     public TestActorFactory(ActorSystem system){
50         this.system = system;
51     }
52
53     /**
54      * Create a normal actor with an auto-generated name
55      *
56      * @param props
57      * @return
58      */
59     public ActorRef createActor(Props props){
60         ActorRef actorRef = system.actorOf(props);
61         createdActors.add(actorRef);
62         return actorRef;
63     }
64
65     /**
66      * Create a normal actor with the passed in name
67      * @param props
68      * @param actorId name of actor
69      * @return
70      */
71     public ActorRef createActor(Props props, String actorId){
72         ActorRef actorRef = system.actorOf(props, actorId);
73         createdActors.add(actorRef);
74         return actorRef;
75     }
76
77     /**
78      * Create a test actor with the passed in name
79      * @param props
80      * @param actorId
81      * @param <T>
82      * @return
83      */
84     public <T extends Actor> TestActorRef<T> createTestActor(Props props, String actorId){
85         TestActorRef<T> actorRef = TestActorRef.create(system, props, actorId);
86         createdActors.add(actorRef);
87         return actorRef;
88     }
89
90     /**
91      * Create a test actor with an auto-generated name
92      * @param props
93      * @param <T>
94      * @return
95      */
96     public <T extends Actor> TestActorRef<T> createTestActor(Props props){
97         TestActorRef<T> actorRef = TestActorRef.create(system, props);
98         createdActors.add(actorRef);
99         return actorRef;
100     }
101
102     /**
103      * Generate a friendly but unique actor id/name
104      * @param prefix
105      * @return
106      */
107     public String generateActorId(String prefix){
108         return prefix + actorCount++;
109     }
110
111     @Override
112     public void close() {
113         new JavaTestKit(system) {{
114             for(ActorRef actor : createdActors) {
115                 watch(actor);
116                 LOG.info("Killing actor {}", actor);
117                 actor.tell(PoisonPill.getInstance(), ActorRef.noSender());
118                 expectTerminated(duration("5 seconds"), actor);
119             }
120         }};
121     }
122 }