@Test
  public void testLifecycle_I2_I2_I1_F2_I1_S1_C() {
    DataSource<Object> dataSource = getAndSubscribe();
    DataSubscriber<Object> subscriber1 = verifyGetAndSubscribeM(mDataSourceSupplier1, mSrc1);
    DataSubscriber<Object> subscriber2 = verifyGetAndSubscribeM(mDataSourceSupplier2, mSrc2);
    DataSubscriber<Object> subscriber3 = verifyGetAndSubscribe(mDataSourceSupplier3, mSrc3);

    Object val2a = mock(Object.class);
    setState(mSrc2, NOT_CLOSED, NOT_FINISHED, WITH_RESULT, val2a, NOT_FAILED, null);
    subscriber2.onNewResult(mSrc2);
    mInOrder.verify(mSrc3).close();
    verifySubscriber(dataSource, mSrc2, ON_NEW_RESULT);
    verifyState(dataSource, mSrc2, NOT_CLOSED, NOT_FINISHED, WITH_RESULT, val2a, NOT_FAILED, null);

    Object val2b = mock(Object.class);
    setState(mSrc2, NOT_CLOSED, NOT_FINISHED, WITH_RESULT, val2b, NOT_FAILED, null);
    subscriber2.onNewResult(mSrc2);
    verifySubscriber(dataSource, mSrc2, ON_NEW_RESULT);
    verifyState(dataSource, mSrc2, NOT_CLOSED, NOT_FINISHED, WITH_RESULT, val2b, NOT_FAILED, null);

    // gets ignored because DS2 was first to produce result
    Object val1a = mock(Object.class);
    setState(mSrc1, NOT_CLOSED, NOT_FINISHED, WITH_RESULT, val1a, NOT_FAILED, null);
    subscriber1.onNewResult(mSrc1);
    verifySubscriber(dataSource, mSrc1, NO_INTERACTIONS);
    verifyState(dataSource, mSrc2, NOT_CLOSED, NOT_FINISHED, WITH_RESULT, val2b, NOT_FAILED, null);

    setState(mSrc2, NOT_CLOSED, FINISHED, WITH_RESULT, val2b, FAILED, mock(Throwable.class));
    subscriber2.onFailure(mSrc2);
    verifySubscriber(dataSource, mSrc2, NO_INTERACTIONS);
    verifyState(dataSource, mSrc2, NOT_CLOSED, NOT_FINISHED, WITH_RESULT, val2b, NOT_FAILED, null);

    // gets ignored because DS2 was first to produce result
    Object val1b = mock(Object.class);
    setState(mSrc1, NOT_CLOSED, NOT_FINISHED, WITH_RESULT, val1b, NOT_FAILED, null);
    subscriber1.onNewResult(mSrc1);
    verifySubscriber(dataSource, mSrc1, NO_INTERACTIONS);
    verifyState(dataSource, mSrc2, NOT_CLOSED, NOT_FINISHED, WITH_RESULT, val2b, NOT_FAILED, null);

    Object val1c = mock(Object.class);
    setState(mSrc1, NOT_CLOSED, FINISHED, WITH_RESULT, val1c, NOT_FAILED, null);
    subscriber1.onNewResult(mSrc1);
    mInOrder.verify(mSrc2).close();
    verifySubscriber(dataSource, mSrc1, ON_NEW_RESULT);
    verifyState(dataSource, mSrc1, NOT_CLOSED, FINISHED, WITH_RESULT, val1c, NOT_FAILED, null);

    testClose(dataSource, mSrc1);
    verifyState(dataSource, null, CLOSED, FINISHED, WITHOUT_RESULT, null, NOT_FAILED, null);
  }
Second data source produced multiple intermediate results first, intermediate result of
highest-quality data source gets ignored afterwards. Second data source fails and first data
source produced another intermediate result, but it gets ignored again. Finally, first data
source produced its final result which is set.
fbcore/src/test/java/com/facebook/datasource/IncreasingQualityDataSourceSupplierTest.java