<s> from __future__ import print_function , unicode_literals <EOL> import time <EOL> import unittest <EOL> import six <EOL> from voodoo . gen import load_dir <EOL> from voodoo . gen . registry import GLOBAL_REGISTRY <EOL> import weblab . configuration_doc as configuration_doc <EOL> import weblab . data . command as Command <EOL> import weblab . experiment . util as ExperimentUtil <EOL> import weblab . core . reservations as Reservation <EOL> import weblab . core . server as UserProcessingServer <EOL> from weblab . core . coordinator . clients . weblabdeusto import WebLabDeustoClient <EOL> from test . util . module_disposer import case_uses_module <EOL> class IntegrationNoConcurrencyTestCase ( object ) : <EOL> def setUp ( self ) : <EOL> GLOBAL_REGISTRY . clear ( ) <EOL> self . global_config = load_dir ( self . DEPLOYMENT_DIR ) <EOL> self . process_handlers = [ ] <EOL> for process in self . PROCESSES : <EOL> process_handler = self . global_config . load_process ( '<STR_LIT>' , process ) <EOL> self . process_handlers . append ( process_handler ) <EOL> self . core_server = GLOBAL_REGISTRY [ self . CORE_ADDRESS ] <EOL> self . experiment_dummy1 = GLOBAL_REGISTRY [ self . EXPERIMENT_DUMMY1 ] <EOL> self . experiment_dummy2 = GLOBAL_REGISTRY [ self . EXPERIMENT_DUMMY2 ] <EOL> self . core_config = self . core_server . config <EOL> self . client = WebLabDeustoClient ( '<STR_LIT>' % self . core_config [ configuration_doc . CORE_FACADE_PORT ] ) <EOL> def tearDown ( self ) : <EOL> GLOBAL_REGISTRY . clear ( ) <EOL> for process_handler in self . process_handlers : <EOL> process_handler . stop ( ) <EOL> def test_simple_single_uses ( self ) : <EOL> for _ in range ( <NUM_LIT:1> ) : <EOL> self . _single_use ( ) <EOL> self . _single_use ( ) <EOL> self . _single_use ( ) <EOL> def _single_use ( self , logout = True , plus_async_use = True ) : <EOL> """<STR_LIT>""" <EOL> self . _single_sync_use ( logout ) <EOL> if plus_async_use : <EOL> self . _single_async_use ( logout ) <EOL> def _single_sync_use ( self , logout = True ) : <EOL> session_id , reservation_id = self . _get_reserved ( ) <EOL> CONTENT = "<STR_LIT>" <EOL> response = self . client . send_file ( reservation_id , ExperimentUtil . serialize ( CONTENT ) , '<STR_LIT>' ) <EOL> self . assertEquals ( response . commandstring , '<STR_LIT>' ) <EOL> response = self . client . send_command ( reservation_id , Command . Command ( "<STR_LIT>" ) ) <EOL> self . assertEquals ( response . commandstring , '<STR_LIT>' ) <EOL> response = self . client . send_command ( reservation_id , Command . Command ( "<STR_LIT>" ) ) <EOL> self . assertEquals ( response . commandstring , "<STR_LIT>" ) <EOL> if logout : <EOL> self . client . logout ( session_id ) <EOL> def _get_reserved ( self ) : <EOL> session_id = self . client . login ( '<STR_LIT>' , '<STR_LIT:password>' ) <EOL> user_information = self . client . get_user_information ( session_id ) <EOL> self . assertEquals ( '<STR_LIT>' , user_information . login ) <EOL> self . assertEquals ( '<STR_LIT>' , user_information . full_name ) <EOL> self . assertEquals ( '<STR_LIT>' , user_information . email ) <EOL> experiments = self . client . list_experiments ( session_id ) <EOL> self . assertEquals ( <NUM_LIT:2> , len ( experiments ) ) <EOL> dummy1_experiments = [ exp . experiment for exp in experiments if exp . experiment . name == '<STR_LIT>' ] <EOL> self . assertEquals ( len ( dummy1_experiments ) , <NUM_LIT:1> ) <EOL> dummy1_experiment = dummy1_experiments [ <NUM_LIT:0> ] <EOL> status = self . client . reserve_experiment ( session_id , dummy1_experiment . to_experiment_id ( ) , "<STR_LIT:{}>" , "<STR_LIT:{}>" ) <EOL> reservation_id = status . reservation_id <EOL> short_time = <NUM_LIT:0.1> <EOL> times = <NUM_LIT> / short_time <EOL> while times > <NUM_LIT:0> : <EOL> new_status = self . client . get_reservation_status ( reservation_id ) <EOL> if not isinstance ( new_status , Reservation . WaitingConfirmationReservation ) and not isinstance ( new_status , Reservation . WaitingReservation ) : <EOL> break <EOL> times -= <NUM_LIT:1> <EOL> time . sleep ( short_time ) <EOL> reservation = self . client . get_reservation_status ( reservation_id ) <EOL> self . assertTrue ( <EOL> isinstance ( reservation , Reservation . ConfirmedReservation ) , <EOL> "<STR_LIT>" % reservation <EOL> ) <EOL> return session_id , reservation_id <EOL> def _single_async_use ( self , logout = True ) : <EOL> session_id , reservation_id = self . _get_reserved ( ) <EOL> CONTENT = "<STR_LIT>" <EOL> reqid = self . client . send_async_file ( reservation_id , ExperimentUtil . serialize ( CONTENT ) , '<STR_LIT>' ) <EOL> self . _wait_async_done ( reservation_id , ( reqid , ) ) <EOL> reqid = self . client . send_async_command ( reservation_id , Command . Command ( "<STR_LIT>" ) ) <EOL> respcmd = self . _get_async_response ( reservation_id , reqid ) <EOL> response = respcmd . get_command_string ( ) <EOL> self . assertEquals ( "<STR_LIT>" , response ) <EOL> reqid = self . client . send_async_command ( reservation_id , Command . Command ( "<STR_LIT>" ) ) <EOL> self . _wait_async_done ( reservation_id , ( reqid , ) ) <EOL> reqid = self . client . send_async_command ( reservation_id , Command . Command ( "<STR_LIT>" ) ) <EOL> self . _wait_async_done ( reservation_id , ( reqid , ) ) <EOL> if logout : <EOL> self . client . logout ( session_id ) <EOL> def _wait_async_done ( self , reservation_id , reqids ) : <EOL> """<STR_LIT>""" <EOL> reqsl = list ( reqids ) <EOL> max_count = <NUM_LIT:15> <EOL> while len ( reqsl ) > <NUM_LIT:0> : <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> max_count -= <NUM_LIT:1> <EOL> if max_count == <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> requests = self . client . check_async_command_status ( reservation_id , tuple ( reqsl ) ) <EOL> self . assertEquals ( len ( reqsl ) , len ( requests ) ) <EOL> for rid , req in six . iteritems ( requests ) : <EOL> status = req [ <NUM_LIT:0> ] <EOL> self . assertTrue ( status in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:error>" ) ) <EOL> if status != "<STR_LIT>" : <EOL> self . assertEquals ( "<STR_LIT>" , status , "<STR_LIT>" + req [ <NUM_LIT:1> ] ) <EOL> reqsl . remove ( rid ) <EOL> def _get_async_response ( self , reservation_id , reqid ) : <EOL> """<STR_LIT>""" <EOL> max_counter = <NUM_LIT:15> <EOL> while True : <EOL> max_counter -= <NUM_LIT:1> <EOL> if max_counter == <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> requests = self . client . check_async_command_status ( reservation_id , ( reqid , ) ) <EOL> self . assertEquals ( <NUM_LIT:1> , len ( requests ) ) <EOL> self . assertTrue ( reqid in requests ) <EOL> req = requests [ reqid ] <EOL> status = req [ <NUM_LIT:0> ] <EOL> self . assertTrue ( status in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:error>" ) ) <EOL> if status != "<STR_LIT>" : <EOL> self . assertEquals ( "<STR_LIT>" , status , "<STR_LIT>" + req [ <NUM_LIT:1> ] ) <EOL> return Command . Command ( req [ <NUM_LIT:1> ] ) <EOL> def test_single_uses_timeout ( self ) : <EOL> core_experiment_poll_time = <NUM_LIT> <EOL> core_time_between_checks = <NUM_LIT> <EOL> self . core_config . _set_value ( '<STR_LIT>' , core_experiment_poll_time ) <EOL> self . core_config . _set_value ( '<STR_LIT>' , core_time_between_checks ) <EOL> self . _single_use ( logout = False , plus_async_use = False ) <EOL> time . sleep ( core_experiment_poll_time + <NUM_LIT> + core_time_between_checks ) <EOL> self . _single_use ( logout = False , plus_async_use = False ) <EOL> def test_two_multiple_uses_of_different_devices ( self ) : <EOL> user1_session_id = self . client . login ( '<STR_LIT>' , '<STR_LIT:password>' ) <EOL> user1_experiments = self . client . list_experiments ( user1_session_id ) <EOL> self . assertEquals ( <NUM_LIT:2> , len ( user1_experiments ) ) <EOL> dummy1_experiments = [ exp . experiment for exp in user1_experiments if exp . experiment . name == '<STR_LIT>' ] <EOL> self . assertEquals ( len ( dummy1_experiments ) , <NUM_LIT:1> ) <EOL> status = self . client . reserve_experiment ( user1_session_id , dummy1_experiments [ <NUM_LIT:0> ] . to_experiment_id ( ) , "<STR_LIT:{}>" , "<STR_LIT:{}>" ) <EOL> user1_reservation_id = status . reservation_id <EOL> user2_session_id = self . client . login ( '<STR_LIT>' , '<STR_LIT:password>' ) <EOL> user2_experiments = self . client . list_experiments ( user2_session_id ) <EOL> self . assertEquals ( <NUM_LIT:2> , len ( user2_experiments ) ) <EOL> dummy2_experiments = [ exp . experiment for exp in user2_experiments if exp . experiment . name == '<STR_LIT>' ] <EOL> self . assertEquals ( len ( dummy2_experiments ) , <NUM_LIT:1> ) <EOL> status = self . client . reserve_experiment ( user2_session_id , dummy2_experiments [ <NUM_LIT:0> ] . to_experiment_id ( ) , "<STR_LIT:{}>" , "<STR_LIT:{}>" ) <EOL> user2_reservation_id = status . reservation_id <EOL> short_time = <NUM_LIT:0.1> <EOL> times = <NUM_LIT> / short_time <EOL> while times > <NUM_LIT:0> : <EOL> time . sleep ( short_time ) <EOL> new_status1 = self . client . get_reservation_status ( user1_reservation_id ) <EOL> new_status2 = self . client . get_reservation_status ( user2_reservation_id ) <EOL> if not isinstance ( new_status1 , Reservation . WaitingConfirmationReservation ) : <EOL> if not isinstance ( new_status2 , Reservation . WaitingConfirmationReservation ) : <EOL> break <EOL> times -= <NUM_LIT:1> <EOL> self . assertTrue ( isinstance ( self . client . get_reservation_status ( user1_reservation_id ) , Reservation . ConfirmedReservation ) ) <EOL> self . assertTrue ( isinstance ( self . client . get_reservation_status ( user2_reservation_id ) , Reservation . ConfirmedReservation ) ) <EOL> CONTENT1 = "<STR_LIT>" <EOL> response = self . client . send_file ( user1_reservation_id , ExperimentUtil . serialize ( CONTENT1 ) , '<STR_LIT>' ) <EOL> self . assertEquals ( '<STR_LIT>' , response . commandstring ) <EOL> respcmd = self . client . send_command ( user1_reservation_id , Command . Command ( "<STR_LIT>" ) ) <EOL> response = respcmd . get_command_string ( ) <EOL> self . assertEquals ( "<STR_LIT>" , response ) <EOL> CONTENT2 = "<STR_LIT>" <EOL> response = self . client . send_file ( user2_reservation_id , ExperimentUtil . serialize ( CONTENT2 ) , '<STR_LIT>' ) <EOL> self . assertEquals ( '<STR_LIT>' , response . commandstring ) <EOL> respcmd = self . client . send_command ( user2_reservation_id , Command . Command ( "<STR_LIT>" ) ) <EOL> self . assertEquals ( "<STR_LIT>" , respcmd . commandstring ) <EOL> self . client . logout ( user1_session_id ) <EOL> self . client . logout ( user2_session_id ) <EOL> @ case_uses_module ( UserProcessingServer ) <EOL> class IntegrationNoConcurrency_Direct_TestCase ( IntegrationNoConcurrencyTestCase , unittest . TestCase ) : <EOL> DEPLOYMENT_DIR = '<STR_LIT>' <EOL> PROCESSES = [ '<STR_LIT>' ] <EOL> CORE_ADDRESS = '<STR_LIT>' <EOL> EXPERIMENT_DUMMY1 = '<STR_LIT>' <EOL> EXPERIMENT_DUMMY2 = '<STR_LIT>' <EOL> @ case_uses_module ( UserProcessingServer ) <EOL> class IntegrationNoConcurrency_Http_TestCase ( IntegrationNoConcurrencyTestCase , unittest . TestCase ) : <EOL> DEPLOYMENT_DIR = '<STR_LIT>' <EOL> PROCESSES = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> CORE_ADDRESS = '<STR_LIT>' <EOL> EXPERIMENT_DUMMY1 = '<STR_LIT>' <EOL> EXPERIMENT_DUMMY2 = '<STR_LIT>' <EOL> def suite ( ) : <EOL> suites = ( unittest . makeSuite ( IntegrationNoConcurrency_Direct_TestCase ) , unittest . makeSuite ( IntegrationNoConcurrency_Http_TestCase ) , ) <EOL> return unittest . TestSuite ( suites ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
