<s> import sys <EOL> import time <EOL> import unittest <EOL> import base64 <EOL> import re <EOL> import threading <EOL> from test import test_support <EOL> from dbconstants import * <EOL> import zkappscale . zktransaction <EOL> class TestZKTransaction ( unittest . TestCase ) : <EOL> def test_getTransactionId ( self ) : <EOL> app = "<STR_LIT>" <EOL> id = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( id > <NUM_LIT:0> ) <EOL> def test_locksimple ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT:root>" <EOL> txid = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid > <NUM_LIT:0> ) <EOL> ret = self . zk . acquire_lock ( app , txid , key ) <EOL> self . assertTrue ( ret ) <EOL> ret = self . zk . release_lock ( app , txid ) <EOL> self . assertTrue ( ret ) <EOL> def test_lockpathkey ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT>" <EOL> txid = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid > <NUM_LIT:0> ) <EOL> ret = self . zk . acquire_lock ( app , txid , key ) <EOL> self . assertTrue ( ret ) <EOL> ret = self . zk . release_lock ( app , txid ) <EOL> self . assertTrue ( ret ) <EOL> def test_lockspecialcharapp ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT>" <EOL> txid = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid > <NUM_LIT:0> ) <EOL> ret = self . zk . acquire_lock ( app , txid , key ) <EOL> self . assertTrue ( ret ) <EOL> ret = self . zk . release_lock ( app , txid ) <EOL> self . assertTrue ( ret ) <EOL> def test_locklongkey ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT>" <EOL> txid = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid > <NUM_LIT:0> ) <EOL> ret = self . zk . acquire_lock ( app , txid , key ) <EOL> self . assertTrue ( ret ) <EOL> ret = self . zk . release_lock ( app , txid ) <EOL> self . assertTrue ( ret ) <EOL> def test_locktwice ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT:root>" <EOL> txid = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid > <NUM_LIT:0> ) <EOL> ret = self . zk . acquire_lock ( app , txid , key ) <EOL> self . assertTrue ( ret ) <EOL> ret = self . zk . acquire_lock ( app , txid , key ) <EOL> self . assertTrue ( ret ) <EOL> ret = self . zk . release_lock ( app , txid ) <EOL> self . assertTrue ( ret ) <EOL> def test_lockdifferentkey ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT:root>" <EOL> key2 = "<STR_LIT>" <EOL> txid = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid > <NUM_LIT:0> ) <EOL> ret = self . zk . acquire_lock ( app , txid , key ) <EOL> self . assertTrue ( ret ) <EOL> try : <EOL> ret = self . zk . acquire_lock ( app , txid , key2 ) <EOL> self . fail ( ) <EOL> except zkappscale . zktransaction . ZKTransactionException as e : <EOL> print e <EOL> self . assertEqual ( zkappscale . zktransaction . ZKTransactionException . TYPE_DIFFERENT_ROOTKEY , e . getType ( ) ) <EOL> ret = self . zk . release_lock ( app , txid ) <EOL> self . assertTrue ( ret ) <EOL> def test_lockafterrelease ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT:root>" <EOL> txid = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid > <NUM_LIT:0> ) <EOL> ret = self . zk . acquire_lock ( app , txid , key ) <EOL> self . assertTrue ( ret ) <EOL> ret = self . zk . release_lock ( app , txid ) <EOL> self . assertTrue ( ret ) <EOL> try : <EOL> ret = self . zk . acquire_lock ( app , txid , key ) <EOL> self . fail ( ) <EOL> except zkappscale . zktransaction . ZKTransactionException as e : <EOL> print e <EOL> self . assertEqual ( zkappscale . zktransaction . ZKTransactionException . TYPE_INVALID , e . getType ( ) ) <EOL> def test_lockinvalidid ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT:root>" <EOL> txid = <NUM_LIT> L <EOL> try : <EOL> self . zk . acquire_lock ( app , txid , key ) <EOL> self . fail ( ) <EOL> except zkappscale . zktransaction . ZKTransactionException as e : <EOL> print e <EOL> self . assertEqual ( zkappscale . zktransaction . ZKTransactionException . TYPE_INVALID , e . getType ( ) ) <EOL> def test_lock1000 ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT:root>" <EOL> for ii in range ( <NUM_LIT:1> , <NUM_LIT:1000> ) : <EOL> txid = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid > <NUM_LIT:0> ) <EOL> ret = self . zk . acquire_lock ( app , txid , key ) <EOL> self . assertTrue ( ret ) <EOL> ret = self . zk . release_lock ( app , txid ) <EOL> self . assertTrue ( ret ) <EOL> def test_releaseinvalidid ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT:root>" <EOL> txid = <NUM_LIT> L <EOL> try : <EOL> self . zk . release_lock ( app , txid ) <EOL> self . fail ( ) <EOL> except zkappscale . zktransaction . ZKTransactionException as e : <EOL> print e <EOL> self . assertEqual ( zkappscale . zktransaction . ZKTransactionException . TYPE_INVALID , e . getType ( ) ) <EOL> def test_releasewithoutlock ( self ) : <EOL> app = "<STR_LIT>" <EOL> txid = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid > <NUM_LIT:0> ) <EOL> ret = self . zk . release_lock ( app , txid ) <EOL> self . assertFalse ( ret ) <EOL> def test_releasedifferentkey ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT:root>" <EOL> key2 = "<STR_LIT>" <EOL> txid = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid > <NUM_LIT:0> ) <EOL> ret = self . zk . acquire_lock ( app , txid , key ) <EOL> self . assertTrue ( ret ) <EOL> try : <EOL> ret = self . zk . release_lock ( app , txid , key2 ) <EOL> self . fail ( ) <EOL> except zkappscale . zktransaction . ZKTransactionException as e : <EOL> print e <EOL> self . assertEqual ( zkappscale . zktransaction . ZKTransactionException . TYPE_DIFFERENT_ROOTKEY , e . getType ( ) ) <EOL> ret = self . zk . release_lock ( app , txid , key ) <EOL> self . assertTrue ( ret ) <EOL> def test_generateid ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT:root>" <EOL> ( id , block ) = self . zk . generateIDBlock ( app , key ) <EOL> print "<STR_LIT>" % ( id , block ) <EOL> self . assertTrue ( id >= <NUM_LIT:0> ) <EOL> self . assertTrue ( block > <NUM_LIT:0> ) <EOL> def test_generateid_pathkey ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT>" <EOL> ( id , block ) = self . zk . generateIDBlock ( app , key ) <EOL> print "<STR_LIT>" % ( id , block ) <EOL> self . assertTrue ( id >= <NUM_LIT:0> ) <EOL> self . assertTrue ( block > <NUM_LIT:0> ) <EOL> def test_generateid_specialcharacterkey ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT>" <EOL> ( id , block ) = self . zk . generateIDBlock ( app , key ) <EOL> print "<STR_LIT>" % ( id , block ) <EOL> self . assertTrue ( id >= <NUM_LIT:0> ) <EOL> self . assertTrue ( block > <NUM_LIT:0> ) <EOL> def test_generateid_twoblocks ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT:root>" <EOL> ( id , block ) = self . zk . generateIDBlock ( app , key ) <EOL> print "<STR_LIT>" % ( id , block ) <EOL> self . assertTrue ( id >= <NUM_LIT:0> ) <EOL> self . assertTrue ( block > <NUM_LIT:0> ) <EOL> ( nextid , block ) = self . zk . generateIDBlock ( app , key ) <EOL> print "<STR_LIT>" % ( nextid , block ) <EOL> self . assertTrue ( nextid >= <NUM_LIT:0> ) <EOL> self . assertTrue ( nextid > id ) <EOL> self . assertTrue ( block > <NUM_LIT:0> ) <EOL> def test_lockmultithread ( self ) : <EOL> self . valueA = <NUM_LIT:10> <EOL> tlist = [ ] <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> thread = threading . Thread ( target = self . __increaseA ) <EOL> thread . start ( ) <EOL> tlist . append ( thread ) <EOL> thread = threading . Thread ( target = self . __decreaseA ) <EOL> thread . start ( ) <EOL> tlist . append ( thread ) <EOL> for t in tlist : <EOL> t . join ( ) <EOL> self . assertEqual ( <NUM_LIT:10> , self . valueA ) <EOL> def test_lockmultithread_differentkeys ( self ) : <EOL> self . valueA = <NUM_LIT:10> <EOL> self . valueB = <NUM_LIT:10> <EOL> tlist = [ ] <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> thread = threading . Thread ( target = self . __increaseA ) <EOL> thread . start ( ) <EOL> tlist . append ( thread ) <EOL> thread = threading . Thread ( target = self . __decreaseA ) <EOL> thread . start ( ) <EOL> tlist . append ( thread ) <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> thread = threading . Thread ( target = self . __increaseB ) <EOL> thread . start ( ) <EOL> tlist . append ( thread ) <EOL> thread = threading . Thread ( target = self . __decreaseB ) <EOL> thread . start ( ) <EOL> tlist . append ( thread ) <EOL> for t in tlist : <EOL> t . join ( ) <EOL> self . assertEqual ( <NUM_LIT:10> , self . valueA ) <EOL> self . assertEqual ( <NUM_LIT:10> , self . valueB ) <EOL> def __increaseA ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT>" <EOL> txid = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid > <NUM_LIT:0> ) <EOL> while not self . zk . acquire_lock ( app , txid , key ) : <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> print "<STR_LIT>" % threading . currentThread ( ) <EOL> tmp = self . valueA <EOL> time . sleep ( <NUM_LIT:0.5> ) <EOL> self . valueA = tmp + <NUM_LIT:1> <EOL> ret = self . zk . release_lock ( app , txid ) <EOL> self . assertTrue ( ret ) <EOL> def __increaseB ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT>" <EOL> txid = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid > <NUM_LIT:0> ) <EOL> while not self . zk . acquire_lock ( app , txid , key ) : <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> print "<STR_LIT>" % threading . currentThread ( ) <EOL> tmp = self . valueB <EOL> self . valueB = tmp + <NUM_LIT:1> <EOL> ret = self . zk . release_lock ( app , txid ) <EOL> self . assertTrue ( ret ) <EOL> def __decreaseA ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT>" <EOL> txid = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid > <NUM_LIT:0> ) <EOL> while not self . zk . acquire_lock ( app , txid , key ) : <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> print "<STR_LIT>" % threading . currentThread ( ) <EOL> tmp = self . valueA <EOL> time . sleep ( <NUM_LIT:0.5> ) <EOL> self . valueA = tmp - <NUM_LIT:1> <EOL> ret = self . zk . release_lock ( app , txid ) <EOL> self . assertTrue ( ret ) <EOL> def __decreaseB ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT>" <EOL> txid = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid > <NUM_LIT:0> ) <EOL> while not self . zk . acquire_lock ( app , txid , key ) : <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> print "<STR_LIT>" % threading . currentThread ( ) <EOL> tmp = self . valueB <EOL> self . valueB = tmp - <NUM_LIT:1> <EOL> ret = self . zk . release_lock ( app , txid ) <EOL> self . assertTrue ( ret ) <EOL> def __lockonlyB ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT>" <EOL> txid = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid > <NUM_LIT:0> ) <EOL> while not self . zk . acquire_lock ( app , txid , key ) : <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> def test_gcsimple ( self ) : <EOL> zkappscale . zktransaction . TX_TIMEOUT = <NUM_LIT:1> <EOL> zkappscale . zktransaction . GC_INTERVAL = <NUM_LIT:1> <EOL> self . keylist = None <EOL> self . zk . startGC ( ) <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT>" <EOL> txid = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid > <NUM_LIT:0> ) <EOL> ret = self . zk . acquire_lock ( app , txid , key ) <EOL> self . assertTrue ( ret ) <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> self . assertTrue ( self . zk . is_blacklisted ( app , txid ) ) <EOL> try : <EOL> self . zk . release_lock ( app , txid , key ) <EOL> self . fail ( ) <EOL> except zkappscale . zktransaction . ZKTransactionException as e : <EOL> print e <EOL> self . assertEqual ( zkappscale . zktransaction . ZKTransactionException . TYPE_EXPIRED , e . getType ( ) ) <EOL> try : <EOL> self . zk . acquire_lock ( app , txid , key ) <EOL> self . fail ( ) <EOL> except zkappscale . zktransaction . ZKTransactionException as e : <EOL> print e <EOL> self . assertEqual ( zkappscale . zktransaction . ZKTransactionException . TYPE_EXPIRED , e . getType ( ) ) <EOL> zkappscale . zktransaction . TX_TIMEOUT = <NUM_LIT:30> <EOL> zkappscale . zktransaction . GC_INTERVAL = <NUM_LIT:30> <EOL> self . zk . startGC ( ) <EOL> def test_gcwithkeylist ( self ) : <EOL> zkappscale . zktransaction . TX_TIMEOUT = <NUM_LIT:1> <EOL> zkappscale . zktransaction . GC_INTERVAL = <NUM_LIT:1> <EOL> self . keylist = None <EOL> self . zk . startGC ( ) <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT>" <EOL> txid = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid > <NUM_LIT:0> ) <EOL> ret = self . zk . acquire_lock ( app , txid , key ) <EOL> self . assertTrue ( ret ) <EOL> vid = <NUM_LIT:100> L <EOL> self . zk . register_updated_key ( app , txid , vid , key + "<STR_LIT>" ) <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> self . assertTrue ( self . zk . is_blacklisted ( app , txid ) ) <EOL> try : <EOL> self . zk . release_lock ( app , txid , key ) <EOL> self . fail ( ) <EOL> except zkappscale . zktransaction . ZKTransactionException as e : <EOL> print e <EOL> self . assertEqual ( zkappscale . zktransaction . ZKTransactionException . TYPE_EXPIRED , e . getType ( ) ) <EOL> try : <EOL> self . zk . acquire_lock ( app , txid , key ) <EOL> self . fail ( ) <EOL> except zkappscale . zktransaction . ZKTransactionException as e : <EOL> print e <EOL> self . assertEqual ( zkappscale . zktransaction . ZKTransactionException . TYPE_EXPIRED , e . getType ( ) ) <EOL> zkappscale . zktransaction . TX_TIMEOUT = <NUM_LIT:30> <EOL> zkappscale . zktransaction . GC_INTERVAL = <NUM_LIT:30> <EOL> self . zk . startGC ( ) <EOL> def test_gcreleaselock ( self ) : <EOL> zkappscale . zktransaction . TX_TIMEOUT = <NUM_LIT:1> <EOL> zkappscale . zktransaction . GC_INTERVAL = <NUM_LIT:1> <EOL> self . keylist = None <EOL> self . zk . startGC ( ) <EOL> self . valueB = <NUM_LIT:1> <EOL> tlock = threading . Thread ( target = self . __lockonlyB ) <EOL> tlock . start ( ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> tincrease = threading . Thread ( target = self . __increaseB ) <EOL> tincrease . start ( ) <EOL> tlock . join ( ) <EOL> tincrease . join ( ) <EOL> self . assertEqual ( <NUM_LIT:2> , self . valueB ) <EOL> zkappscale . zktransaction . TX_TIMEOUT = <NUM_LIT:30> <EOL> zkappscale . zktransaction . GC_INTERVAL = <NUM_LIT:30> <EOL> self . zk . startGC ( ) <EOL> def __rollbackReceiver ( self , app_id , txid , rootkey , keylist ) : <EOL> print "<STR_LIT>" % ( app_id , txid , rootkey , keylist ) <EOL> self . app_id = app_id <EOL> self . txid = txid <EOL> self . rootkey = rootkey <EOL> self . keylist = keylist <EOL> def test_rollback ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT:root>" <EOL> txid = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid > <NUM_LIT:0> ) <EOL> ret = self . zk . acquire_lock ( app , txid , key ) <EOL> self . assertTrue ( ret ) <EOL> ret = self . zk . notify_failed_transaction ( app , txid ) <EOL> self . assertTrue ( ret ) <EOL> self . assertTrue ( self . zk . is_blacklisted ( app , txid ) ) <EOL> try : <EOL> ret = self . zk . release_lock ( app , txid ) <EOL> self . fail <EOL> except zkappscale . zktransaction . ZKTransactionException as e : <EOL> print e <EOL> self . assertEqual ( zkappscale . zktransaction . ZKTransactionException . TYPE_EXPIRED , e . getType ( ) ) <EOL> def test_updateafterrollback ( self ) : <EOL> app = "<STR_LIT>" <EOL> key = "<STR_LIT:root>" <EOL> txid = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid > <NUM_LIT:0> ) <EOL> ret = self . zk . acquire_lock ( app , txid , key ) <EOL> self . assertTrue ( ret ) <EOL> vid = <NUM_LIT:0> L <EOL> self . zk . register_updated_key ( app , txid , vid , key + "<STR_LIT>" ) <EOL> ret = self . zk . notify_failed_transaction ( app , txid ) <EOL> self . assertTrue ( ret ) <EOL> self . assertTrue ( self . zk . is_blacklisted ( app , txid ) ) <EOL> txid2 = self . zk . get_transaction_id ( app ) <EOL> self . assertTrue ( txid2 > <NUM_LIT:0> ) <EOL> ret = self . zk . acquire_lock ( app , txid2 , key ) <EOL> self . assertTrue ( ret ) <EOL> ret = self . zk . get_valid_transaction_id ( app , txid , key + "<STR_LIT>" ) <EOL> self . assertEqual ( vid , ret ) <EOL> vid = <NUM_LIT:100> L <EOL> self . zk . register_updated_key ( app , txid2 , vid , key + "<STR_LIT>" ) <EOL> ret = self . zk . get_valid_transaction_id ( app , txid , key + "<STR_LIT>" ) <EOL> self . assertEqual ( vid , ret ) <EOL> self . assertTrue ( self . zk . release_lock ( app , txid2 ) ) <EOL> def setUp ( self ) : <EOL> global zkconnection <EOL> self . zk = zkconnection <EOL> def tearDown ( self ) : <EOL> self . zk . dump_tree ( "<STR_LIT>" ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> global zkconnection <EOL> zkconnection = zkappscale . zktransaction . ZKTransaction ( ) <EOL> if len ( sys . argv ) > <NUM_LIT:1> and sys . argv [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> zkconnection . dump_tree ( "<STR_LIT>" ) <EOL> else : <EOL> zkconnection . deleteRecursive ( "<STR_LIT>" ) <EOL> test_support . run_unittest ( TestZKTransaction ) <EOL> zkconnection . close ( ) </s>
