예제 #1
0
 @After
 public void shutdown() throws InterruptedException {
   try {
     client1.disconnect();
   } catch (IOException e) {
   }
   server.stop();
 }
예제 #2
0
  @Test(timeout = 20000)
  public void testUserOnlineOffline()
      throws InterruptedException, UnknownHostException, IOException {
    TestClient client2 = createTestClient();
    client2.send(new LoginMessage("Test2"));
    client2.await(WelcomeMessage.class);
    client2.await(ChatMessage.class);

    UserStatusMessage statusMessage = client1.await(UserStatusMessage.class);
    ChatMessage chat = client1.await(ChatMessage.class);
    String message = chat.getMessage();
    assertTrue(
        "Unexpected message: " + message,
        message.contains(client2.getName()) && message.contains("joined"));

    int client2id = statusMessage.getUserId();
    assertEquals(Status.ONLINE, statusMessage.getStatus());
    assertEquals(server.getClients().size() + 1, client2id);
    assertEquals(client2.getName(), statusMessage.getName());

    client2.send(new ServerQueryMessage(Request.USERS));
    client2.await(AvailableModsMessage.class);
    List<UserStatusMessage> users = client2.awaitMany(6, UserStatusMessage.class);
    System.out.println("Online users: " + users);

    // There is no determined order in which the UserStatusMessages are received, so it is harder to
    // make any assertions.
    assertUserFound(users, client1.getName());
    assertUserFound(users, client2.getName());
    assertUserFound(users, "AI Fighter");
    assertUserFound(users, "AI Loser");
    assertUserFound(users, "AI Medium");
    assertUserFound(users, "AI Idiot");

    client2.disconnect();

    System.out.println(chat);
    statusMessage = client1.await(UserStatusMessage.class);
    assertEquals(Status.OFFLINE, statusMessage.getStatus());
    assertEquals(client2id, statusMessage.getUserId());
    assertEquals(client2.getName(), statusMessage.getName());
  }