Пример #1
0
  @Test(timeOut = 10_000)
  public void testBatchFunctionality() {

    // Component to test
    Batch batch = new Batch(0, BATCH_SIZE);

    // Test initial state is OK
    assertTrue(batch.isEmpty(), "Batch should be empty");
    assertFalse(batch.isFull(), "Batch shouldn't be full");
    assertEquals(batch.getNumEvents(), 0, "Num events should be 0");

    // Test getting or setting an element in the batch greater than the current number of events is
    // illegal
    try {
      batch.get(1);
      fail();
    } catch (IllegalStateException ex) {
      // Expected, as we can not access elements in the batch greater than the current number of
      // events
    }
    try {
      batch.set(1, new PersistEvent());
      fail();
    } catch (IllegalStateException ex) {
      // Expected, as we can not access elements in the batch greater than the current number of
      // events
    }

    // Test when filling the batch with different types of events, that becomes full
    for (int i = 0; i < BATCH_SIZE; i++) {
      if (i % 4 == 0) {
        batch.addTimestamp(ANY_ST, channel, monCtx);
      } else if (i % 4 == 1) {
        batch.addCommit(ANY_ST, ANY_CT, channel, monCtx);
      } else if (i % 4 == 2) {
        batch.addCommitRetry(ANY_ST, channel, monCtx);
      } else {
        batch.addAbort(ANY_ST, channel, monCtx);
      }
    }
    assertFalse(batch.isEmpty(), "Batch should contain elements");
    assertTrue(batch.isFull(), "Batch should be full");
    assertEquals(batch.getNumEvents(), BATCH_SIZE, "Num events should be " + BATCH_SIZE);

    // Test an exception is thrown when batch is full and a new element is going to be added
    try {
      batch.addCommit(ANY_ST, ANY_CT, channel, monCtx);
      fail("Should throw an IllegalStateException");
    } catch (IllegalStateException e) {
      assertEquals(e.getMessage(), "batch is full", "message returned doesn't match");
      LOG.debug("IllegalStateException catch properly");
    }
    assertTrue(batch.isFull(), "Batch shouldn't be empty");

    // Check the first 3 events and the last one correspond to the filling done above
    assertTrue(batch.get(0).getType().equals(PersistEvent.Type.TIMESTAMP));
    assertTrue(batch.get(1).getType().equals(PersistEvent.Type.COMMIT));
    assertTrue(batch.get(2).getType().equals(PersistEvent.Type.COMMIT_RETRY));
    assertTrue(batch.get(3).getType().equals(PersistEvent.Type.ABORT));

    // Set a new value for last element in Batch and check we obtain the right result
    batch.decreaseNumEvents();
    assertEquals(batch.getNumEvents(), BATCH_SIZE - 1, "Num events should be " + (BATCH_SIZE - 1));
    try {
      batch.get(BATCH_SIZE - 1);
      fail();
    } catch (IllegalStateException ex) {
      // Expected, as we can not access elements in the batch greater than the current number of
      // events
    }

    // Re-check that batch is NOT full
    assertFalse(batch.isFull(), "Batch shouldn't be full");

    // Clear the batch and goes back to its initial state
    batch.clear();
    assertTrue(batch.isEmpty(), "Batch should be empty");
    assertFalse(batch.isFull(), "Batch shouldn't be full");
    assertEquals(batch.getNumEvents(), 0, "Num events should be 0");
  }