@Test
  public void testRollback() throws Exception {
    // put an item and commit
    putEvents(channel, "rollback", 1, 50);

    Transaction transaction;
    // put an item we will rollback
    transaction = channel.getTransaction();
    transaction.begin();
    channel.put(EventBuilder.withBody("this is going to be rolledback".getBytes(Charsets.UTF_8)));
    transaction.rollback();
    transaction.close();

    // simulate crash
    channel.stop();
    channel = createFileChannel();

    // get the item which was not rolled back
    transaction = channel.getTransaction();
    transaction.begin();
    Event event = channel.take();
    transaction.commit();
    transaction.close();
    Assert.assertNotNull(event);
    Assert.assertEquals("rollback-0-0", new String(event.getBody(), Charsets.UTF_8));
  }
 private RecoverableMemoryChannel createFileChannel() {
   RecoverableMemoryChannel channel = new RecoverableMemoryChannel();
   context = new Context();
   context.put(RecoverableMemoryChannel.WAL_DATA_DIR, dataDir.getAbsolutePath());
   Configurables.configure(channel, context);
   channel.start();
   return channel;
 }
  @Test
  public void testRollbackWithSink() throws Exception {
    final NullSink nullSink = new NullSink();
    Context ctx = new Context();
    ctx.put("batchSize", "1");
    nullSink.configure(ctx);
    nullSink.setChannel(channel);
    final int numItems = 99;
    Thread t =
        new Thread() {
          @Override
          public void run() {
            int count = 0;
            while (count++ < numItems) {
              try {
                nullSink.process();
                Thread.sleep(1);
              } catch (EventDeliveryException e) {
                break;
              } catch (Exception e) {
                Throwables.propagate(e);
              }
            }
          }
        };
    t.setDaemon(true);
    t.setName("NullSink");
    t.start();

    putEvents(channel, "rollback", 10, 100);

    Transaction transaction;
    // put an item we will rollback
    transaction = channel.getTransaction();
    transaction.begin();
    channel.put(EventBuilder.withBody("this is going to be rolledback".getBytes(Charsets.UTF_8)));
    transaction.rollback();
    transaction.close();

    while (t.isAlive()) {
      Thread.sleep(1);
    }

    // simulate crash
    channel.stop();
    channel = createFileChannel();

    // get the item which was not rolled back
    transaction = channel.getTransaction();
    transaction.begin();
    Event event = channel.take();
    transaction.commit();
    transaction.close();
    Assert.assertNotNull(event);
    Assert.assertEquals("rollback-90-9", new String(event.getBody(), Charsets.UTF_8));
  }
 @Test
 public void testRestart() throws Exception {
   List<String> in = Lists.newArrayList();
   try {
     while (true) {
       in.addAll(putEvents(channel, "restart", 1, 1));
     }
   } catch (ChannelException e) {
     Assert.assertEquals("Cannot acquire capacity", e.getMessage());
   }
   channel.stop();
   channel = createFileChannel();
   List<String> out = takeEvents(channel, 1, Integer.MAX_VALUE);
   Collections.sort(in);
   Collections.sort(out);
   Assert.assertEquals(in, out);
 }