DatabaseLockTest.java revision 1a44d5dcabc18cd5ef111f732ccff91683a1a093
1/* 2 * Copyright (C) 2009 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 android.database; 18 19import android.app.Activity; 20import android.content.Context; 21import android.database.sqlite.SQLiteDatabase; 22import android.test.suitebuilder.annotation.LargeTest; 23import android.test.suitebuilder.annotation.Suppress; 24import android.util.Log; 25import java.io.File; 26import java.util.concurrent.atomic.AtomicInteger; 27import android.test.AndroidTestCase; 28 29import junit.framework.TestCase; 30 31/* 32 * This is a series of unit tests for database locks. 33 * 34 * Suppress these tests for now, since they have has inconsistent results. 35 * This should be turned into a performance tracking test. 36 */ 37@Suppress 38public class DatabaseLockTest extends AndroidTestCase { 39 40 private static final int NUM_ITERATIONS = 100; 41 private static final int SLEEP_TIME = 30; 42 private static final int MAX_ALLOWED_LATENCY_TIME = 30; 43 private SQLiteDatabase mDatabase; 44 private File mDatabaseFile; 45 private AtomicInteger mCounter = new AtomicInteger(); 46 47 @Override 48 protected void setUp() throws Exception { 49 super.setUp(); 50 File parentDir = getContext().getFilesDir(); 51 mDatabaseFile = new File(parentDir, "database_test.db"); 52 53 if (mDatabaseFile.exists()) { 54 mDatabaseFile.delete(); 55 } 56 mDatabase = SQLiteDatabase.openOrCreateDatabase(mDatabaseFile.getPath(), null); 57 assertNotNull(mDatabase); 58 } 59 60 @Override 61 protected void tearDown() throws Exception { 62 mDatabase.close(); 63 mDatabaseFile.delete(); 64 super.tearDown(); 65 } 66 67 /* 68 * testLockFairness() tests the fairness of prioritizing multiple threads 69 * attempting to access a database concurrently. 70 * This test is intended to verify that, when two threads are accessing the 71 * same database at the same time with the same prioritization, neither thread 72 * is locked out and prevented from accessing the database. 73 */ 74 @Suppress 75 public void testLockFairness() { 76 startDatabaseFairnessThread(); 77 int previous = 0; 78 for (int i = 0; i < NUM_ITERATIONS; i++) { 79 mDatabase.beginTransaction(); 80 int val = mCounter.get(); 81 if (i == 0) { 82 previous = val - i; 83 } 84 assertTrue(previous == (val - i)); 85 try { 86 Thread.currentThread().sleep(SLEEP_TIME); 87 } catch (InterruptedException e) { 88 // ignore 89 } 90 mDatabase.endTransaction(); 91 } 92 } 93 94 /* 95 * This function is to create the second thread for testLockFairness() test. 96 */ 97 private void startDatabaseFairnessThread() { 98 Thread thread = new DatabaseFairnessThread(); 99 thread.start(); 100 } 101 102 private class DatabaseFairnessThread extends Thread { 103 @Override 104 public void run() { 105 for (int i = 0; i < NUM_ITERATIONS; i++) { 106 mDatabase.beginTransaction(); 107 int val = mCounter.incrementAndGet(); 108 try { 109 Thread.currentThread().sleep(SLEEP_TIME); 110 } catch (InterruptedException e) { 111 // ignore 112 } 113 mDatabase.endTransaction(); 114 } 115 } 116 } 117 118 /* 119 * testLockLatency() tests the latency of database locks. 120 * This test is intended to verify that, even when two threads are accessing 121 * the same database, the locking/unlocking of the database is done within an 122 * appropriate amount of time (MAX_ALLOWED_LATENCY_TIME). 123 */ 124 @Suppress 125 public void testLockLatency() { 126 startDatabaseLatencyThread(); 127 int previous = 0; 128 long sumTime = 0; 129 long maxTime = 0; 130 for (int i = 0; i < NUM_ITERATIONS; i++) { 131 long startTime = System.currentTimeMillis(); 132 mDatabase.beginTransaction(); 133 long endTime = System.currentTimeMillis(); 134 long elapsedTime = endTime - startTime; 135 if (maxTime < elapsedTime) { 136 maxTime = elapsedTime; 137 } 138 sumTime += elapsedTime; 139 try { 140 Thread.currentThread().sleep(SLEEP_TIME); 141 } catch (InterruptedException e) { 142 // ignore 143 } 144 mDatabase.endTransaction(); 145 } 146 long averageTime = sumTime/NUM_ITERATIONS; 147 Log.i("DatabaseLockLatency", "AverageTime: " + averageTime); 148 Log.i("DatabaseLockLatency", "MaxTime: " + maxTime); 149 assertTrue( (averageTime - SLEEP_TIME) <= MAX_ALLOWED_LATENCY_TIME); 150 } 151 152 /* 153 * This function is to create the second thread for testLockLatency() test. 154 */ 155 private void startDatabaseLatencyThread() { 156 Thread thread = new DatabaseLatencyThread(); 157 thread.start(); 158 } 159 160 private class DatabaseLatencyThread extends Thread { 161 @Override 162 public void run() { 163 for (int i = 0; i < NUM_ITERATIONS; i++) 164 { 165 mDatabase.beginTransaction(); 166 try { 167 Thread.currentThread().sleep(SLEEP_TIME); 168 } catch (InterruptedException e) { 169 // ignore 170 } 171 mDatabase.endTransaction(); 172 } 173 } 174 } 175} 176