2012-06-18 17 views
6

Tôi có hai bộ điều khiển sử dụng cả hai diễn viên AKKA trong Play 2.0. Theo đó, có hai trường hợp thử nghiệm kiểm tra hai API này. Tuy nhiên, khi thực hiện 'thử nghiệm chơi', chỉ một trong các trường hợp thử nghiệm sẽ thành công, trường hợp khác không thành công. Nếu tôi chạy chúng một cách riêng biệt, nó chạy thành công. Linh cảm của tôi là hệ thống diễn viên đã bị đóng cửa bởi thử nghiệm đầu tiên. Tuy nhiên, tôi mới chơi Play 2 và Akka, đây chỉ là phỏng đoán của tôi. Có cách giải quyết nào không?Tắt 2.0 hệ thống Akka khi có nhiều testcases hiện diện

@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"); 
      .... 
     } 
    }); 
} 

Hai bộ điều khiển như sau:

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>() { 
         ... 
         } 
       )); 
} 

AKKA Thạc sĩ Posta và người lao động:

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()); 
       } 
     } 
} 

Object Managed:

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

Bạn đã tìm thấy giải pháp chưa? Tôi có cùng một vấn đề trong 2.2.4 – Isammoc

+0

Cũng thấy các vấn đề tương tự - 2.3 mùa xuân akka sử dụng hệ thống diễn viên play2. – JasonG

+0

[lỗi] sbt.ForkMain $ ForkError: Lỗi khi tạo bean có tên 'accountServiceController' được định nghĩa trong tệp [/Users/admin/Development/src/totes/app/target/scala-2.11/classes/controllers/AccountServiceController.class]: Sự khởi tạo của đậu không thành công; ngoại lệ lồng nhau là org.springframework.beans.BeanInstantiationException: Không thể khởi tạo lớp bean [controllers.AccountServiceController]: Constructor đã ném ngoại lệ; ngoại lệ lồng nhau là java.lang.IllegalStateException: không thể tạo trẻ em khi chấm dứt hoặc chấm dứt – JasonG

Trả lời

1

Tôi không thấy vấn đề nữa không. Đây là cấu trúc của trường hợp thử nghiệm của tôi. Có lẽ bạn có thể thử nó và xem nó có hiệu quả với bạn hay không.

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() { 
    } 
} 

Ngoài ra, bạn có thể thử thêm cài đặt này để Build.scala của bạn:

parallelExecution in Test := false 
0

Hey Tôi đã sử dụng một ActorSystem tĩnh và điều này gây ra sự cố

[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 

Bằng cách tạo hệ thống ActorSystem động được tạo mỗi khi ứng dụng bắt đầu, sự cố dường như biến mất. Tôi đã làm việc này một thời gian vì vậy tôi muốn đăng bài này ở đây như một giải pháp tiềm năng.

+0

Trả lời từ nhà phát triển được đăng tại đây http://stackoverflow.com/a/20893193/1996639 – zella

Các vấn đề liên quan