Esempio n. 1
0
def test_invalid_max_error_delay_factor(options):
    options.default_poll_interval = 1
    for i in [-1, 0]:
        options.max_poll_error_delay_factor = i

        with pytest.raises(ValueError):
            repositories.Manager(MagicMock(), MagicMock(), MagicMock())
Esempio n. 2
0
def mock_manager(use_clock=False):
    """
    Returns an instance of repositories.Manager with a mock database, an
    empty shelf (using a dict instead of an actual shelf instance), and
    an example repository
    """
    repostore = repositories.RepositoryStore({})
    repostore._shelf = {
        'repo_a': {
            'id': 'repo_a',
            'service': {
                'location': 'http://a.test'
            }
        }
    }
    queue = Queue.Queue()
    notification_q = repositories.Notification(queue)
    scheduler = repositories.Scheduler()
    scheduler._use_clock = use_clock
    if use_clock:
        scheduler._time = iter(frange(0, 100000, 0.5)).next
    notification_q.connect_with(scheduler)
    manager = repositories.Manager(Mock(), repostore, scheduler)
    manager.db.add_entities.return_value = make_future({'errors': []})
    return scheduler, repostore, manager
Esempio n. 3
0
def test_next_poll_interval_after_successful():
    """
    If successful the next poll interval, should be a random number in the
    manager's interval range.
    """
    scheduler = MagicMock()
    scheduler.get.return_value = make_future('repo1')
    manager = repositories.Manager(MagicMock(), MagicMock(), scheduler)
    manager.fetch_identifiers = MagicMock(
        return_value=make_future({'errors': 0}))

    yield manager.fetch('repo1')

    (repo_id, next_poll_interval), _ = scheduler.schedule.call_args

    min_interval, max_interval = manager.poll_interval_range
    assert min_interval <= next_poll_interval <= max_interval
    assert repo_id == 'repo1'
Esempio n. 4
0
def test_next_poll_interval_max_error_delay():
    """
    If there was an error, the next poll interval, should be a random number
    in the manager's interval range multiplied by the number errors up to
    the max_error_delay_factor
    """
    scheduler = MagicMock()
    scheduler.get.return_value = make_future('repo1')
    manager = repositories.Manager(MagicMock(), MagicMock(), scheduler)
    manager.fetch_identifiers = MagicMock(return_value=make_future(
        {'errors': manager.max_error_delay_factor + 1}))

    yield manager.fetch('repo1')

    (repo_id, next_poll_interval), _ = scheduler.schedule.call_args

    min_interval, max_interval = manager.poll_interval_range
    assert min_interval * manager.max_error_delay_factor <= next_poll_interval
    assert max_interval * manager.max_error_delay_factor >= next_poll_interval
Esempio n. 5
0
def test_unknown_repository(API):
    """
    If a repository is not in the RepositoryStore then the manager should
    not try to fetch the repository, not add it to the repository store and
    not reschedule it. We don't want to keep trying to fetch unknown
    repositories.

    This scenario can arise because a notification is always scheduled to be
    fetched on the assumption that we will be able to find an unknown
    repository in the accounts service.
    """
    scheduler = MagicMock()
    scheduler.get.return_value = make_future('repo1')
    store = repositories.RepositoryStore({})
    store._shelf = {}
    # get_repository raises a KeyError if repo is unknown
    store.get_repository = MagicMock(side_effect=KeyError)

    manager = repositories.Manager(MagicMock(), store, scheduler)
    yield manager.fetch('repo1')

    assert not API().repository.repositories[''].assets.identifiers.called
    assert not scheduler.schedule.called
    assert store._shelf == {}