Search in sources :

Example 16 with Transaction

use of com.squareup.sqlbrite3.BriteDatabase.Transaction in project sqlbrite by square.

the class BriteDatabaseTest method querySubscribedToDuringTransactionOnDifferentThread.

@Test
public void querySubscribedToDuringTransactionOnDifferentThread() throws InterruptedException {
    Transaction transaction = db.newTransaction();
    final CountDownLatch latch = new CountDownLatch(1);
    new Thread() {

        @Override
        public void run() {
            db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).subscribe(o);
            latch.countDown();
        }
    }.start();
    // Wait for the thread to block on initial query.
    Thread.sleep(500);
    o.assertNoMoreEvents();
    // Allow other queries to continue.
    transaction.end();
    // Wait for thread to observe initial query.
    latch.await(500, MILLISECONDS);
    o.assertCursor().hasRow("alice", "Alice Allison").hasRow("bob", "Bob Bobberson").hasRow("eve", "Eve Evenson").isExhausted();
}
Also used : Transaction(com.squareup.sqlbrite3.BriteDatabase.Transaction) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

Transaction (com.squareup.sqlbrite3.BriteDatabase.Transaction)16 Test (org.junit.Test)16 SimpleSQLiteQuery (android.arch.persistence.db.SimpleSQLiteQuery)4 Query (com.squareup.sqlbrite3.SqlBrite.Query)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TargetApi (android.annotation.TargetApi)1 SdkSuppress (android.support.test.filters.SdkSuppress)1 Employee (com.squareup.sqlbrite3.TestDb.Employee)1 Closeable (java.io.Closeable)1