Source:
<code>public void testSortedLedgerFlushFailure() throws Exception { File tmpDir = createTempDir("bkTest", ".dir"); File curDir = Bookie.getCurrentDirectory(tmpDir); Bookie.checkDirectoryStructure(curDir); int gcWaitTime = 1000; ServerConfiguration conf = TestBKConfiguration.newServerConfiguration(); conf.setGcWaitTime(gcWaitTime) .setLedgerDirNames(new String[] { tmpDir.toString() }) .setJournalDirName(tmpDir.toString()) .setLedgerStorageClass(FlushTestSortedLedgerStorage.class.getName()); Bookie bookie = new Bookie(conf); bookie.start(); FlushTestSortedLedgerStorage flushTestSortedLedgerStorage = (FlushTestSortedLedgerStorage) bookie.ledgerStorage; EntryMemTable memTable = flushTestSortedLedgerStorage.memTable; bookie.addEntry(generateEntry(1, 1), new Bookie.NopWriteCallback(), null, "passwd".getBytes()); flushTestSortedLedgerStorage.addEntry(generateEntry(1, 2)); assertFalse("Bookie is expected to be in ReadWrite mode", bookie.isReadOnly()); assertTrue("EntryMemTable SnapShot is expected to be empty", memTable.snapshot.isEmpty()); flushTestSortedLedgerStorage.setInjectMemTableSizeLimitReached(true); flushTestSortedLedgerStorage.setInjectFlushException(true); flushTestSortedLedgerStorage.addEntry(generateEntry(1, 2)); <START> Thread.sleep(1000); <END> assertFalse("EntryMemTable SnapShot is not expected to be empty", memTable.snapshot.isEmpty()); LOG.info("state is {}", bookie.getStateManager().getState()); assertTrue("Bookie is expected to be in Read mode", bookie.isReadOnly()); bookie.addEntry(generateEntry(1, 3), new BookkeeperInternalCallbacks.WriteCallback(){ public void writeComplete(int rc, long ledgerId, long entryId, BookieSocketAddress addr, Object ctx){ LOG.info("fail write to bk"); assertTrue(rc != OK); }; }, null, "passwd".getBytes()); bookie.shutdown(); }</code><technical_language>do need sleep(1000) here? this test sequence more deterministic depending time, reduce flakiness</technical_language>

Target:
public void testSortedLedgerFlushFailure() throws Exception { File tmpDir = createTempDir("bkTest", ".dir"); File curDir = Bookie.getCurrentDirectory(tmpDir); Bookie.checkDirectoryStructure(curDir); int gcWaitTime = 1000; ServerConfiguration conf = TestBKConfiguration.newServerConfiguration(); conf.setGcWaitTime(gcWaitTime) .setLedgerDirNames(new String[] { tmpDir.toString() }) .setJournalDirName(tmpDir.toString()) .setLedgerStorageClass(FlushTestSortedLedgerStorage.class.getName()); Bookie bookie = new Bookie(conf); bookie.start(); FlushTestSortedLedgerStorage flushTestSortedLedgerStorage = (FlushTestSortedLedgerStorage) bookie.ledgerStorage; EntryMemTable memTable = flushTestSortedLedgerStorage.memTable; bookie.addEntry(generateEntry(1, 1), new Bookie.NopWriteCallback(), null, "passwd".getBytes()); flushTestSortedLedgerStorage.addEntry(generateEntry(1, 2)); assertFalse("Bookie is expected to be in ReadWrite mode", bookie.isReadOnly()); assertTrue("EntryMemTable SnapShot is expected to be empty", memTable.snapshot.isEmpty()); flushTestSortedLedgerStorage.setInjectMemTableSizeLimitReached(true); flushTestSortedLedgerStorage.setInjectFlushException(true); flushTestSortedLedgerStorage.addEntry(generateEntry(1, 2)); assertFalse("EntryMemTable SnapShot is not expected to be empty", memTable.snapshot.isEmpty()); assertTrue("Bookie is expected to be in Read mode", bookie.isReadOnly()); bookie.addEntry(generateEntry(1, 3), new BookkeeperInternalCallbacks.WriteCallback(){ public void writeComplete(int rc, long ledgerId, long entryId, BookieSocketAddress addr, Object ctx){ LOG.info("fail write to bk"); assertTrue(rc != OK); }; }, null, "passwd".getBytes()); bookie.shutdown(); }