DownloadManagerStressTest.java revision 31fd85f39b554e09b2e6c1c2ccf5c186859880fa
1/*
2 * Copyright (C) 2010 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
17package android.app;
18
19import java.io.File;
20import java.util.Random;
21
22import android.app.DownloadManager.Query;
23import android.app.DownloadManager.Request;
24import android.database.Cursor;
25import android.net.Uri;
26import android.os.ParcelFileDescriptor;
27import android.test.suitebuilder.annotation.LargeTest;
28import android.util.Log;
29
30
31public class DownloadManagerStressTest extends DownloadManagerBaseTest {
32    private static String LOG_TAG = "android.net.DownloadManagerStressTest";
33
34    /**
35     * {@inheritDoc}
36     */
37    @Override
38    public void setUp() throws Exception {
39        super.setUp();
40        mServer.play(0);
41        removeAllCurrentDownloads();
42    }
43
44    /**
45     * Attempts to downloading thousands of files simultaneously
46     */
47    public void testDownloadThousands() throws Exception {
48        int NUM_FILES = 1500;
49        int MAX_FILE_SIZE = 3000;
50        long[] reqs = new long[NUM_FILES];
51
52        // need to be sure all current downloads have stopped first
53        MultipleDownloadsCompletedReceiver receiver = registerNewMultipleDownloadsReceiver();
54        Cursor cursor = null;
55        try {
56            Random r = new LoggingRng();
57            for (int i = 0; i < NUM_FILES; ++i) {
58                int size = r.nextInt(MAX_FILE_SIZE);
59                byte[] blobData = generateData(size, DataType.TEXT);
60
61                Uri uri = getServerUri(DEFAULT_FILENAME);
62                Request request = new Request(uri);
63                request.setTitle(String.format("%s--%d", DEFAULT_FILENAME, i));
64
65                // Prepare the mock server with a standard response
66                enqueueResponse(HTTP_OK, blobData);
67
68                Log.i(LOG_TAG, "issuing request: " + i);
69                long reqId = mDownloadManager.enqueue(request);
70                reqs[i] = reqId;
71            }
72
73            // wait for the download to complete or timeout
74            waitForDownloadsOrTimeout(WAIT_FOR_DOWNLOAD_POLL_TIME, MAX_WAIT_FOR_DOWNLOAD_TIME);
75            cursor = mDownloadManager.query(new Query());
76            assertEquals(NUM_FILES, cursor.getCount());
77            Log.i(LOG_TAG, "Verified number of downloads in download manager is what we expect.");
78            while (cursor.moveToNext()) {
79                int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
80                String filename = cursor.getString(cursor.getColumnIndex(
81                        DownloadManager.COLUMN_URI));
82                String errorString = String.format("File %s failed to download successfully. " +
83                        "Status code: %d", filename, status);
84                assertEquals(errorString, DownloadManager.STATUS_SUCCESSFUL, status);
85            }
86            Log.i(LOG_TAG, "Verified each download was successful.");
87            assertEquals(NUM_FILES, receiver.numDownloadsCompleted());
88            Log.i(LOG_TAG, "Verified number of completed downloads in our receiver.");
89
90            // Verify that for each request, we can open the downloaded file
91            for (int i = 0; i < NUM_FILES; ++i) {
92                ParcelFileDescriptor pfd = mDownloadManager.openDownloadedFile(reqs[i]);
93                pfd.close();
94            }
95            Log.i(LOG_TAG, "Verified we can open each file.");
96        } finally {
97            if (cursor != null) {
98                cursor.close();
99            }
100            mContext.unregisterReceiver(receiver);
101            removeAllCurrentDownloads();
102        }
103    }
104
105    /**
106     * Tests trying to download a large file (50M bytes).
107     */
108    public void testDownloadLargeFile() throws Exception {
109        long fileSize = 50000000L;  // note: kept relatively small to not exceed /cache dir size
110        File largeFile = createFileOnSD(null, fileSize, DataType.TEXT, null);
111        MultipleDownloadsCompletedReceiver receiver = registerNewMultipleDownloadsReceiver();
112
113        try {
114            long dlRequest = doStandardEnqueue(largeFile);
115
116             // wait for the download to complete
117            waitForDownloadOrTimeout(dlRequest);
118
119            ParcelFileDescriptor pfd = mDownloadManager.openDownloadedFile(dlRequest);
120            verifyFileContents(pfd, largeFile);
121            verifyFileSize(pfd, largeFile.length());
122
123            assertEquals(1, receiver.numDownloadsCompleted());
124            mContext.unregisterReceiver(receiver);
125        } catch (Exception e) {
126            throw e;
127        } finally {
128            largeFile.delete();
129        }
130    }
131
132    /**
133     * Tests trying to download a large file (~300M bytes) when there's not enough space in cache
134     */
135    public void testInsufficientSpace() throws Exception {
136        long fileSize = 300000000L;
137        File largeFile = createFileOnSD(null, fileSize, DataType.TEXT, null);
138
139        Cursor cursor = null;
140        try {
141            long dlRequest = doStandardEnqueue(largeFile);
142
143             // wait for the download to complete
144            waitForDownloadOrTimeout(dlRequest);
145
146            cursor = getCursor(dlRequest);
147            verifyInt(cursor, DownloadManager.COLUMN_STATUS, DownloadManager.STATUS_FAILED);
148            verifyInt(cursor, DownloadManager.COLUMN_ERROR_CODE,
149                    DownloadManager.ERROR_INSUFFICIENT_SPACE);
150        } finally {
151            if (cursor != null) {
152                cursor.close();
153            }
154            largeFile.delete();
155        }
156    }
157}
158