示例#1
0
def test_reset():
    state.reset()
    with pytest.raises(AssertionError):
        state.current_container()

    state.init()
    assert state.current_container() is not None
示例#2
0
def test_state_override_safety():
    from antidote._internal import state

    state.init()
    container = state.current_container()

    def create(c):
        assert c is container
        return Container()

    def worker():
        ThreadSafetyTest.random_delay()
        state.override(create)

    ThreadSafetyTest.run(worker)
示例#3
0
def test_debug():
    with world.test.new():
        world.test.singleton("original", 2)

        with world.test.clone(keep_singletons=True):
            from antidote._internal import state

            world.test.override.singleton("s", 1)

            @world.test.override.factory('f')
            def f():
                return 2

            c = state.current_container()
            assert "Override" in c.debug("s").info
            assert "Singleton" in c.debug("s").info

            assert "Override" in c.debug("f").info
            assert "Factory" in c.debug("f").info

            assert "Singleton" in c.debug("original").info
示例#4
0
def test_state_init_safety():
    from antidote._internal import state
    from antidote._internal import world as world_utils

    old_new_container = world_utils.new_container

    called = 0

    def new_container():
        nonlocal called
        called += 1
        ThreadSafetyTest.random_delay()
        return old_new_container()

    state.reset()
    world_utils.new_container = new_container

    try:
        ThreadSafetyTest.run(state.init)
    finally:
        world_utils.new_container = old_new_container

    assert state.current_container() is not None
    assert called == 1
示例#5
0
def test_double_init():
    state.init()
    state.init()
    assert state.current_container() is not None