|
| 1 | +/* |
| 2 | + * Copyright (C) 2016 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.android.exoplayer2.upstream.cache; |
| 17 | + |
| 18 | +import android.net.Uri; |
| 19 | +import android.test.InstrumentationTestCase; |
| 20 | +import android.test.MoreAsserts; |
| 21 | +import com.google.android.exoplayer2.C; |
| 22 | +import com.google.android.exoplayer2.testutil.FakeDataSource; |
| 23 | +import com.google.android.exoplayer2.testutil.FakeDataSource.Builder; |
| 24 | +import com.google.android.exoplayer2.testutil.TestUtil; |
| 25 | +import com.google.android.exoplayer2.upstream.DataSpec; |
| 26 | +import com.google.android.exoplayer2.upstream.HttpDataSource.InvalidResponseCodeException; |
| 27 | +import java.io.File; |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.Arrays; |
| 30 | + |
| 31 | +/** Unit tests for {@link CacheDataSource}. */ |
| 32 | +public class CacheDataSourceTest extends InstrumentationTestCase { |
| 33 | + |
| 34 | + private static final byte[] TEST_DATA = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 35 | + private static final int MAX_CACHE_FILE_SIZE = 3; |
| 36 | + private static final String KEY_1 = "key 1"; |
| 37 | + private static final String KEY_2 = "key 2"; |
| 38 | + |
| 39 | + private File cacheDir; |
| 40 | + private SimpleCache simpleCache; |
| 41 | + |
| 42 | + @Override |
| 43 | + protected void setUp() throws Exception { |
| 44 | + // Create a temporary folder |
| 45 | + cacheDir = File.createTempFile("CacheDataSourceTest", null); |
| 46 | + assertTrue(cacheDir.delete()); |
| 47 | + assertTrue(cacheDir.mkdir()); |
| 48 | + |
| 49 | + simpleCache = new SimpleCache(cacheDir, new NoOpCacheEvictor()); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected void tearDown() throws Exception { |
| 54 | + TestUtil.recursiveDelete(cacheDir); |
| 55 | + } |
| 56 | + |
| 57 | + public void testMaxCacheFileSize() throws Exception { |
| 58 | + CacheDataSource cacheDataSource = createCacheDataSource(false, false, false); |
| 59 | + assertReadDataContentLength(cacheDataSource, false, false); |
| 60 | + assertEquals((int) Math.ceil((double) TEST_DATA.length / MAX_CACHE_FILE_SIZE), |
| 61 | + cacheDir.listFiles().length); |
| 62 | + } |
| 63 | + |
| 64 | + public void testCacheAndRead() throws Exception { |
| 65 | + assertCacheAndRead(false, false); |
| 66 | + } |
| 67 | + |
| 68 | + public void testCacheAndReadUnboundedRequest() throws Exception { |
| 69 | + assertCacheAndRead(true, false); |
| 70 | + } |
| 71 | + |
| 72 | + public void testCacheAndReadUnknownLength() throws Exception { |
| 73 | + assertCacheAndRead(false, true); |
| 74 | + } |
| 75 | + |
| 76 | + // Disabled test as we don't support caching of definitely unknown length content |
| 77 | + public void disabledTestCacheAndReadUnboundedRequestUnknownLength() throws Exception { |
| 78 | + assertCacheAndRead(true, true); |
| 79 | + } |
| 80 | + |
| 81 | + public void testUnsatisfiableRange() throws Exception { |
| 82 | + // Bounded request but the content length is unknown. This forces all data to be cached but not |
| 83 | + // the length |
| 84 | + assertCacheAndRead(false, true); |
| 85 | + |
| 86 | + // Now do an unbounded request. This will read all of the data from cache and then try to read |
| 87 | + // more from upstream which will cause to a 416 so CDS will store the length. |
| 88 | + CacheDataSource cacheDataSource = createCacheDataSource(true, true, true); |
| 89 | + assertReadDataContentLength(cacheDataSource, true, true); |
| 90 | + |
| 91 | + // If the user try to access off range then it should throw an IOException |
| 92 | + try { |
| 93 | + cacheDataSource = createCacheDataSource(false, false, false); |
| 94 | + cacheDataSource.open(new DataSpec(Uri.EMPTY, TEST_DATA.length, 5, KEY_1)); |
| 95 | + fail(); |
| 96 | + } catch (TestIOException e) { |
| 97 | + // success |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public void testContentLengthEdgeCases() throws Exception { |
| 102 | + // Read partial at EOS but don't cross it so length is unknown |
| 103 | + CacheDataSource cacheDataSource = createCacheDataSource(false, false, true); |
| 104 | + assertReadData(cacheDataSource, true, TEST_DATA.length - 2, 2); |
| 105 | + assertEquals(C.LENGTH_UNSET, simpleCache.getContentLength(KEY_1)); |
| 106 | + |
| 107 | + // Now do an unbounded request for whole data. This will cause a bounded request from upstream. |
| 108 | + // End of data from upstream shouldn't be mixed up with EOS and cause length set wrong. |
| 109 | + cacheDataSource = createCacheDataSource(true, false, true); |
| 110 | + assertReadDataContentLength(cacheDataSource, true, true); |
| 111 | + |
| 112 | + // Now the length set correctly do an unbounded request with offset |
| 113 | + assertEquals(2, cacheDataSource.open(new DataSpec(Uri.EMPTY, TEST_DATA.length - 2, |
| 114 | + C.LENGTH_UNSET, KEY_1))); |
| 115 | + |
| 116 | + // An unbounded request with offset for not cached content |
| 117 | + assertEquals(C.LENGTH_UNSET, cacheDataSource.open(new DataSpec(Uri.EMPTY, TEST_DATA.length - 2, |
| 118 | + C.LENGTH_UNSET, KEY_2))); |
| 119 | + } |
| 120 | + |
| 121 | + private void assertCacheAndRead(boolean unboundedRequest, boolean simulateUnknownLength) |
| 122 | + throws IOException { |
| 123 | + // Read all data from upstream and cache |
| 124 | + CacheDataSource cacheDataSource = createCacheDataSource(false, false, simulateUnknownLength); |
| 125 | + assertReadDataContentLength(cacheDataSource, unboundedRequest, simulateUnknownLength); |
| 126 | + |
| 127 | + // Just read from cache |
| 128 | + cacheDataSource = createCacheDataSource(false, true, simulateUnknownLength); |
| 129 | + assertReadDataContentLength(cacheDataSource, unboundedRequest, |
| 130 | + false /*length is already cached*/); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Reads data until EOI and compares it to {@link #TEST_DATA}. Also checks content length returned |
| 135 | + * from open() call and the cached content length. |
| 136 | + */ |
| 137 | + private void assertReadDataContentLength(CacheDataSource cacheDataSource, |
| 138 | + boolean unboundedRequest, boolean unknownLength) throws IOException { |
| 139 | + int length = unboundedRequest ? C.LENGTH_UNSET : TEST_DATA.length; |
| 140 | + assertReadData(cacheDataSource, unknownLength, 0, length); |
| 141 | + assertEquals("When the range specified, CacheDataSource doesn't reach EOS so shouldn't cache " |
| 142 | + + "content length", !unboundedRequest ? C.LENGTH_UNSET : TEST_DATA.length, |
| 143 | + simpleCache.getContentLength(KEY_1)); |
| 144 | + } |
| 145 | + |
| 146 | + private void assertReadData(CacheDataSource cacheDataSource, boolean unknownLength, int position, |
| 147 | + int length) throws IOException { |
| 148 | + int actualLength = TEST_DATA.length - position; |
| 149 | + if (length != C.LENGTH_UNSET) { |
| 150 | + actualLength = Math.min(actualLength, length); |
| 151 | + } |
| 152 | + assertEquals(unknownLength ? length : actualLength, |
| 153 | + cacheDataSource.open(new DataSpec(Uri.EMPTY, position, length, KEY_1))); |
| 154 | + |
| 155 | + byte[] buffer = new byte[100]; |
| 156 | + int index = 0; |
| 157 | + while (true) { |
| 158 | + int read = cacheDataSource.read(buffer, index, buffer.length - index); |
| 159 | + if (read == C.RESULT_END_OF_INPUT) { |
| 160 | + break; |
| 161 | + } |
| 162 | + index += read; |
| 163 | + } |
| 164 | + assertEquals(actualLength, index); |
| 165 | + MoreAsserts.assertEquals(Arrays.copyOfRange(TEST_DATA, position, position + actualLength), |
| 166 | + Arrays.copyOf(buffer, index)); |
| 167 | + |
| 168 | + cacheDataSource.close(); |
| 169 | + } |
| 170 | + |
| 171 | + private CacheDataSource createCacheDataSource(boolean set416exception, boolean setReadException, |
| 172 | + boolean simulateUnknownLength) { |
| 173 | + Builder builder = new Builder(); |
| 174 | + if (setReadException) { |
| 175 | + builder.appendReadError(new IOException("Shouldn't read from upstream")); |
| 176 | + } |
| 177 | + builder.setSimulateUnknownLength(simulateUnknownLength); |
| 178 | + builder.appendReadData(TEST_DATA); |
| 179 | + FakeDataSource upstream = builder.build(); |
| 180 | + upstream.setUnsatisfiableRangeException(set416exception |
| 181 | + ? new InvalidResponseCodeException(416, null, null) |
| 182 | + : new TestIOException()); |
| 183 | + return new CacheDataSource(simpleCache, upstream, |
| 184 | + CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_CACHE_UNBOUNDED_REQUESTS, |
| 185 | + MAX_CACHE_FILE_SIZE); |
| 186 | + } |
| 187 | + |
| 188 | + private static class TestIOException extends IOException {} |
| 189 | + |
| 190 | +} |
0 commit comments