액터는 일반적으로 비동기적으로 작동이되며, 메시지 설계도 기다림이 없어야합니다.

이러한 메시지 확인을 위해 결과값을 기다리게 하는것이 일반적이지만, 우리의 서비스는 비동기적으로 계속 작동되어야합니다.

Akka의 TestKit은 유닛 테스트시 메시지를 검사하는 툴을 제공하며, 우리가 설계한 액터를 멈추지 않고 순차적인 검사를 지원합니다.


TestCase


TestCode

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.testkit.javadsl.TestKit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import java.time.Duration;

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = AppConfiguration.class)
@SuppressWarnings("Duplicates")
public class SessionTest {

    static ActorSystem system;

    @BeforeClass
    public static void setup() {
        system = ActorSystem.create();
    }

    @AfterClass
    public static void teardown() {
        TestKit.shutdownActorSystem(system);
        system = null;
    }

    @Test
    public void testIt() {
        new TestKit(system) {{
            final ActorRef lobbyActor = system.actorOf(LobbyActor.props(),"lobby");
            final String testSessionID = "jaskfjkjaslfalsf";

            // Create TableActor
            for(int i=0;i<10;i++){
                lobbyActor.tell( new TableCreate(i,"table-"+i , TableCreate.Cmd.CREATE),getRef() );
                expectMsg(Duration.ofSeconds(1), "created");
            }

            // Try Connect
            lobbyActor.tell(new ConnectInfo(testSessionID, null,ConnectInfo.Cmd.CONNECT),getRef());
            expectMsg(Duration.ofSeconds(1), "done");

            // Find User
            lobbyActor.tell(new ConnectInfo(testSessionID, null,ConnectInfo.Cmd.FIND),getRef());
            expectMsg(Duration.ofSeconds(1), "User exists");

            // Join Table : Forward Check , lobby->table->game->getRef()
            lobbyActor.tell(new JoinGame(1,"test",testSessionID),getRef() );
            expectMsg(Duration.ofSeconds(1), "joined");

            // Try Disconnect
            lobbyActor.tell(new ConnectInfo(testSessionID, null,ConnectInfo.Cmd.DISCONET),getRef());
            expectMsg(Duration.ofSeconds(3), "done");

            // Find Again
            lobbyActor.tell(new ConnectInfo(testSessionID, null,ConnectInfo.Cmd.FIND),getRef());
            expectMsg(Duration.ofSeconds(1), "User does not exist");
        }};
    }
}

  • No labels