コード例 #1
0
def test_timeout(secs, timeout, expected_runtime, valid_func):
    model = make_model()

    model_broadcaster = ModelBroadcaster(model,
                                         ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))

    try:
        print '\nsleeping for {} secs...'.format(secs)
        if timeout is None:
            begin = time.time()
            res = model_broadcaster.cmd('sleep', {'secs': secs})
            end = time.time()
        else:
            begin = time.time()
            res = model_broadcaster.cmd('sleep', {'secs': secs},
                                        timeout=timeout)
            end = time.time()

        rt = end - begin

        # runtime duraton should be either:
        # - the expected response time plus a bit of overhead
        # - the expected timeout plus a bit of overhead
        print 'runtime: ', rt
        assert rt >= expected_runtime
        assert rt < expected_runtime + (expected_runtime * 0.06)

        assert valid_func(res)
    finally:
        model_broadcaster.stop()
コード例 #2
0
def test_spill_containers_have_uncertainty_off():
    model = make_model(uncertain=True)

    model_broadcaster = ModelBroadcaster(model,
                                         ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))
    print '\nSpill results:'
    res = model_broadcaster.cmd('get_spill_container_uncertainty', {})
    print [r for r in res]
コード例 #3
0
def test_step():
    model = make_model()

    model_broadcaster = ModelBroadcaster(model, ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))
    print '\nStep results:'
    res = model_broadcaster.cmd('step', {})
    assert len(res) == 9

    model_broadcaster.stop()
コード例 #4
0
def test_uncertainty_array_size():
    model = make_model()

    model_broadcaster = ModelBroadcaster(model,
                                         ('down',),
                                         ('down',))

    try:
        assert len(model_broadcaster.tasks) == 1
    finally:
        model_broadcaster.stop()

    model_broadcaster = ModelBroadcaster(model,
                                         ('down', 'up'),
                                         ('down', 'up'))

    try:
        assert len(model_broadcaster.tasks) == 4
    finally:
        model_broadcaster.stop()

    model_broadcaster = ModelBroadcaster(model,
                                         ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))

    try:
        assert len(model_broadcaster.tasks) == 9
    finally:
        model_broadcaster.stop()
コード例 #5
0
def test_step():
    model = make_model()

    model_broadcaster = ModelBroadcaster(model,
                                         ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))
    print '\nStep results:'
    res = model_broadcaster.cmd('step', {})
    assert len(res) == 9

    model_broadcaster.stop()
コード例 #6
0
def test_spill_containers_have_uncertainty_off():
    model = make_model(uncertain=True)

    model_broadcaster = ModelBroadcaster(model, ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))
    print '\nSpill results:'
    res = model_broadcaster.cmd('get_spill_container_uncertainty', {})
    print[r for r in res]
    assert not any([r for r in res])

    model_broadcaster.stop()
コード例 #7
0
def test_rewind():
    model = make_model()

    model_broadcaster = ModelBroadcaster(model, ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))
    print '\nRewind results:'
    res = model_broadcaster.cmd('rewind', {})

    assert len(res) == 9
    assert all([r is None for r in res])

    model_broadcaster.stop()
コード例 #8
0
def test_cache_dirs():
    model = make_model()

    model_broadcaster = ModelBroadcaster(model, ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))
    print '\nCache directory results:'
    res = model_broadcaster.cmd('get_cache_dir', {})

    assert all([os.path.isdir(d) for d in res])
    assert len(set(res)) == 9  # all dirs should be unique

    model_broadcaster.stop()
コード例 #9
0
def test_cache_dirs():
    model = make_model()

    model_broadcaster = ModelBroadcaster(model,
                                         ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))
    print '\nCache directory results:'
    res = model_broadcaster.cmd('get_cache_dir', {})

    assert all([os.path.isdir(d) for d in res])
    assert len(set(res)) == 9  # all dirs should be unique

    model_broadcaster.stop()
コード例 #10
0
def test_rewind():
    model = make_model()

    model_broadcaster = ModelBroadcaster(model,
                                         ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))
    print '\nRewind results:'
    res = model_broadcaster.cmd('rewind', {})

    assert len(res) == 9
    assert all([r is None for r in res])

    model_broadcaster.stop()
コード例 #11
0
def test_init():
    model = make_model()

    with raises(TypeError):
        ModelBroadcaster(model)

    with raises(TypeError):
        ModelBroadcaster(model,
                         ('down', 'normal', 'up'))

    model_broadcaster = ModelBroadcaster(model,
                                         ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))
    assert hasattr(model_broadcaster, 'id')

    model_broadcaster.stop()
コード例 #12
0
def test_child_exception():
    '''
        This one is a bit tricky.  We would like to simulate an exception
        by making the spill.amount a None value and then instantiating
        our broadcaster.  This is expected to raise a TypeError based on
        the current codebase, but could change.

        We would like to get the exception raised in the child process and
        re-raise it in the broadcaster parent process, complete with good
        traceback information.
    '''
    model = make_model(geojson_output=True)

    model.spills[0].amount = None
    print 'amount:', model.spills[0].amount

    try:
        _model_broadcaster = ModelBroadcaster(model,
                                              ('down', 'normal', 'up'),
                                              ('down', 'normal', 'up'))
    except Exception as e:
        #assert type(e) == TypeError
        assert type(e) == ValueError

        exc_type, exc_value, exc_traceback = sys.exc_info()
        fmt = traceback.format_exception(exc_type, exc_value, exc_traceback)

        last_file_entry = [l for l in fmt if l.startswith('  File ')][-1]
        last_file = last_file_entry.split('"')[1]

        assert os.path.basename(last_file) == 'spill.py'
コード例 #13
0
def test_timeout_2_times():
    model = make_model()

    model_broadcaster = ModelBroadcaster(model,
                                         ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))

    try:
        #
        # First, we set a short timeout for a command, but a shorter command.
        # The command should succeed
        #
        secs, timeout, expected_runtime = 4, 5, 4
        print '\nsleeping for {} secs...'.format(secs)

        begin = time.time()
        res = model_broadcaster.cmd('sleep', {'secs': secs}, timeout=timeout)
        end = time.time()

        rt = end - begin

        assert rt >= expected_runtime
        assert rt < expected_runtime + (expected_runtime * 0.06)
        assert is_valid(res)

        #
        # Next, run a command with no timeout specified.  The timeout should
        # have reverted back to the default, and the command should succeed.
        #
        secs, expected_runtime = 9, 9
        print '\nsleeping for {} secs...'.format(secs)

        begin = time.time()
        res = model_broadcaster.cmd('sleep', {'secs': secs})
        end = time.time()

        rt = end - begin

        assert rt >= expected_runtime
        assert rt < expected_runtime + (expected_runtime * 0.06)
        assert is_valid(res)

    finally:
        model_broadcaster.stop()
コード例 #14
0
def test_full_run():
    model = make_model()

    model_broadcaster = ModelBroadcaster(model,
                                         ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))
    print '\nNumber of time steps:'
    num_steps = model_broadcaster.cmd('num_time_steps', {})
    assert len(num_steps) == 9
    assert len(set(num_steps)) == 1  # all models have the same number of steps

    print '\nStep results:'
    res = model_broadcaster.cmd('full_run', {})
    assert len(res) == 9

    for n, r in zip(num_steps, res):
        assert len(r) == n

    model_broadcaster.stop()
コード例 #15
0
def test_init():
    model = make_model()

    with pytest.raises(TypeError):
        # no uncertainty arguments
        ModelBroadcaster(model)

    with pytest.raises(TypeError):
        # no spill amount uncertainties
        ModelBroadcaster(model,
                         ('down', 'normal', 'up'))

    model_broadcaster = ModelBroadcaster(model,
                                         ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))

    try:
        assert hasattr(model_broadcaster, 'id')
    finally:
        model_broadcaster.stop()
コード例 #16
0
def set_uncertain_models(request):
    session_id = request.session.session_id
    uncertain_models = request.registry.settings['uncertain_models']

    active_model = get_active_model(request)
    if active_model:
        model_broadcaster = ModelBroadcaster(active_model,
                                             ('down', 'normal', 'up'),
                                             ('down', 'normal', 'up'),
                                             'ipc_files')

        uncertain_models[session_id] = model_broadcaster
コード例 #17
0
def test_timeout(secs, timeout, expected_runtime, valid_func):
    model = make_model()

    model_broadcaster = ModelBroadcaster(model,
                                         ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))

    try:
        print '\nsleeping for {} secs...'.format(secs)
        if timeout is None:
            begin = time.time()
            res = model_broadcaster.cmd('sleep', {'secs': secs})
            end = time.time()
        else:
            begin = time.time()
            res = model_broadcaster.cmd('sleep', {'secs': secs},
                                        timeout=timeout)
            end = time.time()

        rt = end - begin

        # runtime duraton should be either:
        # - the expected response time plus a bit of overhead
        # - the expected timeout plus a bit of overhead
        print 'runtime: ', rt
        assert rt >= expected_runtime
        assert rt < expected_runtime + (expected_runtime * 0.06)

        assert valid_func(res)
    finally:
        model_broadcaster.stop()
コード例 #18
0
def test_weathering_output_only():
    model = make_model(geojson_output=True)

    model_broadcaster = ModelBroadcaster(model,
                                         ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))

    res = model_broadcaster.cmd('get_outputters', {})

    assert not [o for r in res for o in r
                if not isinstance(o, WeatheringOutput)]

    res = model_broadcaster.cmd('step', {})

    assert len(res) == 9

    assert [r.keys() for r in res
            if ('step_num' in r and
                'valid' in r and
                'WeatheringOutput' in r)]

    model_broadcaster.stop()
コード例 #19
0
def test_weathering_output_only():
    model = make_model(geojson_output=True)

    model_broadcaster = ModelBroadcaster(model,
                                         ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))

    print '\nOutputter results:'
    res = model_broadcaster.cmd('get_outputters', {})

    assert not [o for r in res for o in r
                if not isinstance(o, WeatheringOutput)]

    print '\nStep results:'
    res = model_broadcaster.cmd('step', {})

    assert len(res) == 9

    # added a 'valid' flag to output
    assert [r.keys() for r in res
            if len(r.keys()) == 2
            and 'WeatheringOutput' in r]

    model_broadcaster.stop()
コード例 #20
0
def test_uncertainty_array_indexing():
    model = make_model()

    model_broadcaster = ModelBroadcaster(model, ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))

    try:
        res = model_broadcaster.cmd('get_wind_timeseries', {},
                                    ('down', 'down'))
        assert np.allclose([r[0] for r in res], 17.449237)

        res = model_broadcaster.cmd('get_spill_amounts', {}, ('down', 'down'))
        assert np.isclose(res[0], 333.33333)

        res = model_broadcaster.cmd('get_wind_timeseries', {}, ('up', 'up'))
        assert np.allclose([r[0] for r in res], 20.166224)

        res = model_broadcaster.cmd('get_spill_amounts', {}, ('up', 'up'))
        assert np.isclose(res[0], 1666.66666)
    finally:
        model_broadcaster.stop()
コード例 #21
0
def test_init():
    model = make_model()

    with raises(TypeError):
        ModelBroadcaster(model)

    with raises(TypeError):
        ModelBroadcaster(model, ('down', 'normal', 'up'))

    model_broadcaster = ModelBroadcaster(model, ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))
    assert hasattr(model_broadcaster, 'id')

    model_broadcaster.stop()
コード例 #22
0
def test_uncertainty_array_indexing():
    model = make_model()

    model_broadcaster = ModelBroadcaster(model, ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))

    print '\nGetting time & spill values for just the (down, down) model:'
    res = model_broadcaster.cmd('get_wind_timeseries', {}, ('down', 'down'))
    assert np.allclose([r[0] for r in res], 17.449237)

    res = model_broadcaster.cmd('get_spill_amounts', {}, ('down', 'down'))
    assert np.isclose(res[0], 333.33333)

    print '\nGetting time & spill values for just the (up, up) model:'
    res = model_broadcaster.cmd('get_wind_timeseries', {}, ('up', 'up'))
    print 'get_wind_timeseries:'
    assert np.allclose([r[0] for r in res], 20.166224)

    res = model_broadcaster.cmd('get_spill_amounts', {}, ('up', 'up'))
    assert np.isclose(res[0], 1666.66666)

    model_broadcaster.stop()
コード例 #23
0
def test_init():
    model = make_model()

    with pytest.raises(TypeError):
        # no uncertainty arguments
        ModelBroadcaster(model)

    with pytest.raises(TypeError):
        # no spill amount uncertainties
        ModelBroadcaster(model, ('down', 'normal', 'up'))

    model_broadcaster = ModelBroadcaster(model, ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))

    try:
        assert hasattr(model_broadcaster, 'id')
    finally:
        model_broadcaster.stop()
コード例 #24
0
def test_full_run():
    model = make_model()

    model_broadcaster = ModelBroadcaster(model, ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))
    print '\nNumber of time steps:'
    num_steps = model_broadcaster.cmd('num_time_steps', {})
    assert len(num_steps) == 9
    assert len(set(num_steps)) == 1  # all models have the same number of steps

    print '\nStep results:'
    res = model_broadcaster.cmd('full_run', {})
    assert len(res) == 9

    for n, r in zip(num_steps, res):
        assert len(r) == n

    model_broadcaster.stop()
コード例 #25
0
def test_uncertainty_array_indexing():
    model = make_model()

    model_broadcaster = ModelBroadcaster(model,
                                         ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))

    try:
        res = model_broadcaster.cmd('get_wind_timeseries', {},
                                    ('down', 'down'))
        assert np.allclose([r[0] for r in res], 17.449237)

        res = model_broadcaster.cmd('get_spill_amounts', {}, ('down', 'down'))
        assert np.isclose(res[0], 333.33333)

        res = model_broadcaster.cmd('get_wind_timeseries', {}, ('up', 'up'))
        assert np.allclose([r[0] for r in res], 20.166224)

        res = model_broadcaster.cmd('get_spill_amounts', {}, ('up', 'up'))
        assert np.isclose(res[0], 1666.66666)
    finally:
        model_broadcaster.stop()
コード例 #26
0
def test_timeout_2_times():
    model = make_model()

    model_broadcaster = ModelBroadcaster(model,
                                         ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))

    try:
        #
        # First, we set a short timeout for a command, but a shorter command.
        # The command should succeed
        #
        secs, timeout, expected_runtime = 4, 5, 4
        print '\nsleeping for {} secs...'.format(secs)

        begin = time.time()
        res = model_broadcaster.cmd('sleep', {'secs': secs}, timeout=timeout)
        end = time.time()

        rt = end - begin

        assert rt >= expected_runtime
        assert rt < expected_runtime + (expected_runtime * 0.06)
        assert is_valid(res)

        #
        # Next, run a command with no timeout specified.  The timeout should
        # have reverted back to the default, and the command should succeed.
        #
        secs, expected_runtime = 9, 9
        print '\nsleeping for {} secs...'.format(secs)

        begin = time.time()
        res = model_broadcaster.cmd('sleep', {'secs': secs})
        end = time.time()

        rt = end - begin

        assert rt >= expected_runtime
        assert rt < expected_runtime + (expected_runtime * 0.06)
        assert is_valid(res)

    finally:
        model_broadcaster.stop()
コード例 #27
0
def test_uncertainty_array_indexing():
    model = make_model()

    model_broadcaster = ModelBroadcaster(model,
                                         ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))

    print '\nGetting time & spill values for just the (down, down) model:'
    res = model_broadcaster.cmd('get_wind_timeseries', {}, ('down', 'down'))
    assert np.allclose([r[0] for r in res], 17.449237)

    res = model_broadcaster.cmd('get_spill_amounts', {}, ('down', 'down'))
    assert np.isclose(res[0], 333.33333)

    print '\nGetting time & spill values for just the (up, up) model:'
    res = model_broadcaster.cmd('get_wind_timeseries', {}, ('up', 'up'))
    print 'get_wind_timeseries:'
    assert np.allclose([r[0] for r in res], 20.166224)

    res = model_broadcaster.cmd('get_spill_amounts', {}, ('up', 'up'))
    assert np.isclose(res[0], 1666.66666)

    model_broadcaster.stop()
コード例 #28
0
def test_weathering_output_only():
    model = make_model(geojson_output=True)

    model_broadcaster = ModelBroadcaster(model, ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))

    res = model_broadcaster.cmd('get_outputters', {})

    assert not [
        o for r in res for o in r if not isinstance(o, WeatheringOutput)
    ]

    res = model_broadcaster.cmd('step', {})

    assert len(res) == 9

    assert [
        r.keys() for r in res
        if ('step_num' in r and 'valid' in r and 'WeatheringOutput' in r)
    ]

    model_broadcaster.stop()
コード例 #29
0
                         Burn(),
                         Skimmer()]

    print 'adding outputters'
    model.outputters += WeatheringOutput()

    return model


if __name__ == '__main__':
    scripting.make_images_dir()

    model = make_model()

    model_broadcaster = ModelBroadcaster(model,
                                         ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))

    print '\nStep results:'
    pp.pprint(model_broadcaster.cmd('step', {}))

    print '\nGetting wind timeseries for all models:'
    pp.pprint(model_broadcaster.cmd('get_wind_timeseries', {}))

    print '\nGetting spill amounts for all models:'
    pp.pprint(model_broadcaster.cmd('get_spill_amounts', {}))

    print '\nGetting time & spill values for just the (down, down) model:'
    pp.pprint((model_broadcaster.cmd('get_wind_timeseries', {},
                                     ('down', 'down')),
               model_broadcaster.cmd('get_spill_amounts', {},
コード例 #30
0
def test_uncertainty_array_size():
    model = make_model()

    model_broadcaster = ModelBroadcaster(model, ('down', ), ('down', ))
    assert len(model_broadcaster.tasks) == 1
    model_broadcaster.stop()

    model_broadcaster = ModelBroadcaster(model, ('down', 'up'), ('down', 'up'))
    assert len(model_broadcaster.tasks) == 4
    model_broadcaster.stop()

    model_broadcaster = ModelBroadcaster(model, ('down', 'normal', 'up'),
                                         ('down', 'normal', 'up'))
    assert len(model_broadcaster.tasks) == 9
    model_broadcaster.stop()