SegmentBufferWriterPool implements WriteOperationHandler { @Override public void flush(@NotNull SegmentStore store) throws IOException { List<SegmentBufferWriter> toFlush = newArrayList(); List<SegmentBufferWriter> toReturn = newArrayList(); poolMonitor.enter(); try { toFlush.addAll(writers.values()); writers.clear(); toReturn.addAll(borrowed); borrowed.clear(); } finally { poolMonitor.leave(); } if (safeEnterWhen(poolMonitor, allReturned(toReturn))) { try { toFlush.addAll(toReturn); disposed.removeAll(toReturn); } finally { poolMonitor.leave(); } } for (SegmentBufferWriter writer : toFlush) { writer.flush(store); } } SegmentBufferWriterPool(
            @NotNull SegmentIdProvider idProvider,
            @NotNull SegmentReader reader,
            @NotNull String wid,
            @NotNull Supplier<GCGeneration> gcGeneration); @Override @NotNull GCGeneration getGCGeneration(); @NotNull @Override RecordId execute(@NotNull GCGeneration gcGeneration,
                            @NotNull WriteOperation writeOperation); @Override void flush(@NotNull SegmentStore store);  }
@Test public void testFlush() throws ExecutionException, InterruptedException, IOException { GCGeneration gen = pool.getGCGeneration(); ConcurrentMap<String, SegmentBufferWriter> map1 = newConcurrentMap(); Future<RecordId> res1 = execute(gen, createOp("a", map1), 0); Future<RecordId> res2 = execute(gen, createOp("b", map1), 1); Future<RecordId> res3 = execute(gen, createOp("c", map1), 2); sleepUninterruptibly(10, MILLISECONDS); assertEquals(rootId, res1.get()); assertEquals(rootId, res2.get()); assertEquals(rootId, res3.get()); assertEquals(3, map1.size()); pool.flush(store); ConcurrentMap<String, SegmentBufferWriter> map2 = newConcurrentMap(); Future<RecordId> res4 = execute(gen, createOp("a", map2), 0); Future<RecordId> res5 = execute(gen, createOp("b", map2), 1); Future<RecordId> res6 = execute(gen, createOp("c", map2), 2); sleepUninterruptibly(10, MILLISECONDS); assertEquals(rootId, res4.get()); assertEquals(rootId, res5.get()); assertEquals(rootId, res6.get()); assertEquals(3, map2.size()); assertTrue(intersection(newHashSet(map1.values()), newHashSet(map2.values())).isEmpty()); }