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 com.android.providers.downloads; 18 19import static java.net.HttpURLConnection.HTTP_OK; 20 21import android.app.DownloadManager; 22import android.test.suitebuilder.annotation.LargeTest; 23import android.util.Pair; 24 25import com.google.android.collect.Lists; 26import com.google.android.collect.Sets; 27import com.google.mockwebserver.MockResponse; 28import com.google.mockwebserver.SocketPolicy; 29 30import java.util.List; 31import java.util.Set; 32 33/** 34 * Download manager tests that require multithreading. 35 */ 36@LargeTest 37public class ThreadingTest extends AbstractPublicApiTest { 38 public ThreadingTest() { 39 super(new FakeSystemFacade()); 40 } 41 42 @Override 43 protected void tearDown() throws Exception { 44 Thread.sleep(50); // give threads a chance to finish 45 super.tearDown(); 46 } 47 48 /** 49 * Test for race conditions when the service is flooded with startService() calls while running 50 * a download. 51 */ 52 public void testFloodServiceWithStarts() throws Exception { 53 enqueueResponse(buildResponse(HTTP_OK, FILE_CONTENT)); 54 Download download = enqueueRequest(getRequest()); 55 while (download.getStatus() != DownloadManager.STATUS_SUCCESSFUL) { 56 startService(null); 57 Thread.sleep(10); 58 } 59 } 60 61 public void testFilenameRace() throws Exception { 62 final List<Pair<Download, String>> downloads = Lists.newArrayList(); 63 64 // Request dozen files at once with same name 65 for (int i = 0; i < 12; i++) { 66 final String body = "DOWNLOAD " + i + " CONTENTS"; 67 enqueueResponse(new MockResponse().setResponseCode(HTTP_OK).setBody(body) 68 .setHeader("Content-type", "text/plain") 69 .setSocketPolicy(SocketPolicy.DISCONNECT_AT_END)); 70 71 final Download d = enqueueRequest(getRequest()); 72 downloads.add(Pair.create(d, body)); 73 } 74 75 // Kick off downloads in parallel 76 final long startMillis = mSystemFacade.currentTimeMillis(); 77 startService(null); 78 79 for (Pair<Download,String> d : downloads) { 80 d.first.waitForStatus(DownloadManager.STATUS_SUCCESSFUL, startMillis); 81 } 82 83 // Ensure that contents are clean and filenames unique 84 final Set<String> seenFiles = Sets.newHashSet(); 85 86 for (Pair<Download, String> d : downloads) { 87 final String file = d.first.getStringField(DownloadManager.COLUMN_LOCAL_FILENAME); 88 if (!seenFiles.add(file)) { 89 fail("Another download already claimed " + file); 90 } 91 92 final String expected = d.second; 93 final String actual = d.first.getContents(); 94 assertEquals(expected, actual); 95 } 96 } 97} 98