@Test
public void testImagePooling() throws InterruptedException, IOException {

  CountDownLatch countDown1 = new CountDownLatch(1);
  CountDownLatch countDown2 = new CountDownLatch(1);
  AtomicBoolean secondImageFreed = new AtomicBoolean(false);
  ImagePoolImpl.ImageImpl image1 = myPool.create(50, 50, BufferedImage.TYPE_INT_ARGB, (image) -> countDown1.countDown());
  image1.drawFrom(getSampleImage());
  BufferedImage internalPtr = image1.myBuffer;
  ImagePoolImpl.ImageImpl image2 = myPool.create(50, 50, BufferedImage.TYPE_INT_ARGB, (image) -> {
    countDown2.countDown();
    secondImageFreed.set(true);
  });
  assertNotEquals(image1.myBuffer, image2.myBuffer);
  //noinspection UnusedAssignment
  image1 = null;
  gc();
  // Wait for the image to be collected
  countDown1.await(3, TimeUnit.SECONDS);
  image1 = myPool.create(50, 50, BufferedImage.TYPE_INT_ARGB, null);
  assertEquals(image1.myBuffer, internalPtr);
  assertFalse(secondImageFreed.get());
  // The image is being reused. Check that it's a clean image
  ImageDiffUtil
    .assertImageSimilar("clean", new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB), image1.myBuffer.getSubimage(0, 0, 50, 50), 0.0);
  //noinspection UnusedAssignment
  image2 = null;
  gc();
  countDown2.await(3, TimeUnit.SECONDS);
  ImagePoolImpl.ImageImpl tmpImage = myPool.create(50, 50, BufferedImage.TYPE_INT_ARGB_PRE, null);
  assertEquals(50, tmpImage.getWidth());
  assertEquals(50, tmpImage.getHeight());
  assertEquals(BufferedImage.TYPE_INT_ARGB_PRE, tmpImage.myBuffer.getType());
  tmpImage = myPool.create(51, 50, BufferedImage.TYPE_INT_ARGB, null);
  assertEquals(51, tmpImage.getWidth());
  assertEquals(50, tmpImage.getHeight());
  assertEquals(BufferedImage.TYPE_INT_ARGB, tmpImage.myBuffer.getType());
  tmpImage = myPool.create(50, 51, BufferedImage.TYPE_INT_ARGB, null);
  assertEquals(50, tmpImage.getWidth());
  assertEquals(51, tmpImage.getHeight());
  assertEquals(BufferedImage.TYPE_INT_ARGB, tmpImage.myBuffer.getType());
  //noinspection UnusedAssignment
  tmpImage = null;
  gc();
}