@Test
  public void shouldNotAttemptReQueueIfNoTestHasBeenPulled() {
    final Collection<String> additions = newLinkedList();
    Queue<String> queue =
        new LinkedList<String>() {
          private static final long serialVersionUID = -1L;

          @Override
          public boolean add(String o) {
            return additions.add(o);
          }
        };
    QueueProcessor processor = createMock(QueueProcessor.class);
    processor.close();
    replay(processor);

    ProcessorRunnable runnable =
        new ProcessorRunnable(
            queue, processor, null, 1, createNiceMock(ConcurrencyController.class));
    Thread.currentThread().interrupt();
    runnable.run();
    assertTrue(additions.isEmpty());
    verify(processor);
  }
  // Call when not tracking state
  @Test
  public void testRunNotTracking() {
    sLogger.setWriter(new StringWriter());
    sLogger.start();
    assertTrue(sLogger.isRunning());
    ArrayList<StateGroup> grouplist = new ArrayList<>();
    grouplist.add(group);

    resetAll();
    expect(mockTracker.isTrackingState()).andReturn(false).anyTimes();
    expect(mockTracker.getIoSet()).andReturn(ioSet).anyTimes();
    expect(mockTracker.getStateList()).andReturn(grouplist).anyTimes();
    replayAll();

    Thread t =
        new Thread(
            new Runnable() {

              @Override
              public void run() {
                try {
                  Thread.sleep(2500);
                } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                }

                sLogger.stop();
              }
            });
    t.start();
    sLogger.run();
  }
 public void testConnectToServerTimeout() throws Exception {
   IoFilter filter = createNiceMock(IoFilter.class);
   SimpleClient client = new SimpleClient(filter, "10.0.0.1", 3000);
   client.sendMessageToServer(new Object());
   Jedis jedis = JedisFactory.getJedis();
   Thread.sleep(3000);
   assertTrue(jedis.exists("client_timeout_map:10.0.0.1:3000"));
   Thread.sleep(2000);
   assertFalse(jedis.exists("client_timeout_map:10.0.0.1:3000"));
 }
  @Test
  public void testRoleAttackWithoutFeedback() throws Exception {

    UserManager manager = UserManager.getInstance();
    manager.removeUser("test001");
    manager.removeUser("test002");

    User user1 = manager.createDefaultUser();
    user1.set_id(new UserId("test001"));
    // user1.setSessionKey(SessionKey.createSessionKeyFromRandomString());
    user1.setUsername("test001");
    for (int i = 0; i < 1; i++) {
      user1.addTool(makeBuffTool(i));
    }
    user1.setBag(makeBag(user1, 3));

    User user2 = manager.createDefaultUser();
    user2.set_id(new UserId("test002"));
    // user2.setSessionKey(SessionKey.createSessionKeyFromRandomString());
    user2.setUsername("test002");
    for (int i = 0; i < 1; i++) {
      user2.addTool(makeBuffTool(i));
    }
    user2.setBag(makeBag(user2, 3));

    manager.saveUser(user1, true);
    manager.saveUserBag(user1, true);
    manager.saveUser(user2, true);
    manager.saveUserBag(user2, true);

    SessionManager sessionManager = new SessionManager();

    IoSession session1 = createNiceMock(IoSession.class);
    sessionManager.registerSession(session1, user1);

    IoSession session2 = createNiceMock(IoSession.class);
    sessionManager.registerSession(session2, user2);

    BattleManager battleManager = BattleManager.getInstance();
    battleManager.clearAllBattles();

    // Create two single room
    RoomManager roomManager = RoomManager.getInstance();
    roomManager.assignRoom(user1, RoomType.SINGLE_ROOM);
    roomManager.assignRoom(user2, RoomType.SINGLE_ROOM);
    // User1 and User2 press "ready start"
    roomManager.readyStart(user1.getSessionKey(), true);
    roomManager.readyStart(user2.getSessionKey(), true);

    Collection<Battle> battles = battleManager.findAllBattles();

    // Thread.sleep(Long.MAX_VALUE);
    assertEquals(1, battles.size());

    Battle battle = battles.iterator().next();
    // Change the battleMap
    ArrayList<BattleBitSetMap> battleMaps = BattleDataLoader4Bitmap.getBattleMapList();
    BattleBitSetMap map03 = null;
    for (BattleBitSetMap bm : battleMaps) {
      if (bm.getMapId().equals("0")) {
        map03 = bm;
        break;
      }
    }
    assertNotNull(map03);
    TestUtil.setPrivateFieldValue("battleMap", battle, map03);

    HashMap<SessionKey, BattleUser> battleUserMap = battle.getBattleUserMap();
    assertEquals(2, battleUserMap.size());

    BattleUser bUser1 = battleUserMap.get(user1.getSessionKey());
    BattleUser bUser2 = battleUserMap.get(user2.getSessionKey());

    assertEquals(user1.getSessionKey(), bUser1.getUserSessionKey());
    assertEquals(user1.getBlood(), bUser1.getBlood());
    assertEquals(user1.getTkew(), bUser1.getThew());

    Jedis jedis = JedisFactory.getJedis();

    // User1 and User2 are stage ready.
    // Test roundStart
    final ArrayList<Message> msgList = new ArrayList<Message>();
    MessageQueue queue = createMock(MessageQueue.class);
    queue.sessionWrite(anyObject(SessionKey.class), anyObject());
    expectLastCall()
        .andAnswer(
            new IAnswer<Object>() {
              public Object answer() throws Throwable {
                Object msg = getCurrentArguments()[1];
                msgList.add((Message) msg);
                return null;
              }
            })
        .anyTimes();
    TestUtil.setPrivateFieldValue("messageQueue", GameContext.getInstance(), queue);
    replay(queue);

    battleManager.stageReady(user1.getSessionKey());
    battleManager.stageReady(user2.getSessionKey());

    // System.out.println(msgList);
    // Check if roundStart is called
    assertEquals(2, msgList.size());
    BseRoundStart roundStart = (BseRoundStart) msgList.get(0);
    assertNotNull(roundStart.getUserId(0));
    assertNotNull(roundStart.getUserId(1));
    // userPos: (990, 37), angle: 93, power: 83, dir: 1
    // Role Attack
    msgList.clear();
    BceRoleAttack.Builder bceRoleAttack = BceRoleAttack.newBuilder();
    bceRoleAttack.setAngle(150000);
    bceRoleAttack.setAtkAngle(bceRoleAttack.getAngle());
    bceRoleAttack.setDirection(RoleDirection.RIGHT.ordinal());
    bceRoleAttack.setPower(30);
    int userx = 1380;
    int usery = 697;
    bceRoleAttack.setUserx(userx);
    bceRoleAttack.setUsery(usery);
    bUser1.setPosX(userx);
    bUser1.setPosY(usery);
    bUser2.setPosX(userx);
    bUser2.setPosY(usery);

    battleManager.roleAttack(bUser1.getUserSessionKey(), bceRoleAttack.build());

    // System.out.println(msgList);
    // 2 roleAttack messages, 2 roundStart messages.
    long timeOut = System.currentTimeMillis() + 10000;
    Message xinqi = null;
    while (System.currentTimeMillis() < timeOut) {
      int size = msgList.size();
      for (int i = 0; i < size; i++) {
        Object xm = msgList.get(i);
        if (xm instanceof BseAskRoundOver) {
          xinqi = (Message) xm;
          break;
        }
      }
      Thread.sleep(200);
    }
    assertTrue(xinqi != null);

    verify(queue);

    // Clean system.
    jedis.del(battle.getBattleSessionKey().toString());
    jedis.del(user1.getSessionKey().toString());
    jedis.del(user2.getSessionKey().toString());
  }
 @After
 public void cleanup() {
   // Clear interrupted state
   Thread.interrupted();
 }