def test_slow_unlocker(): """Manually grabs a lock and makes sure the get/store methods are blocked.""" # Make a file to manually lock path = utils.make_bogus_data_file() schedule_for_cleanup(path) contention = File(path, parent=syn.test_parent) contention = syn.store(contention) # Lock the Cache Map cacheDir = cache.determine_cache_directory(contention) cache.obtain_lock_and_read_cache(cacheDir) # Start a few calls to get/store that should not complete yet store_thread = wrap_function_as_child_thread(lambda: store_catch_412_HTTPError(contention)) get_thread = wrap_function_as_child_thread(lambda: syn.get(contention)) thread.start_new_thread(store_thread, ()) thread.start_new_thread(get_thread, ()) time.sleep(min(5, cache.CACHE_LOCK_TIME / 2)) # Make sure the threads did not finish assert syn.test_threadsRunning > 0 cache.write_cache_then_release_lock(cacheDir) # Let the threads go while syn.test_threadsRunning > 0: time.sleep(1) collect_errors_and_fail()
def test_slow_unlocker(): """Manually grabs a lock and makes sure the get/store methods are blocked.""" # Make a file to manually lock path = utils.make_bogus_data_file() schedule_for_cleanup(path) contention = File(path, parent=syn.test_parent) contention = syn.store(contention) # Lock the Cache Map cacheDir = cache.determine_cache_directory(contention['dataFileHandleId']) cache.obtain_lock_and_read_cache(cacheDir) # Start a few calls to get/store that should not complete yet store_thread = wrap_function_as_child_thread(lambda: store_catch_412_HTTPError(contention)) get_thread = wrap_function_as_child_thread(lambda: syn.get(contention)) thread.start_new_thread(store_thread, ()) thread.start_new_thread(get_thread, ()) time.sleep(min(5, cache.CACHE_LOCK_TIME / 2)) # Make sure the threads did not finish assert syn.test_threadsRunning > 0 cache.write_cache_then_release_lock(cacheDir) # Let the threads go while syn.test_threadsRunning > 0: time.sleep(1) collect_errors_and_fail()
def test_obtain_lock_and_read_cache(lock_valid_mock, time_mock): cacheDir = tempfile.mkdtemp() cacheLock = os.path.join(cacheDir, '.cacheMap.lock') os.makedirs(cacheLock) oldUnlockWaitTime = cache.CACHE_UNLOCK_WAIT_TIME try: cache.CACHE_UNLOCK_WAIT_TIME = 0 # -- Make sure the method retries appropriately -- # Have the method retry locking four times, succeeding on the last one waitStack = [False, True, True, True] time_mock.side_effect = lambda: 0 lock_valid_mock.side_effect = lambda x: waitStack.pop() assert cache.obtain_lock_and_read_cache(cacheDir) == {} assert len(waitStack) == 0 # -- Make sure the method times out -- # time.time is called two times within the loop and once outside the loop timeStack = [ cache.CACHE_MAX_LOCK_TRY_TIME, cache.CACHE_MAX_LOCK_TRY_TIME, 0 ] time_mock.side_effect = lambda: timeStack.pop() assert_raises(Exception, cache.obtain_lock_and_read_cache, cacheDir) assert len(timeStack) == 0 finally: cache.CACHE_UNLOCK_WAIT_TIME = oldUnlockWaitTime
def test_obtain_lock_and_read_cache(lock_valid_mock, time_mock): cacheDir = tempfile.mkdtemp() cacheLock = os.path.join(cacheDir, '.lock') os.makedirs(cacheLock) oldUnlockWaitTime = cache.CACHE_UNLOCK_WAIT_TIME try: cache.CACHE_UNLOCK_WAIT_TIME = 0 # -- Make sure the method retries appropriately -- # Have the method retry locking four times, succeeding on the last one waitStack = [False, True, True, True] time_mock.side_effect = lambda: 0 lock_valid_mock.side_effect = lambda x: waitStack.pop() assert cache.obtain_lock_and_read_cache(cacheDir) == {} assert len(waitStack) == 0 # -- Make sure the method times out -- # time.time is called two times within the loop and once outside the loop timeStack = [cache.CACHE_MAX_LOCK_TRY_TIME, cache.CACHE_MAX_LOCK_TRY_TIME, 0] time_mock.side_effect = lambda: timeStack.pop() assert_raises(Exception, cache.obtain_lock_and_read_cache, cacheDir) assert len(timeStack) == 0 finally: cache.CACHE_UNLOCK_WAIT_TIME = oldUnlockWaitTime
def test_is_lock_valid(): # Lock should be valid right after creation cacheDir = tempfile.mkdtemp() cache.obtain_lock_and_read_cache(cacheDir) assert cache.is_lock_valid(os.path.join(cacheDir, '.cacheMap.lock')) cache.write_cache_then_release_lock(cacheDir)
def test_is_lock_valid(): # Lock should be valid right after creation cacheDir = tempfile.mkdtemp() cache.obtain_lock_and_read_cache(cacheDir) assert cache.is_lock_valid(os.path.join(cacheDir, '.lock')) cache.write_cache_then_release_lock(cacheDir)