Source:
<code>public void testFlushedIfAppendFailedWithBucketClosedException() throws Exception { sink = new HDFSEventSink() { @Override BucketWriter initializeBucketWriter(String realPath, String realName, String lookupPath, HDFSWriter hdfsWriter, WriterCallback closeCallback) { BucketWriter bw = Mockito.spy(super.initializeBucketWriter(realPath, realName, lookupPath, hdfsWriter, closeCallback)); try { Mockito.doCallRealMethod() .doThrow(BucketClosedException.class) .when(bw).append(Mockito.any(Event.class)); } catch (IOException | InterruptedException e) { Assert.fail("This shouldn't happen, as append() is called during mocking."); } return bw; } }; Context context = new Context(ImmutableMap.of("hdfs.path", testPath)); Configurables.configure(sink, context); MemoryChannel channel = new MemoryChannel(); Configurables.configure(channel, new Context()); sink.setChannel(channel); sink.start(); Transaction txn = channel.getTransaction(); txn.begin(); channel.put(EventBuilder.withBody("test".getBytes())); channel.put(EventBuilder.withBody("test".getBytes())); txn.commit(); txn.close(); sink.process(); <START> Map<String, BucketWriter> bucketWriterMap = sink.getSfWriters(); <END> Assert.assertEquals(1, bucketWriterMap.size()); BucketWriter bw = bucketWriterMap.values().iterator().next(); Mockito.verify(bw, Mockito.times(1)).flush(); sink.stop(); }</code><technical_language>Instead of accessing sfwriters collect created BucketWriters test extension of HDFSEventSink. This check bucketwriter created is flushed/closed. sfwriters list reference created bucketwriter (lookup path is common newly created bw kicks out closed line 406)</technical_language>

Target:
public void testFlushedIfAppendFailedWithBucketClosedException() throws Exception { final Set<BucketWriter> bucketWriters = new HashSet<>(); sink = new HDFSEventSink() { @Override BucketWriter initializeBucketWriter(String realPath, String realName, String lookupPath, HDFSWriter hdfsWriter, WriterCallback closeCallback) { BucketWriter bw = Mockito.spy(super.initializeBucketWriter(realPath, realName, lookupPath, hdfsWriter, closeCallback)); try { Mockito.doCallRealMethod() .doThrow(BucketClosedException.class) .when(bw).append(Mockito.any(Event.class)); } catch (IOException | InterruptedException e) { Assert.fail("This shouldn't happen, as append() is called during mocking."); } bucketWriters.add(bw); return bw; } }; Context context = new Context(ImmutableMap.of("hdfs.path", testPath)); Configurables.configure(sink, context); Channel channel = Mockito.spy(new MemoryChannel()); Configurables.configure(channel, new Context()); final Iterator<Event> events = Iterators.forArray( EventBuilder.withBody("test1".getBytes()), EventBuilder.withBody("test2".getBytes())); Mockito.doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { return events.hasNext() ? events.next() : null; } }).when(channel).take(); sink.setChannel(channel); sink.start(); sink.process(); Mockito.verify(channel, Mockito.times(3)).take(); FileSystem fs = FileSystem.get(new Configuration()); int fileCount = 0; for (RemoteIterator<LocatedFileStatus> i = fs.listFiles(new Path(testPath), false); i.hasNext(); i.next()) { fileCount++; } Assert.assertEquals(2, fileCount); Assert.assertEquals(2, bucketWriters.size()); for (BucketWriter bw : bucketWriters) { Mockito.verify(bw, Mockito.times(1)).flush(); } sink.stop(); }