Exemplo n.º 1
0
def test_exception():
    def run(x):
        raise KeyError(x)

    c = Completion(value=3).then(run)
    c.finalize()
    with pytest.raises(KeyError):
        raise_if_exception(c)
Exemplo n.º 2
0
def test_progress():
    c = some_complex_completion()
    mgr = mock.MagicMock()
    mgr.process = lambda cs: [c.finalize(None) for c in cs]

    progress_val = 0.75
    c._last_promise().then(
        on_complete=ProgressReference(message='hello world',
                                      mgr=mgr,
                                      completion=lambda: Completion(
                                          on_complete=lambda _: progress_val))
    )
    mgr.remote.assert_called_with('progress', 'update', c.progress_reference.progress_id, 'hello world', 0.0, [('origin', 'orchestrator')])

    c.finalize()
    mgr.remote.assert_called_with('progress', 'complete', c.progress_reference.progress_id)

    c.progress_reference.update()
    mgr.remote.assert_called_with('progress', 'update', c.progress_reference.progress_id, 'hello world', progress_val, [('origin', 'orchestrator')])
    assert not c.progress_reference.effective

    progress_val = 1
    c.progress_reference.update()
    assert c.progress_reference.effective
    mgr.remote.assert_called_with('progress', 'complete', c.progress_reference.progress_id)
Exemplo n.º 3
0
    def run(x):
        def two(_):
            return execute(x * 2)

        return Completion.with_progress(message='message',
                                        on_complete=two,
                                        mgr=mgr)
Exemplo n.º 4
0
def test_fail():
    c = Completion().then(lambda _: 3)
    c._first_promise.fail(KeyError())
    assert isinstance(c.exception, KeyError)

    with pytest.raises(ValueError,
                  match='Invalid State: called fail, but Completion is already finished: {}'.format(
                      str(ZeroDivisionError()))):
        c._first_promise.fail(ZeroDivisionError())
Exemplo n.º 5
0
def test_side_effect():
    foo = {'x': 1}

    def run(x):
        foo['x'] = x

    foo['x'] = 1
    Completion(value=3).then(run).finalize()
    assert foo['x'] == 3
Exemplo n.º 6
0
def test_with_progress():
    mgr = mock.MagicMock()
    mgr.process = lambda cs: [c.finalize(None) for c in cs]

    def execute(y):
        return str(y)

    def run(x):
        def two(_):
            return execute(x * 2)

        return Completion.with_progress(message='message',
                                        on_complete=two,
                                        mgr=mgr)

    c = Completion(on_complete=lambda x: x * 10).then(run)._first_promise
    c.finalize(2)
    assert c.result == '40'
    c.progress_reference.update()
    assert c.progress_reference.effective
Exemplo n.º 7
0
def test_pretty_print():
    mgr = mock.MagicMock()
    mgr.process = lambda cs: [c.finalize(None) for c in cs]

    def add_one(x):
        return x+1

    c = Completion(value=1, on_complete=add_one).then(
        str
    ).add_progress('message', mgr)

    assert c.pretty_print() == """<Completion>[
       add_one(1),
       str(...),
       ProgressReference(...),
]"""
    c.finalize()
    assert c.pretty_print() == """<Completion>[
(done) add_one(1),
(done) str(2),
(done) ProgressReference('2'),
]"""

    p = some_complex_completion()
    assert p.pretty_print() == """<Completion>[
       <lambda>(3),
       lambda x: x(...),
]"""
    p.finalize()
    assert p.pretty_print() == """<Completion>[
(done) <lambda>(3),
(done) <lambda>(4),
(done) lambda x: x(5),
(done) lambda x: x(5),
]"""

    assert p.result == 5
Exemplo n.º 8
0
def test_raise():
    c = Completion()
    c._exception = ZeroDivisionError()
    with pytest.raises(ZeroDivisionError):
        raise_if_exception(c)
Exemplo n.º 9
0
def test_promise_flat():
    p = Completion()
    p.then(lambda r1: Completion(value=r1 + ' there').then(
        lambda r11: r11 + '!'))
    p.finalize('hello')
    assert p.result == 'hello there!'
Exemplo n.º 10
0
def some_complex_completion():
    c = Completion(value=3).then(
        lambda three: Completion(value=three + 1).then(
            lambda four: four + 1))
    return c
Exemplo n.º 11
0
def test_promise_mondatic_then():
    p = Completion(value=3)
    p.then(lambda three: Completion(value=three + 1))
    p.finalize()
    assert p.result == 4
Exemplo n.º 12
0
def test_promise_then():
    p = Completion(value=3).then(lambda three: three + 1)
    p.finalize()
    assert p.result == 4
Exemplo n.º 13
0
def test_promise():
    p = Completion(value=3)
    p.finalize()
    assert p.result == 3
Exemplo n.º 14
0
def test_fail():
    c = Completion().then(lambda _: 3)
    c._first_promise.fail(KeyError())
    assert isinstance(c.exception, KeyError)
Exemplo n.º 15
0
    def test_scale_up(self, _apply_mds, _describe_service, _list_daemons, _get,
                      mds_autoscaler_module: MDSAutoscaler):
        daemons = Completion(value=[
            DaemonDescription(hostname='myhost',
                              daemon_type='mds',
                              daemon_id='fs_name.myhost.a'),
            DaemonDescription(hostname='myhost',
                              daemon_type='mds',
                              daemon_id='fs_name.myhost.b'),
        ])
        daemons.finalize()
        _list_daemons.return_value = daemons

        services = Completion(value=[
            ServiceDescription(spec=ServiceSpec(service_type='mds',
                                                service_id='fs_name',
                                                placement=PlacementSpec(
                                                    count=2)))
        ])
        services.finalize()
        _describe_service.return_value = services

        apply = Completion(value='')
        apply.finalize()
        _apply_mds.return_value = apply

        _get.return_value = {
            'filesystems': [{
                'mdsmap': {
                    'fs_name': 'fs_name',
                    'in': [{
                        'name': 'mds.fs_name.myhost.a',
                    }],
                    'standby_count_wanted': 2,
                }
            }],
            'standbys': [{
                'name': 'mds.fs_name.myhost.b',
            }],
        }
        mds_autoscaler_module.notify('fs_map', None)

        _apply_mds.assert_called_with(
            ServiceSpec(service_type='mds',
                        service_id='fs_name',
                        placement=PlacementSpec(count=3)))