@Test
public void testContentLengthEdgeCases() throws Exception {

  DataSpec dataSpec = buildDataSpec(TEST_DATA.length - 2, 2);
  // Read partial at EOS but don't cross it so length is unknown.
  CacheDataSource cacheDataSource = createCacheDataSource(false, true);
  assertReadData(cacheDataSource, dataSpec, true);
  assertThat(ContentMetadata.getContentLength(cache.getContentMetadata(defaultCacheKey)))
      .isEqualTo(C.LENGTH_UNSET);
  // Now do an unbounded request for whole data. This will cause a bounded request from upstream.
  // End of data from upstream shouldn't be mixed up with EOS and cause length set wrong.
  cacheDataSource = createCacheDataSource(false, true);
  assertReadDataContentLength(cacheDataSource, unboundedDataSpec, true, false);
  // Now the length set correctly do an unbounded request with offset.
  assertThat(
          cacheDataSource.open(
              buildDataSpec(TEST_DATA.length - 2, C.LENGTH_UNSET, defaultCacheKey)))
      .isEqualTo(2);
  // An unbounded request with offset for not cached content.
  dataSpec =
      new DataSpec(
          Uri.parse("https://www.test.com/other"),
          TEST_DATA.length - 2,
          C.LENGTH_UNSET,
          /* key= */ null);
  assertThat(cacheDataSource.open(dataSpec)).isEqualTo(C.LENGTH_UNSET);
}