2012-06-18 4 views
6

Ho due controller che utilizzano entrambi gli attori AKKA in Play 2.0. Di conseguenza, ci sono due casi di test che testano queste due API. Tuttavia, quando si esegue "play test", solo uno dei casi di test avrà esito positivo, l'altro fallirà. Se li eseguo separatamente, viene eseguito correttamente. La mia impressione è che il sistema dell'attore sia stato chiuso al primo test. Comunque, sono nuovo per Play 2 e Akka, questa è solo una mia ipotesi. C'è una soluzione?Play 2.0 Arresto del sistema Akka quando sono presenti più testicoli

@Test 
public void callPostA() { 
running(testServer(2222, fakeApplication(inMemoryDatabase())), new Runnable() { 
     @Override 
     public void run() { 
      HttpPost httpPost = new HttpPost("http://localhost:2222/controllera"); 
      .... 
     } 
    }); 
} 
@Test 
public void callPostB() { 
running(testServer(2222, fakeApplication(inMemoryDatabase())), new Runnable() { 
     @Override 
     public void run() { 
      HttpPost httpPost = new HttpPost("http://localhost:2222/controllerb"); 
      .... 
     } 
    }); 
} 

due controller sono i seguenti:

public class PostA extends Controller { 
    // master actor for workers 
    public static ActorRef masterActorA = Akka.system().actorOf(new Props(new UntypedActorFactory() { 
    public UntypedActor create() { 
      return new PostAActorMaster(Config.NUMBER_OF_WORKER_ACTOR); 
     } 
     }), "PostAActorMaster"); 

    public static Result postA() { 

     Map<String, String[]> dict = body.asFormUrlEncoded(); 
     String paramField1 = dict.get("paramField1"); 
     String paramField2 = dict.get("paramField2"); 

     ProductInfo pInfo = new ProductInfo(paramField1, paramField2); 
     ProductMessage pMessage = new ProductMessage(pInfo); 
     return async(
     Akka.asPromise(ask(masterActorA, pMessage, 15000)).map(
      new Function<Object, Result>() { 
         ... 
         } 
       )); 
} 

public class PostB extends Controller { 
    // master actor for workers 
    public static ActorRef masterActorB = Akka.system().actorOf(new Props(new UntypedActorFactory() { 
    public UntypedActor create() { 
      return new PostBActorMaster(Config.NUMBER_OF_WORKER_ACTOR); 
     } 
     }), "PostBActorMaster"); 

    public static Result postB() { 

     Map<String, String[]> dict = body.asFormUrlEncoded(); 
     String paramField3 = dict.get("paramField3"); 
     String paramField4 = dict.get("paramField4"); 

     BillInfo bInfo = new BillInfo(paramField3, paramField4); 
     BillMessage pMessage = new BillMessage(bInfo); 
     return async(
     Akka.asPromise(ask(masterActorB, pMessage, 15000)).map(
      new Function<Object, Result>() { 
         ... 
         } 
       )); 
} 

di Posta AKKA Maestro e dei lavoratori:

public class PostAActorMaster extends UntypedActor { 

    private final ActorRef workerRouter; 

    public PostAActorMaster(final int nrOfWorkers) { 
     workerRouter = this.getContext().actorOf(new Props(PostAActorMaster.class).withRouter(new RoundRobinRouter(nrOfWorkers))); 
    } 

    public void onReceive(Object messageObj) { 
      try { 
      if (messageObj instanceof ProductMessage) { 
       // invoke worker to submit channel messaages 
       workerRouter.tell(messageObj, getSender()); 
       } else if (messageObj instanceof ProductMessageResult) { 
        ...... 
        getSender().tell("OK"); 
       } 
      } catch (Exception e) { 
       ...... 
      } 
    } 

} 


public class PostAActorWorker extends UntypedActor { 
    public void onReceive(Object messageObj) throws Exception { 
       if (messageObj instanceof ProductMessage) { 
        ProductMessage pMessage = (ProductMessage)messageObj; 
        ProductInfo pInfo = pMessage.getProductInfo(); 
        log.info(pInfo.getProductId()); 
        ProductMessageResult pr = new ProductMessageResult(pInfo); 
       PostA.masterActor.tell(pr, getSender()); 
       } 
     } 
} 

Managed Object:

public class ProductInfo extends Model { 
     @Id 
     private String productId; 
     ... 
    } 
+0

Hai trovato una soluzione? Ho lo stesso problema in 2.2.4 – Isammoc

+0

Ho visto anche problemi simili - 2.3 akka spring che usano il sistema di attore play2. – JasonG

+0

[errore] sbt.ForkMain $ ForkError: errore nella creazione di bean con nome 'accountServiceController' definito nel file [/Users/admin/Development/src/totes/app/target/scala-2.11/classes/controllers/AccountServiceController.class]: L'istanziazione del bean è fallita; l'eccezione annidata è org.springframework.beans.BeanInstantiationException: impossibile istanziare la classe bean [controller.AccountServiceController]: il costruttore ha lanciato un'eccezione; l'eccezione nidificata è java.lang.IllegalStateException: impossibile creare figli mentre si termina o si termina – JasonG

risposta

1

non vedo il problema più. Ecco la struttura del mio caso di test. Forse puoi provarlo e vedere se funziona per te.

Scala:

object LoginApiTest extends PlaySpecification { 
    "login api quick login" should { 
    "post login data" in new WithCleanTestData { 
     var org1 = new OrgInfo("testorg", "Test Inc"); 
     org1.save(); 
    } 
    } 
} 


abstract class WithCleanTestData extends WithApplication(FakeApplication(
    additionalConfiguration = TestConf.getConf.toMap 
)) { 
    override def around[T: AsResult](t: => T): Result = super.around { 
    prepareDbWithData() 
    t 
    } 
    def prepareDbWithData() = { 
     OrgInfo.getAllOrgInfo.foreach(_.delete) 
    } 
} 

Java:

public class MyHelpers extends Helpers { 
    public static FakeApplication myFakeApplication(Map<String,String> additionalConfiguration) { 
     List<String> withoutPlugins = new ArrayList<String>(); 
     List<String> additionalPlugins = new ArrayList<String>(); 
     return new MyFakeApplication(new java.io.File("."), MyHelpers.class.getClassLoader(), 
      additionalConfiguration, withoutPlugins, additionalPlugins, null); 
    } 
} 

public class BaseModelTest extends WithApplication { 
    @Before 
    public void before() { 
    } 
} 

public class PostTest extends BaseModelTest { 

    @Test 
    public void test1() { 
    } 
} 

Inoltre, è possibile provare aggiungere questa impostazione al vostro Build.scala:

parallelExecution in Test := false 
0

Hey Io sto usando un ActorSystem statica e questo ha causato un problema

[error] sbt.ForkMain$ForkError: Error creating bean with name 'accountServiceController' defined in file [/Users/admin/Development/src/totes/app/target/scala-2.11/classes/controllers/Ac‌​countServiceController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [controllers.AccountServiceController]: Constructor threw exception; nested exception is java.lang.IllegalStateException: cannot create children while terminating or terminated 

Effettuando un ActorSystem dinamico che viene creato ogni volta che l'app viene avviata, il problema sembra scomparire. Ho lavorato su questo per un po 'quindi volevo postarlo qui come una potenziale soluzione.

+0

Risposta dagli sviluppatori pubblicati qui http://stackoverflow.com/a/20893193/1996639 – zella

Problemi correlati