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