ThreadingTest.java revision ae6856b0fca5215f45619dd031a7e7beae7bd8cc
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 android.app.DownloadManager;
20import android.test.suitebuilder.annotation.LargeTest;
21
22/**
23 * Download manager tests that require multithreading.
24 */
25@LargeTest
26public class ThreadingTest extends AbstractPublicApiTest {
27    private static class FakeSystemFacadeWithThreading extends FakeSystemFacade {
28        @Override
29        public void startThread(Thread thread) {
30            thread.start();
31        }
32    }
33
34    public ThreadingTest() {
35        super(new FakeSystemFacadeWithThreading());
36    }
37
38    @Override
39    protected void tearDown() throws Exception {
40        Thread.sleep(50); // give threads a chance to finish
41        super.tearDown();
42    }
43
44    /**
45     * Test for race conditions when the service is flooded with startService() calls while running
46     * a download.
47     */
48    public void testFloodServiceWithStarts() throws Exception {
49        enqueueResponse(buildResponse(HTTP_OK, FILE_CONTENT));
50        Download download = enqueueRequest(getRequest());
51        while (download.getStatus() != DownloadManager.STATUS_SUCCESSFUL) {
52            startService(null);
53            Thread.sleep(10);
54        }
55    }
56}
57