@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)); }
@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)); }