Exemplo n.º 1
0
def test_write_cache_then_release_lock(lock_valid_mock, read_mock):
    cacheDir = tempfile.mkdtemp()
    cacheLock = os.path.join(cacheDir, '.cacheMap.lock')
    # -- Make sure the .lock is removed along with any junk inside --
    os.makedirs(os.path.join(cacheLock, 'random folder'))
    os.makedirs(os.path.join(cacheLock, 'OtherExtraneousFolder'))
    os.makedirs(os.path.join(cacheLock, 'more_stuff_that_should_not_be_here'))
    f, _ = tempfile.mkstemp(dir=cacheLock)
    os.close(f)

    cache.write_cache_then_release_lock(cacheDir)
    assert not os.path.exists(cacheLock)
    assert not lock_valid_mock.called

    # -- Make sure the .cacheMap is written to correctly --
    # Pretend the lock has expired
    lock_valid_mock.return_value = False

    # Pretend the .cacheMap contains some JSON
    read_mock.return_value = {"a": "b", "c": "d", "overwrite": "me"}

    # Make the lock and remove it
    os.makedirs(cacheLock)
    cache.write_cache_then_release_lock(cacheDir,
                                        {"overwrite": "I've lost my identity"})
    assert not os.path.exists(cacheLock)

    lock_valid_mock.assert_called_once_with(cacheLock)
    with open(os.path.join(cacheDir, '.cacheMap'), 'r') as f:
        written = json.load(f)
        assert written == {
            "a": "b",
            "c": "d",
            "overwrite": "I've lost my identity"
        }
Exemplo n.º 2
0
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()
Exemplo n.º 3
0
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_write_cache_then_release_lock(lock_valid_mock, read_mock):
    cacheDir = tempfile.mkdtemp()
    cacheLock = os.path.join(cacheDir, '.lock')
    # -- Make sure the .lock is removed along with any junk inside --
    os.makedirs(os.path.join(cacheLock, 'random folder'))
    os.makedirs(os.path.join(cacheLock, 'OtherExtraneousFolder'))
    os.makedirs(os.path.join(cacheLock, 'more_stuff_that_should_not_be_here'))
    f, _ = tempfile.mkstemp(dir=cacheLock)
    os.close(f)
    
    cache.write_cache_then_release_lock(cacheDir)
    assert not os.path.exists(cacheLock)
    assert not lock_valid_mock.called
    
    # -- Make sure the .cacheMap is written to correctly --
    # Pretend the lock has expired
    lock_valid_mock.return_value = False
    
    # Pretend the .cacheMap contains some JSON
    read_mock.return_value = {"a": "b", "c": "d", "overwrite": "me"}
    
    # Make the lock and remove it
    os.makedirs(cacheLock)
    cache.write_cache_then_release_lock(cacheDir, {"overwrite": "I've lost my identity"})
    assert not os.path.exists(cacheLock)
    
    lock_valid_mock.assert_called_once_with(cacheLock)
    with open(os.path.join(cacheDir, '.cacheMap'), 'r') as f:
        written = json.load(f)
        assert written == {"a": "b", "c": "d", "overwrite": "I've lost my identity"}
Exemplo n.º 5
0
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)