1 /* 2 * Copyright (C) 2017 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 android.database.sqlite; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertTrue; 21 22 import android.content.Context; 23 import android.os.HandlerThread; 24 import android.support.test.InstrumentationRegistry; 25 import android.support.test.filters.SmallTest; 26 import android.support.test.runner.AndroidJUnit4; 27 import android.util.Log; 28 29 import org.junit.After; 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 import java.io.File; 35 36 /** 37 * Tests for {@link SQLiteConnectionPool} 38 * 39 * <p>Run with: bit FrameworksCoreTests:android.database.sqlite.SQLiteConnectionPoolTest 40 */ 41 @RunWith(AndroidJUnit4.class) 42 @SmallTest 43 public class SQLiteConnectionPoolTest { 44 private static final String TAG = "SQLiteConnectionPoolTest"; 45 46 private Context mContext; 47 private File mTestDatabase; 48 private SQLiteDatabaseConfiguration mTestConf; 49 50 51 @Before 52 public void setup() { 53 mContext = InstrumentationRegistry.getContext(); 54 SQLiteDatabase db = SQLiteDatabase 55 .openOrCreateDatabase(mContext.getDatabasePath("pool_test"), null); 56 mTestDatabase = new File(db.getPath()); 57 Log.i(TAG, "setup: created " + mTestDatabase); 58 db.close(); 59 mTestConf = new SQLiteDatabaseConfiguration(mTestDatabase.getPath(), 0); 60 } 61 62 @After 63 public void teardown() { 64 if (mTestDatabase != null) { 65 Log.i(TAG, "teardown: deleting " + mTestDatabase); 66 SQLiteDatabase.deleteDatabase(mTestDatabase); 67 } 68 } 69 70 @Test 71 public void testCloseIdleConnections() throws InterruptedException { 72 HandlerThread thread = new HandlerThread("test-close-idle-connections-thread"); 73 Log.i(TAG, "Starting " + thread.getName()); 74 thread.start(); 75 SQLiteConnectionPool pool = SQLiteConnectionPool.open(mTestConf); 76 pool.setupIdleConnectionHandler(thread.getLooper(), 100); 77 SQLiteConnection c1 = pool.acquireConnection("pragma user_version", 0, null); 78 assertEquals("First connection should be returned", 0, c1.getConnectionId()); 79 pool.releaseConnection(c1); 80 SQLiteConnection c2 = pool.acquireConnection("pragma user_version", 0, null); 81 assertTrue("Returned connection should be the same", c1 == c2); 82 pool.releaseConnection(c2); 83 Thread.sleep(200); 84 SQLiteConnection c3 = pool.acquireConnection("pragma user_version", 0, null); 85 assertTrue("New connection should be returned", c1 != c3); 86 assertEquals("New connection should be returned", 1, c3.getConnectionId()); 87 pool.releaseConnection(c3); 88 pool.close(); 89 thread.quit(); 90 } 91 } 92