def test_run_tx_unretriable_error(self): sess = dict(n=0) errs = [UserAborted, TXTimeout, ConnectionLoss, None] def _tx(tx, err): sess['n'] += 1 foo = tx.lock_get('foo') foo.v = 100 tx.set(foo) if err is not None: raise err() else: tx.commit() for err in errs: try: zktx.run_tx(zkhost, _tx, args=(err, )) except TXError as e: dd(repr(e)) self.assertEqual(len(errs), sess['n']) tx = ZKTransaction(zkhost) rst, ver = tx.zkstorage.record.get('foo') self.assertEqual(100, rst[-1])
def test_run_tx_conn_loss(self): def _tx(tx): tx.lock_get('foo') tx.zke.stop() tx.commit() try: zktx.run_tx(zkhost, _tx) self.fail('ConnectionLoss expected') except ConnectionLoss as e: dd(repr(e))
def test_run_tx_timeout(self): def _tx(tx): tx.lock_get('foo') time.sleep(0.5) tx.commit() try: zktx.run_tx(zkhost, _tx, timeout=0.4) self.fail('TXTimeout expected') except TXTimeout as e: dd(repr(e))
def test_txid_in_run_tx(self): txids = [] count = iter([1, 2, 3]) def _tx_func(tx): for i in count: txids.append(tx.txid) if i != 3: raise Deadlock() zktx.run_tx(zkhost, _tx_func, txid=5) dd(txids) self.assertEqual(5, txids[0]) for i in txids[1:]: self.assertNotEqual(5, i)
def test_run_tx_lock_timeout(self): def _tx0(tx): tx.begin() tx.lock_get('foo') time.sleep(2) def _tx1(tx): tx.lock_get('foo') time.sleep(0.2) tx.commit() th = threadutil.start_daemon(_tx0, args=(ZKTransaction(zkhost, txid=0),)) try: zktx.run_tx(zkhost, _tx1, lock_timeout=0.4) self.fail('TXTimeout expected') except TXTimeout as e: dd(repr(e)) th.join()
def test_run_tx_lock_timeout(self): def _tx0(tx): tx.begin() tx.lock_get('foo') time.sleep(2) def _tx1(tx): tx.lock_get('foo') time.sleep(0.2) tx.commit() th = threadutil.start_daemon(_tx0, args=(ZKTransaction(zkhost, txid=0), )) try: zktx.run_tx(zkhost, _tx1, lock_timeout=0.4) self.fail('TXTimeout expected') except TXTimeout as e: dd(repr(e)) th.join()
def test_run_tx(self): def _tx(tx): foo = tx.lock_get('foo') foo.v = 100 tx.set(foo) tx.commit() status, txid = zktx.run_tx(zkhost, _tx) self.assertEqual((COMMITTED, 1), (status, txid)) t = ZKTransaction(zkhost) rst, ver = t.zkstorage.record.get('foo') self.assertEqual(100, rst[-1]) rst, ver = self.zk.get('tx/journal_id_set') self.assertEqual({COMMITTED: [[0, 1]], PURGED: [], }, utfjson.load(rst))
def test_run_tx(self): def _tx(tx): foo = tx.lock_get('foo') foo.v = 100 tx.set(foo) tx.commit() status, txid = zktx.run_tx(zkhost, _tx) self.assertEqual((COMMITTED, 1), (status, txid)) t = ZKTransaction(zkhost) rst, ver = t.zkstorage.record.get('foo') self.assertEqual(100, rst[-1]) rst, ver = self.zk.get('tx/journal_id_set') self.assertEqual({ COMMITTED: [[0, 1]], PURGED: [], }, utfjson.load(rst))
def test_run_tx_retriable_error(self): errs = [Deadlock, None] def _tx(tx): foo = tx.lock_get('foo') foo.v = 100 tx.set(foo) err = errs.pop(0) if err is not None: raise err() else: tx.commit() status, txid = zktx.run_tx(zkhost, _tx) # 1 error tried and one commit self.assertEqual((COMMITTED, 2), (status, txid)) t = ZKTransaction(zkhost) rst, ver = t.zkstorage.record.get('foo') self.assertEqual(100, rst[-1])