Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6f35c0ec authored by Vasu Nori's avatar Vasu Nori
Browse files

fix all flaky tests to make them work consistently.

Change-Id: I688f7e058511089bec7fa21e972e23780604d98a
parent 34884643
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -57,7 +57,7 @@ import java.util.concurrent.TimeoutException;
 * Base class for Instrumented tests for the Download Manager.
 */
public class DownloadManagerBaseTest extends InstrumentationTestCase {

    private static final String TAG = "DownloadManagerBaseTest";
    protected DownloadManager mDownloadManager = null;
    protected MockWebServer mServer = null;
    protected String mFileType = "text/plain";
@@ -616,19 +616,22 @@ public class DownloadManagerBaseTest extends InstrumentationTestCase {
        int nextChunkSize = CHUNK_SIZE;
        byte[] randomData = null;
        Random rng = new LoggingRng();
        byte[] chunkSizeData = generateData(nextChunkSize, type, rng);

        try {
            while (remaining > 0) {
                if (remaining < CHUNK_SIZE) {
                    nextChunkSize = (int)remaining;
                    remaining = 0;
                    randomData = generateData(nextChunkSize, type, rng);
                }
                else {
                    remaining -= CHUNK_SIZE;
                    randomData = chunkSizeData;
                }

                randomData = generateData(nextChunkSize, type, rng);
                output.write(randomData);
                Log.i(TAG, "while creating " + fileSize + " file, " +
                        "remaining bytes to be written: " + remaining);
            }
        } catch (IOException e) {
            Log.e(LOG_TAG, "Error writing to file " + file.getAbsolutePath());
+0 −154
Original line number Diff line number Diff line
@@ -20,20 +20,14 @@ import coretestutils.http.MockResponse;

import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.app.DownloadManagerBaseTest.DataType;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.os.StatFs;
import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;

/**
@@ -151,55 +145,6 @@ public class DownloadManagerFunctionalTest extends DownloadManagerBaseTest {
        }
    }

    /**
     * Attempts to download several files simultaneously
     */
    @LargeTest
    public void testMultipleDownloads() throws Exception {
        // need to be sure all current downloads have stopped first
        removeAllCurrentDownloads();
        int NUM_FILES = 10;
        int MAX_FILE_SIZE = 10 * 1024; // 10 kb

        Random r = new LoggingRng();
        for (int i=0; i<NUM_FILES; ++i) {
            int size = r.nextInt(MAX_FILE_SIZE);
            byte[] blobData = generateData(size, DataType.TEXT);

            Uri uri = getServerUri(DEFAULT_FILENAME + i);
            Request request = new Request(uri);
            request.setTitle(String.format("%s--%d", DEFAULT_FILENAME + i, i));

            // Prepare the mock server with a standard response
            enqueueResponse(HTTP_OK, blobData);

            long requestID = mDownloadManager.enqueue(request);
        }

        waitForDownloadsOrTimeout(WAIT_FOR_DOWNLOAD_POLL_TIME, MAX_WAIT_FOR_DOWNLOAD_TIME);
        Cursor cursor = mDownloadManager.query(new Query());
        try {
            assertEquals(NUM_FILES, cursor.getCount());

            if (cursor.moveToFirst()) {
                do {
                    int status = cursor.getInt(cursor.getColumnIndex(
                            DownloadManager.COLUMN_STATUS));
                    String filename = cursor.getString(cursor.getColumnIndex(
                            DownloadManager.COLUMN_URI));
                    String errorString = String.format(
                            "File %s failed to download successfully. Status code: %d",
                            filename, status);
                    assertEquals(errorString, DownloadManager.STATUS_SUCCESSFUL, status);
                } while (cursor.moveToNext());
            }

            assertEquals(NUM_FILES, mReceiver.numDownloadsCompleted());
        } finally {
            cursor.close();
        }
    }

    /**
     * Tests trying to download to SD card when the file with same name already exists.
     */
@@ -478,103 +423,4 @@ public class DownloadManagerFunctionalTest extends DownloadManagerBaseTest {
        // Even tho the server drops the connection, we should still get a completed notification
        assertEquals(1, mReceiver.numDownloadsCompleted());
    }

    /**
     * Tests downloading a file to system cache when there isn't enough space in the system cache 
     * to hold the entire file. DownloadManager deletes enough files to make space for the
     * new download.
     */
    @LargeTest
    public void testDownloadToCacheWithAlmostFullCache() throws Exception {
        int DOWNLOAD_FILE_SIZE = 1024 * 1024; // 1MB

        StatFs fs = new StatFs(CACHE_DIR);
        int blockSize = fs.getBlockSize();
        int availableBlocks = fs.getAvailableBlocks();
        int availableBytes = blockSize * availableBlocks;
        Log.i(TAG, "INITIAL stage, available space in /cache: " + availableBytes);
        File outFile = File.createTempFile("DM_TEST", null, new File(CACHE_DIR));
        byte[] buffer = new byte[blockSize];

        try {
            // fill cache to ensure we don't have enough space - take half the size of the
            // download size, and leave that much freespace left on the cache partition
            if (DOWNLOAD_FILE_SIZE <= availableBytes) {
                int writeSizeBytes = availableBytes - (DOWNLOAD_FILE_SIZE / 2);

                int writeSizeBlocks = writeSizeBytes / blockSize;
                int remainderSizeBlocks = availableBlocks - writeSizeBlocks;

                FileOutputStream fo = null;
                try {
                    fo = new FileOutputStream(outFile);
                    while (fs.getAvailableBlocks() >= remainderSizeBlocks) {
                        fo.write(buffer);
                        fs.restat(CACHE_DIR);
                    }
                } catch (IOException e) {
                    Log.e(LOG_TAG, "error filling file: ", e);
                    throw e;
                } finally {
                    if (fo != null) {
                        fo.close();
                    }
                }
            }

            // /cache should now be almost full. 
            long spaceAvailable = fs.getAvailableBlocks() * blockSize;
            Log.i(TAG, "BEFORE download, available space in /cache: " + spaceAvailable);
            assertTrue(DOWNLOAD_FILE_SIZE > spaceAvailable);

            // try to download 1MB file into /cache - and it should succeed
            byte[] blobData = generateData(DOWNLOAD_FILE_SIZE, DataType.TEXT);
            long dlRequest = doBasicDownload(blobData, DOWNLOAD_TO_SYSTEM_CACHE);
            verifyAndCleanupSingleFileDownload(dlRequest, blobData);
        } finally {
            if (outFile != null) {
                outFile.delete();
            }
        }
    }

    /**
     * Tests that files are not deleted when DOWNLOAD_CACHE_NON_PURGEABLE is set, even if we've
     * run out of space.
     */
    @LargeTest
    public void testDownloadToCacheNonPurgeableWithFullCache() throws Exception {
        int fileSize = 1024 * 1024; // 1MB
        byte[] blobData = generateData(fileSize, DataType.BINARY);
        long dlRequest = -1;

        // Fill up the cache partition with DOWNLOAD_CACHE_NON_PURGEABLE downloads
        // until 500KB is left.
        boolean spaceAvailable = true;
        while (spaceAvailable) {
            // since this tests package has android.permission.DOWNLOAD_CACHE_NON_PURGEABLE
            // permission, downloads are automatically set to be DOWNLOAD_CACHE_NON_PURGEABLE
            dlRequest = enqueueDownloadRequest(blobData, DOWNLOAD_TO_DOWNLOAD_CACHE_DIR);
            waitForDownloadOrTimeout(dlRequest);

            // Check if we've filled up the cache yet
            StatFs fs = new StatFs(Environment.getDownloadCacheDirectory().getAbsolutePath());
            int availableBytes = fs.getBlockSize() * fs.getAvailableBlocks();
            Log.i(TAG, "available space in /cache: " + availableBytes);
            spaceAvailable = (availableBytes > fileSize) ? true : false;
        }

        // Now add one more 1MB download (should not fit in the space left over)
        dlRequest = enqueueDownloadRequest(blobData, DOWNLOAD_TO_DOWNLOAD_CACHE_DIR);
        waitForDownloadOrTimeout(dlRequest);

        // For the last download we should have failed b/c there is not enough space left in cache
        Cursor cursor = getCursor(dlRequest);
        try {
            verifyInt(cursor, DownloadManager.COLUMN_REASON,
                    DownloadManager.ERROR_INSUFFICIENT_SPACE);
        } finally {
            cursor.close();
        }
    }
}
+109 −56
Original line number Diff line number Diff line
@@ -16,21 +16,28 @@

package android.app;


import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.os.StatFs;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.Suppress;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;


/**
 * Integration tests of the DownloadManager API.
 */
public class DownloadManagerStressTest extends DownloadManagerBaseTest {
    private static final String TAG = "DownloadManagerStressTest";
    private final static String CACHE_DIR =
            Environment.getDownloadCacheDirectory().getAbsolutePath();

    /**
     * {@inheritDoc}
@@ -38,8 +45,8 @@ public class DownloadManagerStressTest extends DownloadManagerBaseTest {
    @Override
    public void setUp() throws Exception {
        super.setUp();
        mServer.play(0);
        setWiFiStateOn(true);
        mServer.play();
        removeAllCurrentDownloads();
    }

@@ -59,76 +66,62 @@ public class DownloadManagerStressTest extends DownloadManagerBaseTest {
    }

    /**
     * Attempts to downloading thousands of files simultaneously
     * don't run this test - downloadmanager needs to allow only a few simultaneous downloads.
     * Attempts to download several files simultaneously
     */
    @Suppress
    public void testDownloadThousands() throws Exception {
        int NUM_FILES = 1500;
        int MAX_FILE_SIZE = 3000;
        long[] reqs = new long[NUM_FILES];

    @LargeTest
    public void testMultipleDownloads() throws Exception {
        // need to be sure all current downloads have stopped first
        MultipleDownloadsCompletedReceiver receiver = registerNewMultipleDownloadsReceiver();
        Cursor cursor = null;
        try {
        removeAllCurrentDownloads();
        int NUM_FILES = 10;
        int MAX_FILE_SIZE = 10 * 1024; // 10 kb

        Random r = new LoggingRng();
        for (int i=0; i<NUM_FILES; ++i) {
            int size = r.nextInt(MAX_FILE_SIZE);
            byte[] blobData = generateData(size, DataType.TEXT);

                Uri uri = getServerUri(DEFAULT_FILENAME);
            Uri uri = getServerUri(DEFAULT_FILENAME + i);
            Request request = new Request(uri);
                request.setTitle(String.format("%s--%d", DEFAULT_FILENAME, i));
            request.setTitle(String.format("%s--%d", DEFAULT_FILENAME + i, i));

            // Prepare the mock server with a standard response
            enqueueResponse(HTTP_OK, blobData);

                Log.i(LOG_TAG, "issuing request: " + i);
                long reqId = mDownloadManager.enqueue(request);
                reqs[i] = reqId;
            long requestID = mDownloadManager.enqueue(request);
        }

            // wait for the download to complete or timeout
            waitForDownloadsOrTimeout(WAIT_FOR_DOWNLOAD_POLL_TIME,
                    MAX_WAIT_FOR_LARGE_DOWNLOAD_TIME);
            cursor = mDownloadManager.query(new Query());
        waitForDownloadsOrTimeout(WAIT_FOR_DOWNLOAD_POLL_TIME, MAX_WAIT_FOR_DOWNLOAD_TIME);
        Cursor cursor = mDownloadManager.query(new Query());
        try {
            assertEquals(NUM_FILES, cursor.getCount());
            Log.i(LOG_TAG, "Verified number of downloads in download manager is what we expect.");
            while (cursor.moveToNext()) {
                int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));

            if (cursor.moveToFirst()) {
                do {
                    int status = cursor.getInt(cursor.getColumnIndex(
                            DownloadManager.COLUMN_STATUS));
                    String filename = cursor.getString(cursor.getColumnIndex(
                            DownloadManager.COLUMN_URI));
                String errorString = String.format("File %s failed to download successfully. " +
                        "Status code: %d", filename, status);
                    String errorString = String.format(
                            "File %s failed to download successfully. Status code: %d",
                            filename, status);
                    assertEquals(errorString, DownloadManager.STATUS_SUCCESSFUL, status);
                } while (cursor.moveToNext());
            }
            Log.i(LOG_TAG, "Verified each download was successful.");
            assertEquals(NUM_FILES, receiver.numDownloadsCompleted());
            Log.i(LOG_TAG, "Verified number of completed downloads in our receiver.");

            // Verify that for each request, we can open the downloaded file
            for (int i = 0; i < NUM_FILES; ++i) {
                ParcelFileDescriptor pfd = mDownloadManager.openDownloadedFile(reqs[i]);
                pfd.close();
            }
            Log.i(LOG_TAG, "Verified we can open each file.");
            assertEquals(NUM_FILES, mReceiver.numDownloadsCompleted());
        } finally {
            if (cursor != null) {
            cursor.close();
        }
            mContext.unregisterReceiver(receiver);
            removeAllCurrentDownloads();
        }
    }

    /**
     * Tests trying to download a large file (50M bytes).
     */
    @LargeTest
    public void testDownloadLargeFile() throws Exception {
        long fileSize = 50000000L;  // note: kept relatively small to not exceed /cache dir size
        Log.i(TAG, "creating a file of size: " + fileSize);
        File largeFile = createFileOnSD(null, fileSize, DataType.TEXT, null);
        Log.i(TAG, "DONE creating a file of size: " + fileSize);
        MultipleDownloadsCompletedReceiver receiver = registerNewMultipleDownloadsReceiver();

        try {
@@ -149,4 +142,64 @@ public class DownloadManagerStressTest extends DownloadManagerBaseTest {
            largeFile.delete();
        }
    }


    /**
     * Tests downloading a file to system cache when there isn't enough space in the system cache 
     * to hold the entire file. DownloadManager deletes enough files to make space for the
     * new download.
     */
    @LargeTest
    public void testDownloadToCacheWithAlmostFullCache() throws Exception {
        int DOWNLOAD_FILE_SIZE = 1024 * 1024; // 1MB

        StatFs fs = new StatFs(CACHE_DIR);
        int blockSize = fs.getBlockSize();
        int availableBlocks = fs.getAvailableBlocks();
        int availableBytes = blockSize * availableBlocks;
        Log.i(TAG, "INITIAL stage, available space in /cache: " + availableBytes);
        File outFile = File.createTempFile("DM_TEST", null, new File(CACHE_DIR));
        byte[] buffer = new byte[blockSize];

        try {
            // fill cache to ensure we don't have enough space - take half the size of the
            // download size, and leave that much freespace left on the cache partition
            if (DOWNLOAD_FILE_SIZE <= availableBytes) {
                int writeSizeBytes = availableBytes - (DOWNLOAD_FILE_SIZE / 2);

                int writeSizeBlocks = writeSizeBytes / blockSize;
                int remainderSizeBlocks = availableBlocks - writeSizeBlocks;

                FileOutputStream fo = null;
                try {
                    fo = new FileOutputStream(outFile);
                    while (fs.getAvailableBlocks() >= remainderSizeBlocks) {
                        fo.write(buffer);
                        fs.restat(CACHE_DIR);
                    }
                } catch (IOException e) {
                    Log.e(LOG_TAG, "error filling file: ", e);
                    throw e;
                } finally {
                    if (fo != null) {
                        fo.close();
                    }
                }
            }

            // /cache should now be almost full. 
            long spaceAvailable = fs.getAvailableBlocks() * blockSize;
            Log.i(TAG, "BEFORE download, available space in /cache: " + spaceAvailable);
            assertTrue(DOWNLOAD_FILE_SIZE > spaceAvailable);

            // try to download 1MB file into /cache - and it should succeed
            byte[] blobData = generateData(DOWNLOAD_FILE_SIZE, DataType.TEXT);
            long dlRequest = doBasicDownload(blobData, DOWNLOAD_TO_SYSTEM_CACHE);
            verifyAndCleanupSingleFileDownload(dlRequest, blobData);
        } finally {
            if (outFile != null) {
                outFile.delete();
            }
        }
    }
}