Search in sources :

Example 6 with Transaction

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

the class BriteDatabaseTest method callingEndMultipleTimesThrows.

@Test
public void callingEndMultipleTimesThrows() {
    Transaction transaction = db.newTransaction();
    transaction.end();
    try {
        transaction.end();
        fail();
    } catch (IllegalStateException e) {
        assertThat(e).hasMessage("Not in transaction.");
    }
}
Also used : Transaction(com.squareup.sqlbrite.BriteDatabase.Transaction) Test(org.junit.Test)

Example 7 with Transaction

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

the class BriteDatabaseTest method transactionIsCloseable.

@Test
public void transactionIsCloseable() throws IOException {
    db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).subscribe(o);
    o.assertCursor().hasRow("alice", "Alice Allison").hasRow("bob", "Bob Bobberson").hasRow("eve", "Eve Evenson").isExhausted();
    Transaction transaction = db.newTransaction();
    //noinspection UnnecessaryLocalVariable
    // Verify type is implemented.
    Closeable closeableTransaction = transaction;
    try {
        db.insert(TABLE_EMPLOYEE, employee("john", "John Johnson"));
        db.insert(TABLE_EMPLOYEE, employee("nick", "Nick Nickers"));
        transaction.markSuccessful();
    } finally {
        closeableTransaction.close();
    }
    o.assertCursor().hasRow("alice", "Alice Allison").hasRow("bob", "Bob Bobberson").hasRow("eve", "Eve Evenson").hasRow("john", "John Johnson").hasRow("nick", "Nick Nickers").isExhausted();
}
Also used : Transaction(com.squareup.sqlbrite.BriteDatabase.Transaction) Closeable(java.io.Closeable) Test(org.junit.Test)

Example 8 with Transaction

use of com.squareup.sqlbrite.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.sqlbrite.BriteDatabase.Transaction) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 9 with Transaction

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

the class BriteDatabaseTest method nestedTransactionsOnlyNotifyOnce.

@Test
public void nestedTransactionsOnlyNotifyOnce() {
    db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).subscribe(o);
    o.assertCursor().hasRow("alice", "Alice Allison").hasRow("bob", "Bob Bobberson").hasRow("eve", "Eve Evenson").isExhausted();
    Transaction transactionOuter = db.newTransaction();
    try {
        db.insert(TABLE_EMPLOYEE, employee("john", "John Johnson"));
        Transaction transactionInner = db.newTransaction();
        try {
            db.insert(TABLE_EMPLOYEE, employee("nick", "Nick Nickers"));
            transactionInner.markSuccessful();
        } finally {
            transactionInner.end();
        }
        transactionOuter.markSuccessful();
    } finally {
        transactionOuter.end();
    }
    o.assertCursor().hasRow("alice", "Alice Allison").hasRow("bob", "Bob Bobberson").hasRow("eve", "Eve Evenson").hasRow("john", "John Johnson").hasRow("nick", "Nick Nickers").isExhausted();
}
Also used : Transaction(com.squareup.sqlbrite.BriteDatabase.Transaction) Test(org.junit.Test)

Example 10 with Transaction

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

the class BriteDatabaseTest method transactionCreatedFromTransactionNotificationWorks.

@Test
public void transactionCreatedFromTransactionNotificationWorks() {
    // Tests the case where a transaction is created in the subscriber to a query which gets
    // notified as the result of another transaction being committed. With improper ordering, this
    // can result in creating a new transaction before the old is committed on the underlying DB.
    db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).subscribe(new Action1<Query>() {

        @Override
        public void call(Query query) {
            db.newTransaction().end();
        }
    });
    Transaction transaction = db.newTransaction();
    try {
        db.insert(TABLE_EMPLOYEE, employee("john", "John Johnson"));
        transaction.markSuccessful();
    } finally {
        transaction.end();
    }
}
Also used : Query(com.squareup.sqlbrite.SqlBrite.Query) Transaction(com.squareup.sqlbrite.BriteDatabase.Transaction) Test(org.junit.Test)

Aggregations

Transaction (com.squareup.sqlbrite.BriteDatabase.Transaction)14 Test (org.junit.Test)14 Query (com.squareup.sqlbrite.SqlBrite.Query)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TargetApi (android.annotation.TargetApi)1 SdkSuppress (android.support.test.filters.SdkSuppress)1 Employee (com.squareup.sqlbrite.TestDb.Employee)1 Closeable (java.io.Closeable)1