def test():
    from spinoff.util.testing import assert_raises

    with assert_raises(AssertionError):
        Restart(1)()

    assert Restart == Restart()

    assert Restart is Restart()

    assert Restart == Restart(ANY, ANY)
    assert Restart(1) == Restart(1)
    assert Restart(1) == Restart(ANY)
    assert Restart(ANY) == Restart(1)
    assert Restart(1, 2) == Restart(1, 2)
    assert Restart(1, 2) == Restart(1, 2)

    assert Restart(1, 2) != Restart(3, 4)

    with assert_raises(TypeError):
        Restart(within=3)

    assert repr(Restart) == 'Restart'
    assert repr(Restart(1)) == 'Restart(max=1)'
    assert repr(Restart(1, 2)) == 'Restart(max=1, within=2)'

    assert Restart != 'foo'

    for decision in [Resume, Restart, Stop, Escalate, Default, None]:
        assert isinstance(decision, Decision), decision

    assert not isinstance(None, _Restart)
Beispiel #2
0
def test_deep_recursion():
    def fact(n, result=1):
        if n <= 1:
            returnValue(result)
        else:
            noreturn(fact(n - 1, n * result))
        yield

    assert deferred_result(inlineCallbacks(fact)(1)) == 1
    assert deferred_result(inlineCallbacks(fact)(10)) == safe_fact(10)

    with recursion_limit(100):
        with assert_not_raises(RuntimeError):
            # +10 is actually too high here as we probably already have some stuff on the stack, but just to be sure
            assert deferred_result(inlineCallbacks(fact)(110)) == safe_fact(110)

    # ...and now let's prove that the same (tail call optimizable) algorithm without noreturn will eat up the stack

    def normal_fact(n, result=1):
        if n <= 1:
            returnValue(result)
        else:
            return normal_fact(n - 1, n * result)

    with recursion_limit(100):
        with assert_raises(RuntimeError):
            normal_fact(110)
Beispiel #3
0
def test_unordered_enum_value():
    v1 = EnumValue('V1')
    v2 = EnumValue('V2')

    assert v1 != v2, "two enum values should never be equal"
    assert v1 == v1, "enum value should always be equal to itself"
    assert v1 is v1, "enum value should always be identical to itself"

    with assert_raises(TypeError):
        v1 < v2

    with assert_raises(TypeError):
        v1 > v2

    with assert_raises(TypeError):
        EnumValue('X') < EnumValue('Y', order=1)
    with assert_raises(TypeError):
        EnumValue('X', order=1) < EnumValue('Y')
    with assert_raises(TypeError):
        EnumValue('X') > EnumValue('Y', order=1)
    with assert_raises(TypeError):
        EnumValue('X', order=1) > EnumValue('Y')
Beispiel #4
0
def test_enum_value_names_must_be_unique():
    with assert_raises(TypeError):
        enums('a', 'a')
    with assert_raises(TypeError):
        enums('a', 'a', 'b')
Beispiel #5
0
def test_enums_with_mixed_ordered_and_unordered_items_is_not_allowed():
    with assert_raises(TypeError):
        enums('a', 'b', (1, 'c'), (2, 'd'))
Beispiel #6
0
def test_looking_up_a_non_existent_local_actor_raises_runtime_error(defer):
    node = DummyNode()
    defer(node.stop)
    with assert_raises(RuntimeError):
        node.guardian / 'noexist'
Beispiel #7
0
def test_spawning_with_autogenerated_looking_name_raises_an_exception(defer):
    node = DummyNode()
    defer(node.stop)
    with assert_raises(ValueError):
        node.spawn(Actor, name='$1')
Beispiel #8
0
 def pre_start(self):
     self.spawn(Actor, name='a')
     with assert_raises(NameConflict):
         self.spawn(Actor, name='a')
     spawned.set()
Beispiel #9
0
def test_toplevel_actor_paths_must_be_unique(defer):
    node = DummyNode()
    defer(node.stop)
    node.spawn(Actor, name='a')
    with assert_raises(NameConflict):
        node.spawn(Actor, name='a')
Beispiel #10
0
def test_looking_up_a_non_existent_local_actor_raises_runtime_error(defer):
    node = DummyNode()
    defer(node.stop)
    with assert_raises(RuntimeError):
        node.guardian / 'noexist'
Beispiel #11
0
def test_spawning_with_autogenerated_looking_name_raises_an_exception(defer):
    node = DummyNode()
    defer(node.stop)
    with assert_raises(ValueError):
        node.spawn(Actor, name='$1')
Beispiel #12
0
 def pre_start(self):
     self.spawn(Actor, name='a')
     with assert_raises(NameConflict):
         self.spawn(Actor, name='a')
     spawned.set()
Beispiel #13
0
def test_toplevel_actor_paths_must_be_unique(defer):
    node = DummyNode()
    defer(node.stop)
    node.spawn(Actor, name='a')
    with assert_raises(NameConflict):
        node.spawn(Actor, name='a')