def test_concurrent_non_overlapping(self): with rethinktx.Transaction(self.conn) as tx1, \ rethinktx.Transaction(self.conn) as tx2: tx1.table('table1').put('key1', 'data1') tx2.table('table1').put('key2', 'data2') with rethinktx.Transaction(self.conn) as tx: self.assertEqual('data1', tx.table('table1').get('key1')) self.assertEqual('data2', tx.table('table1').get('key2'))
def test_concurrent_overlapping(self): with rethinktx.Transaction(self.conn) as tx1, \ rethinktx.Transaction(self.conn) as tx2: tx1.table('table1').put('key', 'data1') tx2.table('table1').put('key', 'data2') with self.assertRaises(rethinktx.OptimisticLockFailure): tx1.commit() # tx2 should win since it have greater XID/timestamp with rethinktx.Transaction(self.conn) as tx: self.assertEqual('data2', tx.table('table1').get('key'))
def _total_balance(self): with mocks.get_connection() as conn: total_balance = 0 with rethinktx.Transaction(conn) as tx: accounts_tbl = tx.table('accounts') for account_id in self.account_ids: total_balance += accounts_tbl.get(account_id)['balance'] return total_balance
def _create_accounts(conn, num_accounts): account_ids = [] with rethinktx.Transaction(conn) as tx: accounts_tbl = tx.table('accounts') for i in six.moves.range(num_accounts): key = str(uuid.uuid4()) account_ids.append(key) accounts_tbl.put(key, {'index': i, 'balance': 0}) return account_ids
def test_write_to_changed_committed(self): with rethinktx.Transaction(self.conn) as tx: tx.table('table1').put('key-1', 'data1') with rethinktx.Transaction(self.conn) as tx1, \ rethinktx.Transaction(self.conn) as tx2: # Read key-1 through tx2 in order to fix current value for tx2 self.assertEqual('data1', tx2.table('table1').get('key-1')) # Write key-1 through tx1 and commit it, making value red by tx2 # stale tx1.table('table1').put('key-1', 'modified data1') tx1.commit() # Now write to key-1 through tx2 should fail with optimisic lock # failure with self.assertRaises(rethinktx.OptimisticLockFailure): tx2.table('table1').put('key-1', 'what a failure') tx2.abort()
def perform_work(conn, account_ids): for _ in six.moves.range(NUM_ITERATIONS): acct_from_id, acct_to_id = random.sample(account_ids, 2) try: with rethinktx.Transaction(conn) as tx: accounts_tbl = tx.table('accounts') acct_from = accounts_tbl.get(acct_from_id) acct_to = accounts_tbl.get(acct_to_id) acct_from['balance'] -= 10 acct_to['balance'] += 10 accounts_tbl.put(acct_from_id, acct_from) accounts_tbl.put(acct_to_id, acct_to) except rethinktx.OptimisticLockFailure: pass except rethinkdb.ReqlAvailabilityError: pass
def test_put_get(self): with rethinktx.Transaction(self.conn) as tx1: tx1.table('table1').put('key', 'data') with rethinktx.Transaction(self.conn) as tx2: self.assertEqual('data', tx2.table('table1').get('key'))
def test_get_non_existent_return_default(self): with rethinktx.Transaction(self.conn) as tx: uniq_val = object() self.assertIs(uniq_val, tx.table('table1').get('key', uniq_val))
def test_get_non_existent_raise_notfound(self): with rethinktx.Transaction(self.conn) as tx: with self.assertRaises(rethinktx.NotFound): tx.table('table1').get('key')