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