<s> import sys <EOL> import itertools <EOL> import os . path <EOL> import pickle <EOL> import unittest <EOL> this_dir = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> sys . path . append ( os . path . dirname ( this_dir ) ) <EOL> from paxos import essential <EOL> PID = essential . ProposalID <EOL> class EssentialMessenger ( object ) : <EOL> resolution = None <EOL> def setUp ( self ) : <EOL> self . _msgs = list ( ) <EOL> def _append ( self , * args ) : <EOL> self . _msgs . append ( args ) <EOL> def send_prepare ( self , proposal_id ) : <EOL> self . _append ( '<STR_LIT>' , proposal_id ) <EOL> def send_promise ( self , to_uid , proposal_id , proposal_value , accepted_value ) : <EOL> self . _append ( '<STR_LIT>' , to_uid , proposal_id , proposal_value , accepted_value ) <EOL> def send_accept ( self , proposal_id , proposal_value ) : <EOL> self . _append ( '<STR_LIT>' , proposal_id , proposal_value ) <EOL> def send_accepted ( self , proposal_id , accepted_value ) : <EOL> self . _append ( '<STR_LIT>' , proposal_id , accepted_value ) <EOL> def on_resolution ( self , proposal_id , value ) : <EOL> self . resolution = ( proposal_id , value ) <EOL> @ property <EOL> def last_msg ( self ) : <EOL> return self . _msgs [ - <NUM_LIT:1> ] <EOL> def nmsgs ( self ) : <EOL> return len ( self . _msgs ) <EOL> def clear_msgs ( self ) : <EOL> self . _msgs = list ( ) <EOL> def am ( self , * args ) : <EOL> self . assertTrue ( len ( self . _msgs ) == <NUM_LIT:1> ) <EOL> self . assertEquals ( self . last_msg , args ) <EOL> self . _msgs = list ( ) <EOL> def amm ( self , msgs ) : <EOL> self . assertEquals ( len ( self . _msgs ) , len ( msgs ) ) <EOL> for a , e in itertools . izip ( self . _msgs , msgs ) : <EOL> self . assertEquals ( a , e ) <EOL> self . _msgs = list ( ) <EOL> def an ( self ) : <EOL> self . assertTrue ( len ( self . _msgs ) == <NUM_LIT:0> ) <EOL> def ae ( self , * args ) : <EOL> self . assertEquals ( * args ) <EOL> def at ( self , * args ) : <EOL> self . assertTrue ( * args ) <EOL> class EssentialProposerTests ( object ) : <EOL> proposer_factory = None <EOL> def setUp ( self ) : <EOL> super ( EssentialProposerTests , self ) . setUp ( ) <EOL> self . p = self . proposer_factory ( self , '<STR_LIT:A>' , <NUM_LIT:2> ) <EOL> def al ( self , value ) : <EOL> if hasattr ( self . p , '<STR_LIT>' ) : <EOL> self . assertEquals ( self . p . leader , value ) <EOL> def num_promises ( self ) : <EOL> if hasattr ( self . p , '<STR_LIT>' ) : <EOL> return len ( self . p . promises_rcvd ) <EOL> else : <EOL> return self . p . numPromises ( ) <EOL> def test_set_proposal_no_value ( self ) : <EOL> self . ae ( self . p . proposed_value , None ) <EOL> self . p . set_proposal ( '<STR_LIT:foo>' ) <EOL> self . ae ( self . p . proposed_value , '<STR_LIT:foo>' ) <EOL> self . an ( ) <EOL> def test_set_proposal_with_previous_value ( self ) : <EOL> self . p . set_proposal ( '<STR_LIT:foo>' ) <EOL> self . p . set_proposal ( '<STR_LIT:bar>' ) <EOL> self . ae ( self . p . proposed_value , '<STR_LIT:foo>' ) <EOL> self . an ( ) <EOL> def test_prepare ( self ) : <EOL> self . al ( False ) <EOL> self . ae ( self . p . proposer_uid , '<STR_LIT:A>' ) <EOL> self . ae ( self . p . quorum_size , <NUM_LIT:2> ) <EOL> self . p . prepare ( ) <EOL> self . am ( '<STR_LIT>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) ) <EOL> self . al ( False ) <EOL> def test_prepare_two ( self ) : <EOL> self . p . prepare ( ) <EOL> self . am ( '<STR_LIT>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) ) <EOL> self . p . prepare ( ) <EOL> self . am ( '<STR_LIT>' , PID ( <NUM_LIT:2> , '<STR_LIT:A>' ) ) <EOL> def test_prepare_with_promises_rcvd ( self ) : <EOL> self . p . prepare ( ) <EOL> self . am ( '<STR_LIT>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) ) <EOL> self . ae ( self . num_promises ( ) , <NUM_LIT:0> ) <EOL> self . p . recv_promise ( '<STR_LIT:B>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , None , None ) <EOL> self . ae ( self . num_promises ( ) , <NUM_LIT:1> ) <EOL> self . p . prepare ( ) <EOL> self . am ( '<STR_LIT>' , PID ( <NUM_LIT:2> , '<STR_LIT:A>' ) ) <EOL> self . ae ( self . num_promises ( ) , <NUM_LIT:0> ) <EOL> def test_recv_promise_ignore_other_nodes ( self ) : <EOL> self . p . prepare ( ) <EOL> self . am ( '<STR_LIT>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) ) <EOL> self . ae ( self . num_promises ( ) , <NUM_LIT:0> ) <EOL> self . p . recv_promise ( '<STR_LIT:B>' , PID ( <NUM_LIT:1> , '<STR_LIT:B>' ) , None , None ) <EOL> self . ae ( self . num_promises ( ) , <NUM_LIT:0> ) <EOL> def test_recv_promise_ignore_duplicate_response ( self ) : <EOL> self . p . prepare ( ) <EOL> self . am ( '<STR_LIT>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) ) <EOL> self . ae ( self . num_promises ( ) , <NUM_LIT:0> ) <EOL> self . p . recv_promise ( '<STR_LIT:B>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , None , None ) <EOL> self . ae ( self . num_promises ( ) , <NUM_LIT:1> ) <EOL> self . p . recv_promise ( '<STR_LIT:B>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , None , None ) <EOL> self . ae ( self . num_promises ( ) , <NUM_LIT:1> ) <EOL> def test_recv_promise_set_proposal_from_null ( self ) : <EOL> self . p . prepare ( ) <EOL> self . clear_msgs ( ) <EOL> self . p . prepare ( ) <EOL> self . am ( '<STR_LIT>' , PID ( <NUM_LIT:2> , '<STR_LIT:A>' ) ) <EOL> self . ae ( self . p . last_accepted_id , None ) <EOL> self . ae ( self . p . proposed_value , None ) <EOL> self . p . recv_promise ( '<STR_LIT:B>' , PID ( <NUM_LIT:2> , '<STR_LIT:A>' ) , PID ( <NUM_LIT:1> , '<STR_LIT:B>' ) , '<STR_LIT:foo>' ) <EOL> self . ae ( self . p . last_accepted_id , PID ( <NUM_LIT:1> , '<STR_LIT:B>' ) ) <EOL> self . ae ( self . p . proposed_value , '<STR_LIT:foo>' ) <EOL> def test_recv_promise_override_previous_proposal_value ( self ) : <EOL> self . p . prepare ( ) <EOL> self . p . prepare ( ) <EOL> self . p . prepare ( ) <EOL> self . p . recv_promise ( '<STR_LIT:B>' , PID ( <NUM_LIT:3> , '<STR_LIT:A>' ) , PID ( <NUM_LIT:1> , '<STR_LIT:B>' ) , '<STR_LIT:foo>' ) <EOL> self . clear_msgs ( ) <EOL> self . p . prepare ( ) <EOL> self . am ( '<STR_LIT>' , PID ( <NUM_LIT:4> , '<STR_LIT:A>' ) ) <EOL> self . p . recv_promise ( '<STR_LIT:B>' , PID ( <NUM_LIT:4> , '<STR_LIT:A>' ) , PID ( <NUM_LIT:3> , '<STR_LIT:B>' ) , '<STR_LIT:bar>' ) <EOL> self . ae ( self . p . last_accepted_id , PID ( <NUM_LIT:3> , '<STR_LIT:B>' ) ) <EOL> self . ae ( self . p . proposed_value , '<STR_LIT:bar>' ) <EOL> def test_recv_promise_ignore_previous_proposal_value ( self ) : <EOL> self . p . prepare ( ) <EOL> self . p . prepare ( ) <EOL> self . p . prepare ( ) <EOL> self . p . recv_promise ( '<STR_LIT:B>' , PID ( <NUM_LIT:3> , '<STR_LIT:A>' ) , PID ( <NUM_LIT:1> , '<STR_LIT:B>' ) , '<STR_LIT:foo>' ) <EOL> self . clear_msgs ( ) <EOL> self . p . prepare ( ) <EOL> self . am ( '<STR_LIT>' , PID ( <NUM_LIT:4> , '<STR_LIT:A>' ) ) <EOL> self . p . recv_promise ( '<STR_LIT:B>' , PID ( <NUM_LIT:4> , '<STR_LIT:A>' ) , PID ( <NUM_LIT:3> , '<STR_LIT:B>' ) , '<STR_LIT:bar>' ) <EOL> self . ae ( self . p . last_accepted_id , PID ( <NUM_LIT:3> , '<STR_LIT:B>' ) ) <EOL> self . ae ( self . p . proposed_value , '<STR_LIT:bar>' ) <EOL> self . p . recv_promise ( '<STR_LIT:C>' , PID ( <NUM_LIT:4> , '<STR_LIT:A>' ) , PID ( <NUM_LIT:2> , '<STR_LIT:B>' ) , '<STR_LIT>' ) <EOL> self . ae ( self . p . last_accepted_id , PID ( <NUM_LIT:3> , '<STR_LIT:B>' ) ) <EOL> self . ae ( self . p . proposed_value , '<STR_LIT:bar>' ) <EOL> class EssentialAcceptorTests ( object ) : <EOL> acceptor_factory = None <EOL> def setUp ( self ) : <EOL> super ( EssentialAcceptorTests , self ) . setUp ( ) <EOL> self . a = self . acceptor_factory ( self , '<STR_LIT:A>' , <NUM_LIT:2> ) <EOL> def test_recv_prepare_initial ( self ) : <EOL> self . ae ( self . a . promised_id , None ) <EOL> self . ae ( self . a . accepted_value , None ) <EOL> self . ae ( self . a . accepted_id , None ) <EOL> self . a . recv_prepare ( '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) ) <EOL> self . am ( '<STR_LIT>' , '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , None , None ) <EOL> def test_recv_prepare_duplicate ( self ) : <EOL> self . a . recv_prepare ( '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) ) <EOL> self . am ( '<STR_LIT>' , '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , None , None ) <EOL> self . a . recv_prepare ( '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) ) <EOL> self . am ( '<STR_LIT>' , '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , None , None ) <EOL> def test_recv_prepare_override ( self ) : <EOL> self . a . recv_prepare ( '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) ) <EOL> self . am ( '<STR_LIT>' , '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , None , None ) <EOL> self . a . recv_accept_request ( '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> self . clear_msgs ( ) <EOL> self . a . recv_prepare ( '<STR_LIT:B>' , PID ( <NUM_LIT:2> , '<STR_LIT:B>' ) ) <EOL> self . am ( '<STR_LIT>' , '<STR_LIT:B>' , PID ( <NUM_LIT:2> , '<STR_LIT:B>' ) , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> def test_recv_accept_request_initial ( self ) : <EOL> self . a . recv_accept_request ( '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> self . am ( '<STR_LIT>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> def test_recv_accept_request_promised ( self ) : <EOL> self . a . recv_prepare ( '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) ) <EOL> self . am ( '<STR_LIT>' , '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , None , None ) <EOL> self . a . recv_accept_request ( '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> self . am ( '<STR_LIT>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> def test_recv_accept_request_greater_than_promised ( self ) : <EOL> self . a . recv_prepare ( '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) ) <EOL> self . am ( '<STR_LIT>' , '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , None , None ) <EOL> self . a . recv_accept_request ( '<STR_LIT:A>' , PID ( <NUM_LIT:5> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> self . am ( '<STR_LIT>' , PID ( <NUM_LIT:5> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> def test_recv_accept_request_less_than_promised ( self ) : <EOL> self . a . recv_prepare ( '<STR_LIT:A>' , PID ( <NUM_LIT:5> , '<STR_LIT:A>' ) ) <EOL> self . am ( '<STR_LIT>' , '<STR_LIT:A>' , PID ( <NUM_LIT:5> , '<STR_LIT:A>' ) , None , None ) <EOL> self . a . recv_accept_request ( '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> self . ae ( self . a . accepted_value , None ) <EOL> self . ae ( self . a . accepted_id , None ) <EOL> self . ae ( self . a . promised_id , PID ( <NUM_LIT:5> , '<STR_LIT:A>' ) ) <EOL> class EssentialLearnerTests ( object ) : <EOL> learner_factory = None <EOL> def setUp ( self ) : <EOL> super ( EssentialLearnerTests , self ) . setUp ( ) <EOL> self . l = self . learner_factory ( self , '<STR_LIT:A>' , <NUM_LIT:2> ) <EOL> def test_basic_resolution ( self ) : <EOL> self . ae ( self . l . quorum_size , <NUM_LIT:2> ) <EOL> self . ae ( self . l . final_value , None ) <EOL> self . ae ( self . l . final_proposal_id , None ) <EOL> self . l . recv_accepted ( '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> self . ae ( self . l . final_value , None ) <EOL> self . l . recv_accepted ( '<STR_LIT:B>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> self . ae ( self . l . final_value , '<STR_LIT:foo>' ) <EOL> self . ae ( self . l . final_proposal_id , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) ) <EOL> def test_ignore_after_resolution ( self ) : <EOL> self . l . recv_accepted ( '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> self . ae ( self . l . final_value , None ) <EOL> self . at ( not self . l . complete ) <EOL> self . l . recv_accepted ( '<STR_LIT:B>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> self . ae ( self . l . final_value , '<STR_LIT:foo>' ) <EOL> self . ae ( self . l . final_proposal_id , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) ) <EOL> self . l . recv_accepted ( '<STR_LIT:A>' , PID ( <NUM_LIT:5> , '<STR_LIT:A>' ) , '<STR_LIT:bar>' ) <EOL> self . l . recv_accepted ( '<STR_LIT:B>' , PID ( <NUM_LIT:5> , '<STR_LIT:A>' ) , '<STR_LIT:bar>' ) <EOL> self . ae ( self . l . final_value , '<STR_LIT:foo>' ) <EOL> self . ae ( self . l . final_proposal_id , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) ) <EOL> def test_ignore_duplicate_messages ( self ) : <EOL> self . l . recv_accepted ( '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> self . ae ( self . l . final_value , None ) <EOL> self . l . recv_accepted ( '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> self . ae ( self . l . final_value , None ) <EOL> self . l . recv_accepted ( '<STR_LIT:B>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> self . ae ( self . l . final_value , '<STR_LIT:foo>' ) <EOL> self . ae ( self . l . final_proposal_id , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) ) <EOL> def test_ignore_old_messages ( self ) : <EOL> self . l . recv_accepted ( '<STR_LIT:A>' , PID ( <NUM_LIT:5> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> self . ae ( self . l . final_value , None ) <EOL> self . l . recv_accepted ( '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , '<STR_LIT:bar>' ) <EOL> self . ae ( self . l . final_value , None ) <EOL> self . l . recv_accepted ( '<STR_LIT:B>' , PID ( <NUM_LIT:5> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> self . ae ( self . l . final_value , '<STR_LIT:foo>' ) <EOL> self . ae ( self . l . final_proposal_id , PID ( <NUM_LIT:5> , '<STR_LIT:A>' ) ) <EOL> def test_overwrite_old_messages ( self ) : <EOL> self . l . recv_accepted ( '<STR_LIT:A>' , PID ( <NUM_LIT:1> , '<STR_LIT:A>' ) , '<STR_LIT:bar>' ) <EOL> self . ae ( self . l . final_value , None ) <EOL> self . l . recv_accepted ( '<STR_LIT:B>' , PID ( <NUM_LIT:5> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> self . ae ( self . l . final_value , None ) <EOL> self . l . recv_accepted ( '<STR_LIT:A>' , PID ( <NUM_LIT:5> , '<STR_LIT:A>' ) , '<STR_LIT:foo>' ) <EOL> self . ae ( self . l . final_value , '<STR_LIT:foo>' ) <EOL> self . ae ( self . l . final_proposal_id , PID ( <NUM_LIT:5> , '<STR_LIT:A>' ) ) <EOL> class TPax ( object ) : <EOL> def __init__ ( self , messenger , uid , quorum_size ) : <EOL> self . messenger = messenger <EOL> self . proposer_uid = uid <EOL> self . quorum_size = quorum_size <EOL> class TProposer ( essential . Proposer , TPax ) : <EOL> pass <EOL> class TAcceptor ( essential . Acceptor , TPax ) : <EOL> pass <EOL> class TLearner ( essential . Learner , TPax ) : <EOL> pass <EOL> class EssentialProposerTester ( EssentialProposerTests , EssentialMessenger , unittest . TestCase ) : <EOL> proposer_factory = TProposer <EOL> class EssentialAcceptorTester ( EssentialAcceptorTests , EssentialMessenger , unittest . TestCase ) : <EOL> acceptor_factory = TAcceptor <EOL> class EssentialLearnerTester ( EssentialLearnerTests , EssentialMessenger , unittest . TestCase ) : <EOL> learner_factory = TLearner <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
