public static void move(String name, ContentDump source, ContentDump target) { Collection<String> content = source.read(name); try (ContentWriter contentWriter = target.createWriter(name)) { contentWriter.write(content).flush(); } source.delete(name); }
private void assertExactlyOnce(List<String> expectedValues) throws IOException { ArrayList<String> actualValues = new ArrayList<>(); for (String name : targetDirectory.listFiles()) { actualValues.addAll(targetDirectory.read(name)); } Collections.sort(actualValues); Collections.sort(expectedValues); assertEquals(expectedValues, actualValues); }
@Override protected void commit(ContentTransaction transaction) { if (throwException.get()) { throw new RuntimeException("Expected exception"); } ContentDump.move( transaction.tmpContentWriter.getName(), tmpDirectory, targetDirectory); }
@Test public void testFailBeforeNotify() throws Exception { harness.open(); harness.processElement("42", 0); harness.snapshot(0, 1); harness.processElement("43", 2); OperatorSubtaskState snapshot = harness.snapshot(1, 3); tmpDirectory.setWritable(false); try { harness.processElement("44", 4); harness.snapshot(2, 5); fail("something should fail"); } catch (Exception ex) { if (!(ex.getCause() instanceof ContentDump.NotWritableException)) { throw ex; } // ignore } closeTestHarness(); tmpDirectory.setWritable(true); setUpTestHarness(); harness.initializeState(snapshot); assertExactlyOnce(Arrays.asList("42", "43")); closeTestHarness(); assertEquals(0, tmpDirectory.listFiles().size()); }
@Override protected ContentTransaction beginTransaction() throws Exception { return new ContentTransaction(tmpDirectory.createWriter(UUID.randomUUID().toString())); }
@Before public void setUp() throws Exception { loggingEvents = new ArrayList<>(); setupLogger(); targetDirectory = new ContentDump(); tmpDirectory = new ContentDump(); clock = new SettableClock(); setUpTestHarness(); }
@Override protected void abort(ContentTransaction transaction) { transaction.tmpContentWriter.close(); tmpDirectory.delete(transaction.tmpContentWriter.getName()); } }
@Test public void testNotifyOfCompletedCheckpoint() throws Exception { harness.open(); harness.processElement("42", 0); harness.snapshot(0, 1); harness.processElement("43", 2); harness.snapshot(1, 3); harness.processElement("44", 4); harness.snapshot(2, 5); harness.notifyOfCompletedCheckpoint(1); assertExactlyOnce(Arrays.asList("42", "43")); assertEquals(2, tmpDirectory.listFiles().size()); // one for checkpointId 2 and second for the currentTransaction }