Beispiel #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()
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()
Beispiel #3
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]
Beispiel #4
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()
Beispiel #5
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()
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()
Beispiel #7
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()
Beispiel #8
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()
Beispiel #9
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()
Beispiel #10
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()
Beispiel #11
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()
Beispiel #12
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()
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()
Beispiel #14
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()
Beispiel #15
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()
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()
Beispiel #17
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()
Beispiel #18
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()
Beispiel #19
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()
Beispiel #20
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()
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()
    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', {},
                                     ('down', 'down')),
               ))

    print '\nGetting time & spill values for just the (normal, normal) model:'