public void testGetNumConnectedClients() {
    final long timeout = System.currentTimeMillis() + 15;
    JUnitMessageQueue queue = createQueue(15, 1, 1);
    assertEquals(0, queue.getNumConnectedClients());

    // Add some clients in a few ways.
    {
      queue.getTestBlock(createClientInfo(0, "ie6"), 0, timeout);
      queue.reportFatalLaunch(createClientInfo(1, "gecko"), null);
      queue.reportResults(createClientInfo(2, "safari"), createTestResults(0));
      assertEquals(3, queue.getNumConnectedClients());
    }

    // Add duplicate clients.
    {
      queue.getTestBlock(createClientInfo(3, "ie6"), 0, timeout);
      queue.reportFatalLaunch(createClientInfo(3, "ie6"), null);
      queue.reportResults(createClientInfo(4, "safari"), createTestResults(0));
      assertEquals(5, queue.getNumConnectedClients());
    }

    // Add existing clients.
    {
      queue.getTestBlock(createClientInfo(0, "ie6"), 0, timeout);
      queue.reportFatalLaunch(createClientInfo(1, "gecko"), null);
      queue.reportResults(createClientInfo(2, "safari"), createTestResults(0));
      assertEquals(5, queue.getNumConnectedClients());
    }
  }
  public void testGetTestBlock() {
    final long timeout = System.currentTimeMillis() + 15;
    JUnitMessageQueue queue = createQueue(15, 2, 3);
    TestInfo[] testBlock0 = queue.getTestBlocks().get(0);
    TestInfo[] testBlock1 = queue.getTestBlocks().get(1);

    // Get the first test block.
    {
      TestBlock block = queue.getTestBlock(createClientInfo(0, "ie6"), 0, timeout);
      assertEquals(testBlock0, block.getTests());
      assertEquals(0, block.getIndex());
    }

    // Get the second test block.
    {
      TestBlock block = queue.getTestBlock(createClientInfo(0, "ie6"), 1, timeout);
      assertEquals(testBlock1, block.getTests());
      assertEquals(1, block.getIndex());
    }

    // Get the third test block.
    {
      assertNull(queue.getTestBlock(createClientInfo(0, "ie6"), 2, timeout));
    }
  }
  public void testNewClients() {
    final long timeout = System.currentTimeMillis() + 15;
    JUnitMessageQueue queue = createQueue(15, 1, 1);
    assertEquals(0, queue.getNewClients().length);

    // Add some clients in a few ways.
    {
      queue.getTestBlock(createClientInfo(0, "ie6"), 0, timeout);
      queue.reportFatalLaunch(createClientInfo(1, "gecko"), null);
      queue.reportResults(createClientInfo(2, "safari"), createTestResults(0));
      assertSimilar(new String[] {"desc0", "desc1", "desc2"}, queue.getNewClients());
      assertEquals(0, queue.getNewClients().length);
    }

    // Add duplicate clients.
    {
      queue.getTestBlock(createClientInfo(3, "ie6"), 0, timeout);
      queue.reportFatalLaunch(createClientInfo(3, "ie6"), null);
      queue.reportResults(createClientInfo(4, "safari"), createTestResults(0));
      queue.getTestBlock(createClientInfo(3, "ie6"), 0, timeout);
      assertSimilar(new String[] {"desc3", "desc4"}, queue.getNewClients());
      assertEquals(0, queue.getNewClients().length);
    }

    // Add existing clients.
    {
      queue.getTestBlock(createClientInfo(0, "ie6"), 0, timeout);
      queue.reportFatalLaunch(createClientInfo(1, "gecko"), null);
      queue.reportResults(createClientInfo(2, "safari"), createTestResults(0));
      assertEquals(0, queue.getNewClients().length);
    }
  }
  public void testGetNumRetrievedClients() {
    final long timeout = System.currentTimeMillis() + 15;
    JUnitMessageQueue queue = createQueue(15, 2, 3);
    TestInfo[] testBlock0 = queue.getTestBlocks().get(0);
    TestInfo test0_0 = testBlock0[0];
    TestInfo test0_1 = testBlock0[1];
    TestInfo test0_2 = testBlock0[2];
    TestInfo[] testBlock1 = queue.getTestBlocks().get(1);
    TestInfo test1_0 = testBlock1[0];
    TestInfo test1_1 = testBlock1[1];
    TestInfo test1_2 = testBlock1[2];
    assertEquals(0, queue.getNumClientsRetrievedTest(test0_0));
    assertEquals(0, queue.getNumClientsRetrievedTest(test0_1));
    assertEquals(0, queue.getNumClientsRetrievedTest(test0_2));
    assertEquals(0, queue.getNumClientsRetrievedTest(test1_0));
    assertEquals(0, queue.getNumClientsRetrievedTest(test1_1));
    assertEquals(0, queue.getNumClientsRetrievedTest(test1_2));

    // First client retrieves the first test block.
    {
      queue.getTestBlock(createClientInfo(0, "ie6"), 0, timeout);
      assertEquals(1, queue.getNumClientsRetrievedTest(test0_0));
      assertEquals(1, queue.getNumClientsRetrievedTest(test0_1));
      assertEquals(1, queue.getNumClientsRetrievedTest(test0_2));
      assertEquals(0, queue.getNumClientsRetrievedTest(test1_0));
      assertEquals(0, queue.getNumClientsRetrievedTest(test1_1));
      assertEquals(0, queue.getNumClientsRetrievedTest(test1_2));
    }

    // Second client retrieves the first test block.
    {
      queue.getTestBlock(createClientInfo(1, "ie6"), 0, timeout);
      assertEquals(2, queue.getNumClientsRetrievedTest(test0_0));
      assertEquals(2, queue.getNumClientsRetrievedTest(test0_1));
      assertEquals(2, queue.getNumClientsRetrievedTest(test0_2));
      assertEquals(0, queue.getNumClientsRetrievedTest(test1_0));
      assertEquals(0, queue.getNumClientsRetrievedTest(test1_1));
      assertEquals(0, queue.getNumClientsRetrievedTest(test1_2));
    }

    // First client retrieves the second test block.
    {
      queue.getTestBlock(createClientInfo(0, "ie6"), 1, timeout);
      assertEquals(2, queue.getNumClientsRetrievedTest(test0_0));
      assertEquals(2, queue.getNumClientsRetrievedTest(test0_1));
      assertEquals(2, queue.getNumClientsRetrievedTest(test0_2));
      assertEquals(1, queue.getNumClientsRetrievedTest(test1_0));
      assertEquals(1, queue.getNumClientsRetrievedTest(test1_1));
      assertEquals(1, queue.getNumClientsRetrievedTest(test1_2));
    }

    // First client retrieves the second test block again.
    {
      queue.getTestBlock(createClientInfo(0, "ie6"), 1, timeout);
      assertEquals(2, queue.getNumClientsRetrievedTest(test0_0));
      assertEquals(2, queue.getNumClientsRetrievedTest(test0_1));
      assertEquals(2, queue.getNumClientsRetrievedTest(test0_2));
      assertEquals(1, queue.getNumClientsRetrievedTest(test1_0));
      assertEquals(1, queue.getNumClientsRetrievedTest(test1_1));
      assertEquals(1, queue.getNumClientsRetrievedTest(test1_2));
    }
  }