<s> from __future__ import print_function , unicode_literals <EOL> import sys <EOL> import time <EOL> import unittest <EOL> from voodoo . gen import load_dir <EOL> from voodoo . gen . registry import GLOBAL_REGISTRY <EOL> from weblab . data . command import Command <EOL> from weblab . data . experiments import ExperimentId , RunningReservationResult <EOL> from weblab . core . coordinator . clients . weblabdeusto import WebLabDeustoClient <EOL> from weblab . core . reservations import Reservation <EOL> DEBUG = False <EOL> def debug ( msg ) : <EOL> if DEBUG : <EOL> print ( ) <EOL> print ( "<STR_LIT>" , msg ) <EOL> print ( ) <EOL> sys . stdout . flush ( ) <EOL> class AbstractFederatedWebLabDeustoTestCase ( object ) : <EOL> def setUp ( self ) : <EOL> GLOBAL_REGISTRY . clear ( ) <EOL> CONSUMER_CONFIG_PATH = self . FEDERATED_DEPLOYMENTS + '<STR_LIT>' <EOL> PROVIDER1_CONFIG_PATH = self . FEDERATED_DEPLOYMENTS + '<STR_LIT>' <EOL> PROVIDER2_CONFIG_PATH = self . FEDERATED_DEPLOYMENTS + '<STR_LIT>' <EOL> self . consumer_handler = load_dir ( CONSUMER_CONFIG_PATH ) . load_process ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . provider1_handler = load_dir ( PROVIDER1_CONFIG_PATH ) . load_process ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . provider2_handler = load_dir ( PROVIDER2_CONFIG_PATH ) . load_process ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . consumer_client = WebLabDeustoClient ( "<STR_LIT>" % <NUM_LIT> ) <EOL> self . provider1_client = WebLabDeustoClient ( "<STR_LIT>" % <NUM_LIT> ) <EOL> self . provider2_client = WebLabDeustoClient ( "<STR_LIT>" % <NUM_LIT> ) <EOL> self . dummy1 = ExperimentId ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . dummy2 = ExperimentId ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . dummy3 = ExperimentId ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . dummy4 = ExperimentId ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def tearDown ( self ) : <EOL> self . consumer_handler . stop ( ) <EOL> self . provider1_handler . stop ( ) <EOL> self . provider2_handler . stop ( ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> def test_federated_experiment ( self ) : <EOL> debug ( "<STR_LIT>" ) <EOL> session_id = self . consumer_client . login ( '<STR_LIT>' , '<STR_LIT:password>' ) <EOL> reservation_id = self . _test_reservation ( session_id , self . dummy2 , '<STR_LIT>' , True , True ) <EOL> self . _wait_multiple_reservations ( <NUM_LIT:20> , session_id , [ reservation_id ] , [ <NUM_LIT:0> ] ) <EOL> reservation_result = self . consumer_client . get_experiment_use_by_id ( session_id , reservation_id ) <EOL> self . assertTrue ( reservation_result . is_finished ( ) ) <EOL> self . _find_command ( reservation_result , '<STR_LIT>' ) <EOL> reservation_id = self . _test_reservation ( session_id , self . dummy3 , '<STR_LIT>' , True , True ) <EOL> self . _wait_multiple_reservations ( <NUM_LIT:20> , session_id , [ reservation_id ] , [ <NUM_LIT:0> ] ) <EOL> reservation_result = self . consumer_client . get_experiment_use_by_id ( session_id , reservation_id ) <EOL> self . assertTrue ( reservation_result . is_finished ( ) ) <EOL> self . _find_command ( reservation_result , '<STR_LIT>' ) <EOL> reservation_id = self . _test_reservation ( session_id , self . dummy4 , '<STR_LIT>' , True , True ) <EOL> self . _wait_multiple_reservations ( <NUM_LIT:20> , session_id , [ reservation_id ] , [ <NUM_LIT:0> ] ) <EOL> reservation_result = self . consumer_client . get_experiment_use_by_id ( session_id , reservation_id ) <EOL> self . assertTrue ( reservation_result . is_finished ( ) ) <EOL> self . _find_command ( reservation_result , '<STR_LIT>' ) <EOL> reservation_id1 = self . _test_reservation ( session_id , self . dummy1 , '<STR_LIT>' , True , False , user_agent = '<STR_LIT>' ) <EOL> reservation_id2 = self . _test_reservation ( session_id , self . dummy1 , '<STR_LIT>' , True , False , user_agent = '<STR_LIT>' ) <EOL> reservation_id3 = self . _test_reservation ( session_id , self . dummy1 , '<STR_LIT>' , True , False , user_agent = '<STR_LIT>' ) <EOL> reservation_ids = ( reservation_id1 , reservation_id2 , reservation_id3 ) <EOL> reservation_results = self . consumer_client . get_experiment_uses_by_id ( session_id , reservation_ids ) <EOL> self . assertEquals ( RunningReservationResult ( ) , reservation_results [ <NUM_LIT:0> ] ) <EOL> self . assertEquals ( RunningReservationResult ( ) , reservation_results [ <NUM_LIT:1> ] ) <EOL> self . assertEquals ( RunningReservationResult ( ) , reservation_results [ <NUM_LIT:2> ] ) <EOL> self . consumer_client . finished_experiment ( reservation_id2 ) <EOL> self . _wait_multiple_reservations ( <NUM_LIT:20> , session_id , reservation_ids , [ <NUM_LIT:1> ] ) <EOL> reservation_results = self . consumer_client . get_experiment_uses_by_id ( session_id , reservation_ids ) <EOL> self . assertEquals ( RunningReservationResult ( ) , reservation_results [ <NUM_LIT:0> ] ) <EOL> self . assertEquals ( RunningReservationResult ( ) , reservation_results [ <NUM_LIT:2> ] ) <EOL> self . assertTrue ( reservation_results [ <NUM_LIT:1> ] . is_finished ( ) ) <EOL> self . assertEquals ( '<STR_LIT>' , reservation_results [ <NUM_LIT:1> ] . experiment_use . request_info [ '<STR_LIT>' ] ) <EOL> self . assertEquals ( <NUM_LIT:4> , len ( reservation_results [ <NUM_LIT:1> ] . experiment_use . commands ) ) <EOL> self . assertEquals ( '<STR_LIT>' , reservation_results [ <NUM_LIT:1> ] . experiment_use . commands [ <NUM_LIT:2> ] . response . commandstring ) <EOL> reservation_id2b = self . _test_reservation ( session_id , self . dummy1 , '<STR_LIT>' , True , False ) <EOL> self . consumer_client . finished_experiment ( reservation_id1 ) <EOL> self . _test_reservation ( session_id , self . dummy1 , '<STR_LIT>' , True , False ) <EOL> reservation_status = self . consumer_client . get_reservation_status ( reservation_id3 ) <EOL> provider2_reservation_id = reservation_status . remote_reservation_id <EOL> self . consumer_client . finished_experiment ( reservation_id3 ) <EOL> self . _test_reservation ( session_id , self . dummy1 , '<STR_LIT>' , True , False ) <EOL> self . _wait_multiple_reservations ( <NUM_LIT> , session_id , reservation_ids , [ <NUM_LIT:0> , <NUM_LIT:2> ] ) <EOL> reservation_results = self . consumer_client . get_experiment_uses_by_id ( session_id , reservation_ids ) <EOL> self . assertTrue ( reservation_results [ <NUM_LIT:0> ] . is_finished ( ) ) <EOL> self . assertEquals ( '<STR_LIT>' , reservation_results [ <NUM_LIT:0> ] . experiment_use . request_info [ '<STR_LIT>' ] ) <EOL> self . assertEquals ( '<STR_LIT>' , reservation_results [ <NUM_LIT:0> ] . experiment_use . commands [ <NUM_LIT:2> ] . response . commandstring ) <EOL> self . assertTrue ( reservation_results [ <NUM_LIT:2> ] . is_finished ( ) ) <EOL> self . assertEquals ( '<STR_LIT>' , reservation_results [ <NUM_LIT:2> ] . experiment_use . request_info [ '<STR_LIT>' ] ) <EOL> self . assertEquals ( '<STR_LIT>' , reservation_results [ <NUM_LIT:2> ] . experiment_use . commands [ <NUM_LIT:2> ] . response . commandstring ) <EOL> provider2_session_id = self . provider2_client . login ( '<STR_LIT>' , '<STR_LIT:password>' ) <EOL> provider2_result = self . provider2_client . get_experiment_use_by_id ( provider2_session_id , provider2_reservation_id ) <EOL> self . assertTrue ( provider2_result . is_finished ( ) ) <EOL> self . assertEquals ( '<STR_LIT>' , provider2_result . experiment_use . request_info [ '<STR_LIT>' ] ) <EOL> reservation_4 = self . _test_reservation ( session_id , self . dummy1 , '<STR_LIT>' , False , False ) <EOL> reservation_status = self . consumer_client . get_reservation_status ( reservation_4 ) <EOL> self . assertEquals ( Reservation . WAITING , reservation_status . status ) <EOL> self . assertEquals ( <NUM_LIT:0> , reservation_status . position ) <EOL> reservation_5 = self . _test_reservation ( session_id , self . dummy1 , '<STR_LIT>' , False , False ) <EOL> reservation_status = self . consumer_client . get_reservation_status ( reservation_5 ) <EOL> self . assertEquals ( Reservation . WAITING , reservation_status . status ) <EOL> self . assertEquals ( <NUM_LIT:1> , reservation_status . position ) <EOL> self . consumer_client . finished_experiment ( reservation_id2b ) <EOL> self . _wait_reservation ( reservation_4 , '<STR_LIT>' , True ) <EOL> self . consumer_client . finished_experiment ( reservation_4 ) <EOL> self . _wait_reservation ( reservation_5 , '<STR_LIT>' , True ) <EOL> for _ in range ( <NUM_LIT:50> ) : <EOL> time . sleep ( <NUM_LIT:0.5> ) <EOL> results = self . consumer_client . get_experiment_uses_by_id ( session_id , ( reservation_id2b , reservation_4 ) ) <EOL> if results [ <NUM_LIT:0> ] . is_finished ( ) and results [ <NUM_LIT:1> ] . is_finished ( ) : <EOL> break <EOL> final_reservation_results = self . consumer_client . get_experiment_uses_by_id ( session_id , ( reservation_id2b , reservation_4 ) ) <EOL> self . assertTrue ( final_reservation_results [ <NUM_LIT:0> ] . is_finished ( ) ) <EOL> self . assertTrue ( final_reservation_results [ <NUM_LIT:1> ] . is_finished ( ) ) <EOL> self . assertEquals ( '<STR_LIT>' , final_reservation_results [ <NUM_LIT:0> ] . experiment_use . commands [ <NUM_LIT:2> ] . response . commandstring ) <EOL> self . assertEquals ( '<STR_LIT>' , final_reservation_results [ <NUM_LIT:1> ] . experiment_use . commands [ <NUM_LIT:2> ] . response . commandstring ) <EOL> debug ( "<STR_LIT>" ) <EOL> def _wait_multiple_reservations ( self , times , session_id , reservation_ids , reservations_to_wait ) : <EOL> for _ in range ( times ) : <EOL> time . sleep ( <NUM_LIT:0.5> ) <EOL> results = self . consumer_client . get_experiment_uses_by_id ( session_id , reservation_ids ) <EOL> all_finished = True <EOL> for reservation_to_wait in reservations_to_wait : <EOL> all_finished = all_finished and results [ reservation_to_wait ] . is_finished ( ) <EOL> if all_finished : <EOL> break <EOL> def _find_command ( self , reservation_result , expected_response ) : <EOL> found = False <EOL> commands = reservation_result . experiment_use . commands <EOL> for command in commands : <EOL> if command . command . commandstring == '<STR_LIT>' : <EOL> found = True <EOL> response = command . response . commandstring <EOL> self . assertEquals ( expected_response , response , "<STR_LIT>" % ( expected_response , commands , response ) ) <EOL> self . assertTrue ( found , "<STR_LIT>" ) <EOL> def _test_reservation ( self , session_id , experiment_id , expected_server_info , wait , finish , user_agent = None ) : <EOL> debug ( "<STR_LIT>" % ( session_id , experiment_id , wait , finish ) ) <EOL> reservation_status = self . consumer_client . reserve_experiment ( session_id , experiment_id , "<STR_LIT:{}>" , "<STR_LIT:{}>" , user_agent = user_agent ) <EOL> reservation_id = reservation_status . reservation_id <EOL> if not wait : <EOL> if finish : <EOL> debug ( "<STR_LIT>" % reservation_id ) <EOL> self . consumer_client . finished_experiment ( reservation_id ) <EOL> debug ( "<STR_LIT>" % reservation_id ) <EOL> return reservation_id <EOL> reservation_id = self . _wait_reservation ( reservation_id , expected_server_info , finish ) <EOL> debug ( "<STR_LIT>" % reservation_id ) <EOL> return reservation_id <EOL> def _wait_reservation ( self , reservation_id , expected_server_info , finish ) : <EOL> max_timeout = <NUM_LIT:10> <EOL> initial_time = time . time ( ) <EOL> reservation_status = self . consumer_client . get_reservation_status ( reservation_id ) <EOL> while reservation_status . status in ( Reservation . WAITING , Reservation . WAITING_CONFIRMATION ) : <EOL> if time . time ( ) - initial_time > max_timeout : <EOL> self . fail ( "<STR_LIT>" % expected_server_info ) <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> reservation_status = self . consumer_client . get_reservation_status ( reservation_id ) <EOL> self . assertEquals ( Reservation . CONFIRMED , reservation_status . status ) <EOL> experiment_reservation_id = reservation_status . remote_reservation_id <EOL> if experiment_reservation_id . id == '<STR_LIT>' : <EOL> experiment_reservation_id = reservation_id <EOL> client = WebLabDeustoClient ( reservation_status . url ) <EOL> response = client . send_command ( experiment_reservation_id , Command ( "<STR_LIT>" ) ) <EOL> self . assertEquals ( expected_server_info , response . get_command_string ( ) ) <EOL> if finish : <EOL> self . consumer_client . finished_experiment ( reservation_id ) <EOL> return reservation_id <EOL> class SqlFederatedWebLabDeustoTestCase ( AbstractFederatedWebLabDeustoTestCase , unittest . TestCase ) : <EOL> FEDERATED_DEPLOYMENTS = '<STR_LIT>' <EOL> IS_SQL = True <EOL> try : <EOL> import redis <EOL> assert redis is not None <EOL> except ImportError : <EOL> REDIS_AVAILABLE = False <EOL> else : <EOL> REDIS_AVAILABLE = True <EOL> if REDIS_AVAILABLE : <EOL> class RedisFederatedWebLabDeustoTestCase ( AbstractFederatedWebLabDeustoTestCase , unittest . TestCase ) : <EOL> FEDERATED_DEPLOYMENTS = '<STR_LIT>' <EOL> IS_SQL = False <EOL> def suite ( ) : <EOL> suites = [ unittest . makeSuite ( SqlFederatedWebLabDeustoTestCase ) ] <EOL> if REDIS_AVAILABLE : <EOL> suites . append ( unittest . makeSuite ( RedisFederatedWebLabDeustoTestCase ) ) <EOL> else : <EOL> print ( "<STR_LIT>" , file = sys . stderr ) <EOL> return unittest . TestSuite ( suites ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
