Beispiel #1
0
def test_nested_doers():
    """
    Test Doist running nested DoDoers and Doers
    """

    tock = 0.03125
    doist = doing.Doist(tock=tock)
    assert doist.tyme == 0.0  # on next cycle
    assert doist.tock == tock == 0.03125
    assert doist.real == False
    assert doist.limit == None
    assert doist.doers == []

    doer0 = doing.ExDoer(tock=0.0, tymth=doist.tymen())
    doer1 = doing.ExDoer(tock=tock*2, tymth=doist.tymen())
    assert doer0.tock == 0.0
    assert doer1.tock == tock * 2
    aDoers = [doer0, doer1]
    for doer in aDoers:
        assert doer.states == []
        assert doer.count == None
        assert doer.done == None

    aDoer = doing.DoDoer(tock=0.0, tymth=doist.tymen(), doers=aDoers)
    assert aDoer.doers == aDoers
    assert aDoer.done == None


    doer2 = doing.ExDoer(tock=0.0, tymth=doist.tymen())
    doer3 = doing.ExDoer(tock=tock*4, tymth=doist.tymen())
    assert doer2.tock == 0.0
    assert doer3.tock == tock * 4
    bDoers = [doer2, doer3]
    for doer in bDoers:
        assert doer.states == []
        assert doer.count == None
        assert doer.done == None

    bDoer = doing.DoDoer(tock=tock*2, tymth=doist.tymen(), doers=bDoers)
    assert bDoer.doers == bDoers
    assert bDoer.done == None

    doers = [aDoer, bDoer]
    ticks = 8
    limit = tock * ticks
    doist.do(doers=doers, limit=limit)  # run em all
    assert doist.tyme == limit == 0.25

    assert aDoer.done == True
    assert bDoer.done == False


    assert doer0.states == [State(tyme=0.0, context='enter', feed=0.0, count=0),
                            State(tyme=0.0, context='recur', feed=0.0, count=1),
                            State(tyme=0.03125, context='recur', feed=0.03125, count=2),
                            State(tyme=0.0625, context='recur', feed=0.0625, count=3),
                            State(tyme=0.09375, context='recur', feed=0.09375, count=4),
                            State(tyme=0.09375, context='exit', feed=None, count=5)]
    assert doer1.done == True

    assert doer1.states == [State(tyme=0.0, context='enter', feed=0.0, count=0),
                            State(tyme=0.0, context='recur', feed=0.0, count=1),
                            State(tyme=0.0625, context='recur', feed=0.0625, count=2),
                            State(tyme=0.125, context='recur', feed=0.125, count=3),
                            State(tyme=0.1875, context='recur', feed=0.1875, count=4),
                            State(tyme=0.1875, context='exit', feed=None, count=5)]
    assert doer1.done == True

    assert doer2.states == [State(tyme=0.0, context='enter', feed=0.0, count=0),
                            State(tyme=0.0, context='recur', feed=0.0, count=1),
                            State(tyme=0.0625, context='recur', feed=0.0625, count=2),
                            State(tyme=0.125, context='recur', feed=0.125, count=3),
                            State(tyme=0.1875, context='recur', feed=0.1875, count=4),
                            State(tyme=0.1875, context='exit', feed=None, count=5)]
    assert doer2.done == True

    assert doer3.states == [State(tyme=0.0, context='enter', feed=0.0, count=0),
                            State(tyme=0.0, context='recur', feed=0.0, count=1),
                            State(tyme=0.125, context='recur', feed=0.125, count=2),
                            State(tyme=0.25, context='close', feed=None, count=3),
                            State(tyme=0.25, context='exit', feed=None, count=4)]
    assert doer3.done == False
    """End Test """
Beispiel #2
0
def test_run_sam_eve_demo():
    """
    Test demo setupController and run with DoDoers and Doist
    """
    help.ogler.resetLevel(level=logging.DEBUG)

    tock = 0.03125
    expire = 2.0
    doist = doing.Doist(limit=expire, tock=tock, real=True)

    name = "sam"
    remote = 5621
    local = 5620

    secrets = [
        'ArwXoACJgOleVZ2PY7kXn7rA0II0mHYDhc6WrBH8fDAc',
        'A6zz7M08-HQSFq92sJ8KJOT2cZ47x7pXFQLPB0pckB3Q',
        'AcwFTk-wgk3ZT2buPRIbK-zxgPx-TKbaegQvPEivN90Y',
        'Alntkt3u6dDgiQxTATr01dy8M72uuaZEf9eTdM-70Gk8',
        'A1-QxDkso9-MR1A8rZz_Naw6fgaAtayda8hrbkRVVu1E',
        'AKuYMe09COczwf2nIoD5AE119n7GLFOVFlNLxZcKuswc',
        'AxFfJTcSuEE11FINfXMqWttkZGnUZ8KaREhrnyAXTsjw',
        'ALq-w1UKkdrppwZzGTtz4PWYEeWm0-sDHzOv5sq96xJY'
    ]

    # sams is list of Doers
    sams = demoing.setupDemoController(secrets=secrets,
                                       name=name,
                                       remotePort=remote,
                                       localPort=local)

    name = "eve"
    remote = 5620
    local = 5621

    # set of secrets (seeds for private keys)
    secrets = [
        'AgjD4nRlycmM5cPcAkfOATAp8wVldRsnc9f1tiwctXlw',
        'AKUotEE0eAheKdDJh9QvNmSEmO_bjIav8V_GmctGpuCQ',
        'AK-nVhMMJciMPvmF5VZE_9H-nhrgng9aJWf7_UHPtRNM',
        'AT2cx-P5YUjIw_SLCHQ0pqoBWGk9s4N1brD-4pD_ANbs',
        'Ap5waegfnuP6ezC18w7jQiPyQwYYsp9Yv9rYMlKAYL8k',
        'Aqlc_FWWrxpxCo7R12uIz_Y2pHUH2prHx1kjghPa8jT8',
        'AagumsL8FeGES7tYcnr_5oN6qcwJzZfLKxoniKUpG4qc',
        'ADW3o9m3udwEf0aoOdZLLJdf1aylokP0lwwI_M2J9h0s'
    ]

    eves = demoing.setupDemoController(secrets=secrets,
                                       name=name,
                                       remotePort=remote,
                                       localPort=local)

    samDoer = doing.DoDoer(doers=sams)
    eveDoer = doing.DoDoer(doers=eves)

    # run components
    # doist = doing.Doist(limit=expire, tock=tock, real=True, doers=[eveDoer, samDoer])
    doist.do(doers=[eveDoer, samDoer])

    help.ogler.resetLevel(level=help.ogler.level)
    """End Test"""
Beispiel #3
0
def test_dodoer_always():
    """
    Test DoDoer class with tryDoer and always
    """
    # create some TryDoers for doers
    doer0 = TryDoer(stop=1)
    doer1 = TryDoer(stop=2)
    doer2 = TryDoer(stop=3)

    doers = [doer0, doer1, doer2]
    tock = 1.0
    dodoer = doing.DoDoer(tock=tock, doers=list(doers))
    assert dodoer.tock == tock == 1.0
    assert dodoer.doers == doers
    for doer in dodoer.doers:
        assert doer.done == None
    assert dodoer.always == False

    limit = 5.0
    doist = doing.Doist(tock=tock, limit=limit, doers=[dodoer])
    assert doist.tock == tock ==  1.0
    assert doist.limit == limit == 5.0
    assert doist.doers == [dodoer]
    assert dodoer.done == None
    assert dodoer.always == False
    assert not dodoer.deeds

    # limit = 5 is long enough that all TryDoers complete
    doist.do()
    assert doist.tyme == 4.0
    assert dodoer.done
    assert dodoer.tyme == doist.tyme
    assert dodoer.always == False
    for doer in dodoer.doers:
        assert doer.done
        assert doer.tyme == dodoer.tyme == doist.tyme
    assert not dodoer.deeds

    # redo but with limit == so not all complete
    doist.do(limit=2)
    assert doist.tyme == 6.0
    assert not dodoer.done
    assert dodoer.always == False
    assert doer0.done
    assert not doer1.done
    assert not doer2.done
    assert not dodoer.deeds

    # redo but with ddoer.always == True but limit enough to complete
    dodoer.always = True
    assert dodoer.always == True
    doist.do(limit=5)
    assert doist.tyme == 11.0
    assert not dodoer.done  # dodoer not done
    assert dodoer.always == True
    for doer in dodoer.doers:   #  but all its doers are done
        assert doer.done
    assert not dodoer.deeds

    # redo but with ddoer.always == True but limit not enought to complete
    assert dodoer.always == True
    doist.do(limit=2)
    assert doist.tyme == 13.0
    assert not dodoer.done  # dodoer not done
    assert dodoer.always == True
    assert doer0.done
    assert not doer1.done
    assert not doer2.done
    assert not dodoer.deeds

    # redo but with ddoer.always == True but manual run doist so does not
    # force complete doers
    assert dodoer.always == True
    assert doist.tyme == 13.0
    deeds = doist.ready(doers=[dodoer])
    doist.once(deeds)
    doist.once(deeds)
    assert doist.tyme == 15.0
    assert not dodoer.done  # dodoer not done
    assert dodoer.always == True
    assert doer0.done
    assert not doer1.done
    assert not doer2.done
    assert len(dodoer.deeds) == 2  # deeds still there

    # now extend deeds
    doer3 = TryDoer(stop=1)
    doer4 = TryDoer(stop=2)
    moredoers =  [doer3, doer4]
    dodoer.extend(doers=list(moredoers))
    assert dodoer.doers == doers + moredoers
    assert len(dodoer.deeds) == 4
    indices = [index for dog, retyme, index in dodoer.deeds]
    assert indices == [1, 2, 3, 4]
    doist.once(deeds)
    doist.once(deeds)
    assert doist.tyme == 17.0
    assert not dodoer.done  # dodoer not done
    assert dodoer.always == True
    assert doer0.done
    assert doer1.done
    assert doer2.done
    assert doer3.done
    assert not doer4.done
    assert len(dodoer.deeds) == 1  # deeds still there
    doist.close(deeds)
    assert dodoer.done == False  # forced close so not done
    assert doer0.done
    assert doer1.done
    assert doer2.done
    assert doer3.done
    assert not doer4.done  # forced close so not done
    assert not deeds


    # start over with full set to test remove
    doer0 = TryDoer(stop=1)
    doer1 = TryDoer(stop=2)
    doer2 = TryDoer(stop=3)
    doer3 = TryDoer(stop=2)
    doer4 = TryDoer(stop=3)
    doers = [doer0, doer1, doer2, doer3, doer4]
    dodoer = doing.DoDoer(tock=tock, doers=list(doers), always=True)
    assert dodoer.tock == tock == 1.0
    assert dodoer.doers ==doers
    for doer in dodoer.doers:
        assert doer.done == None
    assert dodoer.always == True

    limit = 5.0
    doist = doing.Doist(tock=tock, limit=limit, doers=[dodoer])
    assert doist.tock == tock ==  1.0
    assert doist.limit == limit == 5.0
    assert doist.doers == [dodoer]

    assert dodoer.done == None
    assert dodoer.always == True
    assert not dodoer.deeds

    deeds = doist.ready(doers=[dodoer])
    assert not dodoer.done
    doist.once(deeds)
    doist.once(deeds)
    assert doist.tyme == 2.0
    assert not dodoer.done  # dodoer not done
    assert dodoer.always == True
    assert doer0.done
    assert not doer1.done
    assert not doer2.done
    assert not doer3.done
    assert not doer4.done
    assert len(dodoer.deeds) == 4  # deeds still there
    dodoer.remove(doers=[doer0, doer1, doer3])
    assert dodoer.doers == [doer2, doer4]
    assert len(dodoer.deeds) == 2


    """End Test """
Beispiel #4
0
def test_dodoer():
    """
    Test DoDoer class
    """
    tock = 1.0
    dodoer = doing.DoDoer()
    assert dodoer.tock == 0.0

    tymist = tyming.Tymist()
    dodoer = doing.DoDoer(tymth=tymist.tymen(), tock=tock)
    assert dodoer.tock == tock == 1.0
    dodoer.tock = 0.0
    assert dodoer.tock ==  0.0

    # create generator use send and run until normal exit. emulates Doist.ready
    args = {}
    dog = dodoer(tymth=dodoer.tymth, tock=dodoer.tock, **args)
    assert inspect.isgenerator(dog)
    assert dodoer.doers == []

    result = dog.send(None)
    assert result == dodoer.tock == 0.0
    assert dodoer.doers == []

    tymist.tick()  # empty doers does list so ends right away
    with pytest.raises(StopIteration):
        try:
            result = dog.send(tymist.tyme)
        except StopIteration as ex:
            assert ex.value == True
            raise

    tymist.tick()
    with pytest.raises(StopIteration):  # send after break
        try:
            result = dog.send(tymist.tyme)
        except StopIteration as ex:
            assert ex.value == None
            raise

    # create some doers
    doer0 = doing.Doer()
    doer1 = doing.Doer()
    doer2 = doing.Doer()

    doers = [doer0, doer1, doer2]

    # create generator use send and then explicit close. emulates Doist.ready
    args = {}
    dog = dodoer(tymth=dodoer.tymth, tock=dodoer.tock, doers=doers, **args)
    assert inspect.isgenerator(dog)
    assert dodoer.doers == []

    result = dog.send(None)
    assert result == dodoer.tock == 0.0
    assert dodoer.doers == doers

    tymist.tick()
    result = dog.send(tymist.tyme)
    assert result == dodoer.tock == 0.0

    tymist.tick()
    result = dog.send(tymist.tyme)
    assert result == dodoer.tock == 0.0

    result = dog.close()
    assert result == None  # no yielded value on close

    tymist.tick()
    with pytest.raises(StopIteration):  # send after close
        try:
            result = dog.send(tymist.tyme)
        except StopIteration as ex:
            assert ex.value == None
            raise

    """End Test """
Beispiel #5
0
def test_doize_dodoer_with_bound_method():
    """
    Test decorator @doize with bound method returning generator
    """
    # run until complete normal exit so done==True
    class A():
        def __init__(self):
            self.x = 1

        @doing.doize(tock=0.25)
        def myDo(self, tymth=None, tock=0.0, **opts):
            while self.x <= 3:
                tyme = yield(tock)
                self.x += 1

            return True

    a = A()
    assert a.x == 1

    assert inspect.ismethod(a.myDo)
    assert inspect.isgeneratorfunction(a.myDo)
    # read of bound method attribute is allowed
    assert a.myDo.__func__.tock == a.myDo.tock == 0.25
    assert a.myDo.__func__.done == a.myDo.done == None
    assert a.myDo.__func__.opts == a.myDo.opts == dict()

    with pytest.raises(AttributeError):
        a.myDo.tock = 0.2  # can't write to bound method attribute

    a.myDo.__func__.tock = 0.2  # can write to bound method.__func__ attribute
    assert a.myDo.tock == 0.2

    doist = doing.Doist(limit=1.0)

    myGen = a.myDo(tymth=doist.tymen(), tock=a.myDo.tock, **a.myDo.opts)
    assert inspect.isgenerator(myGen)

    doist.do(doers=[a.myDo])
    assert a.myDo.done
    assert a.x == 4

    a.x =  1
    assert a.x == 1
    doist.tyme = 0.0

    dodoer = doing.DoDoer(doers=[a.myDo])

    doist.do(doers=[dodoer])
    assert a.myDo.done
    assert a.x == 4

    # run forever so forced complete done == False
    class B():
        def __init__(self):
            self.x = 1

        @doing.doize(tock=0.25)
        def myDo(self, tymth=None, tock=0.0, **opts):
            while True:
                tyme = yield(tock)
                self.x += 1
            return True

    b = B()
    assert b.x == 1

    assert inspect.ismethod(b.myDo)
    assert inspect.isgeneratorfunction(b.myDo)
    # read of bound method attribute is allowed
    assert b.myDo.__func__.tock == b.myDo.tock == 0.25
    assert b.myDo.__func__.done == b.myDo.done == None
    assert b.myDo.__func__.opts == b.myDo.opts == dict()

    with pytest.raises(AttributeError):
        b.myDo.tock = 0.2  # can't write to bound method attribute

    b.myDo.__func__.tock = 0.2  # can write to bound method.__func__ attribute
    assert b.myDo.tock == 0.2

    doist = doing.Doist(limit=1.0)

    myGen = b.myDo(tymth=doist.tymen(), tock=b.myDo.tock, **b.myDo.opts)
    assert inspect.isgenerator(myGen)

    doist.do(doers=[b.myDo])
    assert b.myDo.done == False
    assert b.x == 6

    b.x =  1
    assert b.x == 1
    doist.tyme = 0.0

    dodoer = doing.DoDoer(doers=[b.myDo])

    doist.do(doers=[dodoer])
    assert b.myDo.done == False
    assert b.x == 6

    """End Test"""
Beispiel #6
0
def test_dodoer_remove():
    """
    Test .remove method of DoDoer
    """
    tock = 1.0
    # start over with full set to test remove
    doer0 = TryDoer(stop=1)
    doer1 = TryDoer(stop=2)
    doer2 = TryDoer(stop=3)
    doer3 = TryDoer(stop=2)
    doer4 = TryDoer(stop=3)
    doers = [doer0, doer1, doer2, doer3, doer4]
    dodoer = doing.DoDoer(tock=tock, doers=list(doers), always=True)
    assert dodoer.tock == tock == 1.0
    assert dodoer.doers == doers
    for doer in dodoer.doers:
        assert doer.done == None
    assert dodoer.always == True

    limit = 5.0
    doist = doing.Doist(tock=tock, limit=limit, doers=[dodoer])
    assert doist.tyme == 0.0
    assert doist.tock == tock == 1.0
    assert doist.limit == limit == 5.0
    assert doist.doers == [dodoer]

    assert dodoer.done == None
    assert dodoer.always == True
    assert not dodoer.deeds

    doist.enter()
    assert not dodoer.done
    doist.recur()
    doist.recur()
    assert doist.tyme == 2.0
    assert not dodoer.done  # dodoer not done
    assert dodoer.always == True
    assert doer0.done
    assert not doer1.done
    assert not doer2.done
    assert not doer3.done
    assert not doer4.done
    assert len(dodoer.deeds) == 4  # other deeds still there
    doers = [doer for dog, retyme, doer in dodoer.deeds]
    assert doers == [doer1, doer2, doer3, doer4]  # doer0 is removed
    for dog, retyme, doer in dodoer.deeds:
        assert not doer.done

    dodoer.remove(doers=[doer0, doer1, doer3])
    assert dodoer.doers == [doer2, doer4]
    assert len(dodoer.deeds) == 2
    doers = [doer for dog, retyme, doer in dodoer.deeds]
    assert doers == [doer2, doer4]  # others are removed
    for dog, retyme, doer in dodoer.deeds:
        assert not doer.done
    assert not doer1.done  # forced exit
    assert not doer3.done  # forced exit

    doist.recur()
    doist.recur()
    assert doist.tyme == 4.0
    assert doist.done == None  # never called .do
    assert dodoer.done == True  # all its doers completed
    assert len(dodoer.deeds) == 0  # all done
    assert len(dodoer.doers) == 2  # not removed but completed
    for doer in dodoer.doers:
        assert doer.done
    assert doer0.done  # already clean done before remove
    assert not doer1.done  # forced exit upon remove before done
    assert doer2.done  # clean done
    assert not doer3.done  # forced exit upon remove before done
    assert doer4.done  # clean done
    doist.recur()
    doist.recur(
    )  # does not complete because dodoer not done its always == True
    """Done Test"""
Beispiel #7
0
def test_dodoer_always():
    """
    Test DoDoer class with tryDoer and always
    """
    tock = 1.0
    # create some TryDoers for doers
    doer0 = TryDoer(stop=1)
    doer1 = TryDoer(stop=2)
    doer2 = TryDoer(stop=3)

    doers = [doer0, doer1, doer2]
    dodoer = doing.DoDoer(tock=tock, doers=list(doers))  # make copy
    assert dodoer.tock == tock == 1.0
    assert dodoer.doers == doers
    for doer in dodoer.doers:
        assert doer.done == None
    assert dodoer.always == False

    limit = 5.0
    doist = doing.Doist(tock=tock, limit=limit, doers=[dodoer])
    assert doist.tock == tock == 1.0
    assert doist.limit == limit == 5.0
    assert doist.doers == [dodoer]
    assert dodoer.done == None
    assert dodoer.always == False
    assert not dodoer.deeds

    # limit = 5 is long enough that all TryDoers complete
    doist.do()
    assert doist.tyme == 4.0
    assert dodoer.done
    assert dodoer.tyme == doist.tyme
    assert dodoer.always == False
    for doer in dodoer.doers:
        assert doer.done
        assert doer.tyme == dodoer.tyme == doist.tyme
    assert not dodoer.deeds

    # redo but with limit == so not all complete
    doist.do(limit=2)
    assert doist.tyme == 6.0
    assert not dodoer.done
    assert dodoer.always == False
    assert doer0.done
    assert not doer1.done
    assert not doer2.done
    assert not dodoer.deeds

    # redo but with ddoer.always == True but limit enough to complete
    dodoer.always = True
    assert dodoer.always == True
    doist.do(limit=5)
    assert doist.tyme == 11.0
    assert not dodoer.done  # dodoer not done
    assert dodoer.always == True
    for doer in dodoer.doers:  #  but all its doers are done
        assert doer.done
    assert not dodoer.deeds

    # redo but with ddoer.always == True but limit not enought to complete
    assert dodoer.always == True
    doist.do(limit=2)
    assert doist.tyme == 13.0
    assert not dodoer.done  # dodoer not done
    assert dodoer.always == True
    assert doer0.done
    assert not doer1.done
    assert not doer2.done
    assert not dodoer.deeds

    # redo but with ddoer.always == True but manual run doist so does not
    # force complete doers
    assert dodoer.always == True
    assert doist.tyme == 13.0
    doist.enter()
    doist.recur()
    doist.recur()
    assert doist.tyme == 15.0
    assert not dodoer.done  # dodoer not done
    assert dodoer.always == True
    assert doer0.done
    assert not doer1.done
    assert not doer2.done
    assert len(dodoer.deeds) == 2  # deeds still there

    # Test extend
    # now extend Doers
    doer3 = TryDoer(stop=1)
    doer4 = TryDoer(stop=2)
    moredoers = [doer3, doer4]
    dodoer.extend(doers=list(moredoers))
    assert dodoer.doers == doers + moredoers
    assert len(dodoer.deeds) == 4
    doers = [doer for dog, retyme, doer in dodoer.deeds]
    assert doers == [doer1, doer2, doer3, doer4]
    doist.recur()
    doist.recur()
    assert doist.tyme == 17.0
    assert not dodoer.done  # dodoer not done
    assert dodoer.always == True
    assert doer0.done
    assert doer1.done
    assert doer2.done
    assert doer3.done
    assert not doer4.done
    assert len(dodoer.deeds) == 1  # deeds still there
    doist.exit()
    assert dodoer.done == False  # forced close so not done
    assert doer0.done
    assert doer1.done
    assert doer2.done
    assert doer3.done
    assert not doer4.done  # forced close so not done
    assert not doist.deeds
    """End Test """