@Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);
    mMultiplexProducer =
        new BitmapMemoryCacheKeyMultiplexProducer(mCacheKeyFactory, mInputProducer);
    mImageRequest1 = mock(ImageRequest.class);
    mImageRequest2 = mock(ImageRequest.class);
    mProducerContext1 =
        new SettableProducerContext(
            mImageRequest1,
            "id1",
            mProducerListener,
            mCallerContext,
            ImageRequest.RequestLevel.FULL_FETCH,
            false,
            true,
            Priority.MEDIUM,
            mConfig);
    mProducerContext2 =
        new SettableProducerContext(
            mImageRequest1,
            "id2",
            mProducerListener,
            mCallerContext,
            ImageRequest.RequestLevel.FULL_FETCH,
            false,
            true,
            Priority.MEDIUM,
            mConfig);
    mProducerContext3 =
        new SettableProducerContext(
            mImageRequest2,
            "id3",
            mProducerListener,
            mCallerContext,
            ImageRequest.RequestLevel.FULL_FETCH,
            false,
            true,
            Priority.MEDIUM,
            mConfig);
    mBitmapMemoryCacheKey1 = mock(BitmapMemoryCacheKey.class);
    mBitmapMemoryCacheKey2 = mock(BitmapMemoryCacheKey.class);
    mConsumer1 = mock(Consumer.class);
    mConsumer2 = mock(Consumer.class);
    mConsumer3 = mock(Consumer.class);
    mFinalCloseableImage1 = mock(CloseableImage.class);
    mFinalCloseableImage2 = mock(CloseableImage.class);
    mIntermediateCloseableImage1 = mock(CloseableImage.class);
    mIntermediateCloseableImage2 = mock(CloseableImage.class);
    mFinalImageReference1 = CloseableReference.of(mFinalCloseableImage1);
    mFinalImageReference2 = CloseableReference.of(mFinalCloseableImage2);
    mIntermediateImageReference1 = CloseableReference.of(mIntermediateCloseableImage1);
    mIntermediateImageReference2 = CloseableReference.of(mIntermediateCloseableImage2);

    when(mCacheKeyFactory.getBitmapCacheKey(mImageRequest1, mCallerContext))
        .thenReturn(mBitmapMemoryCacheKey1);
    when(mCacheKeyFactory.getBitmapCacheKey(mImageRequest2, mCallerContext))
        .thenReturn(mBitmapMemoryCacheKey2);

    doAnswer(
            new Answer() {
              @Nullable
              @Override
              public Void answer(InvocationOnMock invocation) throws Throwable {
                if (mForwardingConsumer1 == null) {
                  mForwardingConsumer1 = (Consumer) invocation.getArguments()[0];
                  mMultiplexedContext1 = (BaseProducerContext) invocation.getArguments()[1];
                } else {
                  mForwardingConsumer2 = (Consumer) invocation.getArguments()[0];
                  mMultiplexedContext2 = (BaseProducerContext) invocation.getArguments()[1];
                }
                return null;
              }
            })
        .when(mInputProducer)
        .produceResults(any(Consumer.class), any(ProducerContext.class));
  }
An extra flag to check we maintain other flags than just whether this is the last result
imagepipeline/src/test/java/com/facebook/imagepipeline/producers/MultiplexProducerTest.java