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.");
}
}
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();
}
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();
}
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();
}
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();
}
}
Aggregations