<s> from __future__ import absolute_import <EOL> from __future__ import division <EOL> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> from builtins import str <EOL> import filecmp , os , sys , traceback , logging , requests , uuid <EOL> import time , random <EOL> from threading import Lock <EOL> import six <EOL> if six . PY2 : <EOL> import thread <EOL> from Queue import Queue <EOL> else : <EOL> import _thread as thread <EOL> from queue import Queue <EOL> import synapseclient <EOL> import synapseclient . utils as utils <EOL> import synapseclient . cache as cache <EOL> from synapseclient . exceptions import * <EOL> from synapseclient . utils import MB , GB <EOL> from synapseclient import Activity , Entity , Project , Folder , File <EOL> import integration <EOL> from integration import schedule_for_cleanup <EOL> def setup ( module ) : <EOL> print ( '<STR_LIT:\n>' ) <EOL> print ( '<STR_LIT>' * <NUM_LIT> ) <EOL> print ( os . path . basename ( __file__ ) ) <EOL> print ( '<STR_LIT>' * <NUM_LIT> ) <EOL> module . syn = integration . syn <EOL> module . project = integration . project <EOL> module . syn . test_parent = module . syn . store ( Project ( name = str ( uuid . uuid4 ( ) ) ) ) <EOL> schedule_for_cleanup ( module . syn . test_parent ) <EOL> module . syn . test_keepRunning = True <EOL> module . syn . test_errors = Queue ( ) <EOL> module . syn . test_runCountMutex = Lock ( ) <EOL> module . syn . test_threadsRunning = <NUM_LIT:0> <EOL> def teardown ( module ) : <EOL> del module . syn . test_parent <EOL> del module . syn . test_keepRunning <EOL> del module . syn . test_errors <EOL> del module . syn . test_runCountMutex <EOL> del module . syn . test_threadsRunning <EOL> def test_threaded_access ( ) : <EOL> """<STR_LIT>""" <EOL> requests_log = logging . getLogger ( "<STR_LIT>" ) <EOL> requests_originalLevel = requests_log . getEffectiveLevel ( ) <EOL> requests_log . setLevel ( logging . WARNING ) <EOL> print ( "<STR_LIT>" ) <EOL> store_thread = wrap_function_as_child_thread ( thread_keep_storing_one_File ) <EOL> get_thread = wrap_function_as_child_thread ( thread_get_files_from_Project ) <EOL> update_thread = wrap_function_as_child_thread ( thread_get_and_update_file_from_Project ) <EOL> thread . start_new_thread ( store_thread , ( ) ) <EOL> thread . start_new_thread ( store_thread , ( ) ) <EOL> thread . start_new_thread ( store_thread , ( ) ) <EOL> thread . start_new_thread ( store_thread , ( ) ) <EOL> thread . start_new_thread ( get_thread , ( ) ) <EOL> thread . start_new_thread ( get_thread , ( ) ) <EOL> thread . start_new_thread ( get_thread , ( ) ) <EOL> thread . start_new_thread ( update_thread , ( ) ) <EOL> thread . start_new_thread ( update_thread , ( ) ) <EOL> thread . start_new_thread ( update_thread , ( ) ) <EOL> time . sleep ( <NUM_LIT:20> ) <EOL> print ( "<STR_LIT>" ) <EOL> syn . test_keepRunning = False <EOL> while syn . test_threadsRunning > <NUM_LIT:0> : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> requests_log . setLevel ( requests_originalLevel ) <EOL> collect_errors_and_fail ( ) <EOL> def wrap_function_as_child_thread ( function ) : <EOL> """<STR_LIT>""" <EOL> def child_thread ( ) : <EOL> syn . test_runCountMutex . acquire ( ) <EOL> syn . test_threadsRunning += <NUM_LIT:1> <EOL> syn . test_runCountMutex . release ( ) <EOL> try : <EOL> function ( ) <EOL> except Exception : <EOL> syn . test_errors . put ( traceback . format_exc ( ) ) <EOL> syn . test_runCountMutex . acquire ( ) <EOL> syn . test_threadsRunning -= <NUM_LIT:1> <EOL> syn . test_runCountMutex . release ( ) <EOL> return child_thread <EOL> def collect_errors_and_fail ( ) : <EOL> """<STR_LIT>""" <EOL> failures = [ ] <EOL> for i in range ( syn . test_errors . qsize ( ) ) : <EOL> failures . append ( syn . test_errors . get ( ) ) <EOL> if len ( failures ) > <NUM_LIT:0> : <EOL> raise SynapseError ( '<STR_LIT:\n>' + '<STR_LIT:\n>' . join ( failures ) ) <EOL> def thread_keep_storing_one_File ( ) : <EOL> """<STR_LIT>""" <EOL> path = utils . make_bogus_data_file ( ) <EOL> schedule_for_cleanup ( path ) <EOL> myPrecious = File ( path , parent = syn . test_parent , description = '<STR_LIT>' , mwa = "<STR_LIT>" ) <EOL> while syn . test_keepRunning : <EOL> stored = store_catch_412_HTTPError ( myPrecious ) <EOL> if stored is not None : <EOL> myPrecious = stored <EOL> else : <EOL> myPrecious = syn . get ( myPrecious ) <EOL> sleep_for_a_bit ( ) <EOL> def thread_get_files_from_Project ( ) : <EOL> """<STR_LIT>""" <EOL> while syn . test_keepRunning : <EOL> for id in get_all_ids_from_Project ( ) : <EOL> pass <EOL> sleep_for_a_bit ( ) <EOL> def thread_get_and_update_file_from_Project ( ) : <EOL> """<STR_LIT>""" <EOL> while syn . test_keepRunning : <EOL> id = get_all_ids_from_Project ( ) <EOL> if len ( id ) <= <NUM_LIT:0> : <EOL> continue <EOL> id = id [ random . randrange ( len ( id ) ) ] <EOL> entity = syn . get ( id ) <EOL> path = utils . make_bogus_data_file ( ) <EOL> schedule_for_cleanup ( path ) <EOL> entity . path = path <EOL> entity = store_catch_412_HTTPError ( entity ) <EOL> if entity is not None : <EOL> assert os . stat ( entity . path ) == os . stat ( path ) <EOL> sleep_for_a_bit ( ) <EOL> def sleep_for_a_bit ( ) : <EOL> """<STR_LIT>""" <EOL> time . sleep ( random . randint ( <NUM_LIT:1> , <NUM_LIT:5> ) ) <EOL> def get_all_ids_from_Project ( ) : <EOL> """<STR_LIT>""" <EOL> others = syn . chunkedQuery ( '<STR_LIT>' % syn . test_parent . id ) <EOL> return [ result [ '<STR_LIT>' ] for result in others ] <EOL> def store_catch_412_HTTPError ( entity ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return syn . store ( entity ) <EOL> except SynapseHTTPError as err : <EOL> if err . response . status_code == <NUM_LIT> : <EOL> return None <EOL> raise </s>
