Esempio n. 1
0
    def test_scheduling_cleanup(self):
        rlist = []
        def f():
            rlist.append('fb')
            core.schedule()
            rlist.append('fa')

        def g():
            rlist.append('gb')
            core.schedule()
            rlist.append('ga')

        def h():
            rlist.append('hb')
            core.schedule()
            rlist.append('ha')

        tf = core.tasklet(f)()
        tg = core.tasklet(g)()
        th = core.tasklet(h)()

        rlist.append('mb')
        core.run()
        rlist.append('ma')

        assert rlist == 'mb fb gb hb fa ga ha ma'.split()
Esempio n. 2
0
    def test_multiple_producers(self):
        messages = []
        sources = []

        def f():
            while True:
                source, msg = receive()
                if not msg:
                    break
                if source.ref not in sources:
                    sources.append(source.ref)
                messages.append(msg)

        def f1(ref):
            msg = ['hello', 'world']
            for s in msg:
                send(ref, s)

        def f2(ref):
            msg = ['brave', 'new', 'world', '']
            for s in msg:
                send(ref, s)

        pid0 = spawn(f)
        pid1 = spawn(f1, pid0)
        pid2 = spawn(f2, pid0)

        core.run()

        assert len(messages) == 5
        assert sources == [5, 6]
Esempio n. 3
0
    def test_schedule_callback(self):
        res = []
        cb = []

        def schedule_cb(prev, next):
            cb.append((prev, next))

        core.set_schedule_callback(schedule_cb)

        def f(i):
            res.append('A_%s' % i)
            core.schedule()
            res.append('B_%s' % i)

        t1 = core.tasklet(f)(1)
        t2 = core.tasklet(f)(2)
        maintask = core.getmain()
        core.run()
        assert res == ['A_1', 'A_2', 'B_1', 'B_2']
        assert len(cb) == 5
        assert cb[0] == (maintask, t1)
        assert cb[1] == (t1, t2)
        assert cb[2] == (t2, t1)
        assert cb[3] == (t1, t2)
        assert cb[4] == (t2, maintask)
Esempio n. 4
0
    def test_scheduling_cleanup(self):
        rlist = []

        def f():
            rlist.append('fb')
            core.schedule()
            rlist.append('fa')

        def g():
            rlist.append('gb')
            core.schedule()
            rlist.append('ga')

        def h():
            rlist.append('hb')
            core.schedule()
            rlist.append('ha')

        tf = core.tasklet(f)()
        tg = core.tasklet(g)()
        th = core.tasklet(h)()

        rlist.append('mb')
        core.run()
        rlist.append('ma')

        assert rlist == 'mb fb gb hb fa ga ha ma'.split()
Esempio n. 5
0
    def test_async_with_blocking_channel(self):

        c = core.channel(10)

        unblocked_sent = 0
        for i in range(10):
            c.send(True)
            unblocked_sent += 1

        assert unblocked_sent == 10
        assert c.balance == 10

        r_list = []
        def f():
            start = time.time()
            c.send(True)
            r_list.append(start)

        core.tasklet(f)()


        unblocked_recv = []
        for i in range(11):
            time.sleep(0.01)
            unblocked_recv.append(c.receive())
            core.schedule()


        core.run()

        diff = time.time() - r_list[0]

        assert len(unblocked_recv) == 11
        assert diff > 0.1
Esempio n. 6
0
    def test_except(self):
        rlist = []

        def f():
            rlist.append('f')
            return 1 / 0

        def g():
            rlist.append('bg')
            core.schedule()
            rlist.append('ag')

        def h():
            rlist.append('bh')
            core.schedule()
            rlist.append('ah')

        tg = core.tasklet(g)()
        tf = core.tasklet(f)()
        th = core.tasklet(h)()

        try:
            core.run()
            # cheating, can't test for ZeroDivisionError
        except ZeroDivisionError:
            rlist.append('E')
        core.schedule()
        core.schedule()

        assert rlist == "bg f E bh ag ah".split()
Esempio n. 7
0
    def test_simple_channel(self):
        output = []
        def print_(*args):
            output.append(args)

        def Sending(channel):
            print_("sending")
            channel.send("foo")

        def Receiving(channel):
            print_("receiving")
            print_(channel.receive())

        ch=core.channel()

        task=core.tasklet(Sending)(ch)

        # Note: the argument, schedule is taking is the value,
        # schedule returns, not the task that runs next

        #core.schedule(task)
        core.schedule()
        task2=core.tasklet(Receiving)(ch)
        #core.schedule(task2)
        core.schedule()

        core.run()

        assert output == [('sending',), ('receiving',), ('foo',)]
Esempio n. 8
0
    def test_mailbox(self):
        messages = []
        sources = []

        def f():
            while True:
                source, msg = receive()
                if not msg:
                    break
                if source.ref not in sources:
                    sources.append(source.ref)
                messages.append(msg)

        def f1(ref):
            msg = ['hello', ' ', 'world']
            for s in msg:
                send(ref, s)

        pid0 = spawn(f)
        pid1 = spawn(f1, pid0)

        core.run()

        assert messages == ['hello', ' ', 'world']
        assert sources == [3]
Esempio n. 9
0
    def test_simple_channel(self):
        output = []
        def print_(*args):
            output.append(args)

        def Sending(channel):
            print_("sending")
            channel.send("foo")

        def Receiving(channel):
            print_("receiving")
            print_(channel.receive())

        ch=core.channel()

        task=core.tasklet(Sending)(ch)

        # Note: the argument, schedule is taking is the value,
        # schedule returns, not the task that runs next

        #core.schedule(task)
        core.schedule()
        task2=core.tasklet(Receiving)(ch)
        #core.schedule(task2)
        core.schedule()

        core.run()

        assert output == [('sending',), ('receiving',), ('foo',)]
Esempio n. 10
0
    def test_except_full(self):
        rlist = []
        def f():
            rlist.append('f')
            return 1/0

        def g():
            rlist.append('bg')
            core.schedule()
            rlist.append('ag')

        def h():
            rlist.append('bh')
            core.schedule()
            rlist.append('ah')

        tg = core.tasklet(g)()
        tf = core.tasklet(f)()
        th = core.tasklet(h)()

        try:
            core.run()
        except ZeroDivisionError:
            rlist.append('E')
        core.schedule()
        core.schedule()

        assert rlist == "bg f E bh ag ah".split()
Esempio n. 11
0
    def test_multiple_producers(self):
        messages = []
        sources = []
        def f():
            while True:
                source, msg = receive()
                if not msg:
                    break
                if source.ref not in sources:
                    sources.append(source.ref)
                messages.append(msg)

        def f1(ref):
            msg = ['hello', 'world']
            for s in msg:
                send(ref, s)

        def f2(ref):
            msg = ['brave', 'new', 'world', '']
            for s in msg:
                send(ref, s)

        pid0 = spawn(f)
        pid1 = spawn(f1, pid0)
        pid2 = spawn(f2, pid0)

        core.run()

        assert len(messages) == 5
        assert sources == [5, 6]
Esempio n. 12
0
    def test_schedule_return_value(self):
        def task(val):
            value = core.schedule(val)
            assert value == val

        core.tasklet(task)(10)
        core.tasklet(task)(5)

        core.run()
Esempio n. 13
0
    def test_balance_recv(self):
        def Receiving(channel):
            channel.receive()

        ch=core.channel()

        task=core.tasklet(Receiving)(ch)
        core.run()

        assert ch.balance == -1
Esempio n. 14
0
    def test_balance_send(self):
        def Sending(channel):
            channel.send("foo")

        ch=core.channel()

        task=core.tasklet(Sending)(ch)
        core.run()

        assert ch.balance == 1
Esempio n. 15
0
    def test_balance_send(self):
        def Sending(channel):
            channel.send("foo")

        ch=core.channel()

        task=core.tasklet(Sending)(ch)
        core.run()

        assert ch.balance == 1
Esempio n. 16
0
    def test_balance_recv(self):
        def Receiving(channel):
            channel.receive()

        ch=core.channel()

        task=core.tasklet(Receiving)(ch)
        core.run()

        assert ch.balance == -1
Esempio n. 17
0
    def test_simple_pipe(self):
        def pipe(X_in, X_out):
            foo = X_in.receive()
            X_out.send(foo)

        X, Y = core.channel(), core.channel()
        t = core.tasklet(pipe)(X, Y)
        core.run()
        X.send(42)
        assert Y.receive() == 42
Esempio n. 18
0
    def test_schedule_return_value(self):

        def task(val):
            value = core.schedule(val)
            assert value == val

        core.tasklet(task)(10)
        core.tasklet(task)(5)

        core.run()
Esempio n. 19
0
    def test_simple_pipe(self):
        def pipe(X_in, X_out):
            foo = X_in.receive()
            X_out.send(foo)

        X, Y = core.channel(), core.channel()
        t = core.tasklet(pipe)(X, Y)
        core.run()
        X.send(42)
        assert Y.receive() == 42
Esempio n. 20
0
def test_simple_timer():
    r_list = []
    def _func(now, t):
        r_list.append(from_nanotime(now))

    now = time.time()
    t = Timer(_func, 0.1)
    t.start()
    run()
    delay = r_list[0]
    assert (now + 0.09) <= delay <= (now + 0.11), delay
Esempio n. 21
0
def test_simple_timer():
    r_list = []

    def _func(now, t):
        r_list.append(from_nanotime(now))

    now = time.time()
    t = Timer(_func, 0.1)
    t.start()
    run()
    delay = r_list[0]
    assert (now + 0.09) <= delay <= (now + 0.11), delay
Esempio n. 22
0
    def test_getruncount(self):
        assert core.getruncount() == 1
        def with_schedule():
            assert core.getruncount() == 2

        t1 = core.tasklet(with_schedule)()
        assert core.getruncount() == 2
        core.schedule()
        def with_run():
            assert core.getruncount() == 1

        t2 = core.tasklet(with_run)()
        core.run()
Esempio n. 23
0
    def test_spawn_after(self):
        r_list = []
        def f():
            r_list.append(time.time())

        start = time.time()
        spawn_after(0.3, f)

        core.run()

        end = r_list[0]
        diff = end - start
        assert 0.29 <= diff <= 0.31
Esempio n. 24
0
    def test_run(self):
        output = []
        def print_(*args):
            output.append(args)

        def f(i):
            print_(i)

        core.tasklet(f)(1)
        core.tasklet(f)(2)
        core.run()

        assert output == [(1,), (2,)]
Esempio n. 25
0
    def test_getruncount(self):
        assert core.getruncount() == 1
        def with_schedule():
            assert core.getruncount() == 2

        t1 = core.tasklet(with_schedule)()
        assert core.getruncount() == 2
        core.schedule()
        def with_run():
            assert core.getruncount() == 1

        t2 = core.tasklet(with_run)()
        core.run()
Esempio n. 26
0
    def test_spawn_after(self):
        r_list = []

        def f():
            r_list.append(time.time())

        start = time.time()
        spawn_after(0.3, f)

        core.run()

        end = r_list[0]
        diff = end - start
        assert 0.29 <= diff <= 0.31
Esempio n. 27
0
def test_repeat():
    r = []
    def f(now, t):
        if len(r) == 3:
            t.stop()
            return
        r.append(now)

    t = Timer(f, 0.01, 0.01)
    t.start()
    run()
    assert len(r) == 3
    assert r[2] > r[1]
    assert r[1] > r[0]
Esempio n. 28
0
    def test_run(self):
        output = []

        def print_(*args):
            output.append(args)

        def f(i):
            print_(i)

        core.tasklet(f)(1)
        core.tasklet(f)(2)
        core.run()

        assert output == [(1, ), (2, )]
Esempio n. 29
0
    def test_sleep2(self):
        rlist = []

        def f():
            sleep()
            rlist.append('a')

        def f1():
            rlist.append('b')

        core.tasklet(f)()
        core.tasklet(f1)()
        core.run()

        assert rlist == ['b', 'a']
Esempio n. 30
0
    def test_simple(self):
        def f(): return

        pid = spawn(f)
        register("test", pid)

        assert pid in registry
        assert "test" in registry
        assert registry["test"] ==  pid
        assert registry[pid] == ["test"]

        del registry[pid]
        assert registry["test"] is None

        core.run()
Esempio n. 31
0
def test_repeat():
    r = []

    def f(now, t):
        if len(r) == 3:
            t.stop()
            return
        r.append(now)

    t = Timer(f, 0.01, 0.01)
    t.start()
    run()
    assert len(r) == 3
    assert r[2] > r[1]
    assert r[1] > r[0]
Esempio n. 32
0
    def test_send_exception(self):
        def exp_sender(chan):
            chan.send_exception(Exception, 'test')

        def exp_recv(chan):
            try:
                val = chan.receive()
            except Exception as exp:
                assert exp.__class__ is Exception
                assert str(exp) == 'test'

        chan = core.channel()
        t1 = core.tasklet(exp_recv)(chan)
        t2 = core.tasklet(exp_sender)(chan)
        core.run()
Esempio n. 33
0
    def test_send_after(self):
        r_list = []
        def f():
            receive()
            r_list.append(time.time())

        ref = spawn(f)
        start = time.time()
        send_after(0.3, ref, None)

        core.run()

        end = r_list[0]
        diff = end - start
        assert 0.29 <= diff <= 0.31
Esempio n. 34
0
    def test_send_exception(self):
        def exp_sender(chan):
            chan.send_exception(Exception, 'test')

        def exp_recv(chan):
            try:
                val = chan.receive()
            except Exception as exp:
                assert exp.__class__ is Exception
                assert str(exp) == 'test'

        chan = core.channel()
        t1 = core.tasklet(exp_recv)(chan)
        t2 = core.tasklet(exp_sender)(chan)
        core.run()
Esempio n. 35
0
    def test_sleep2(self):
        rlist = []

        def f():
            sleep()
            rlist.append('a')

        def f1():
            rlist.append('b')

        core.tasklet(f)()
        core.tasklet(f1)()
        core.run()

        assert rlist == ['b', 'a']
Esempio n. 36
0
    def test_send_after(self):
        r_list = []

        def f():
            receive()
            r_list.append(time.time())

        ref = spawn(f)
        start = time.time()
        send_after(0.3, ref, None)

        core.run()

        end = r_list[0]
        diff = end - start
        assert 0.29 <= diff <= 0.31
Esempio n. 37
0
    def test_simple(self):
        def f():
            return

        pid = spawn(f)
        register("test", pid)

        assert pid in registry
        assert "test" in registry
        assert registry["test"] == pid
        assert registry[pid] == ["test"]

        del registry[pid]
        assert registry["test"] is None

        core.run()
Esempio n. 38
0
    def test_simple(self):
        r_list = []
        def f():
            r_list.append(True)

        pid = spawn(f)
        assert isinstance(pid, ActorRef)
        assert pid.ref == 0
        assert hasattr(pid.actor, 'mailbox')

        sleep(0.1)
        core.run()

        assert r_list == [True]
        assert pid.actor is None
        assert pid.is_alive is False
Esempio n. 39
0
    def test_ticker(self):
        rlist = []

        def f():
            ticker = Ticker(0.1)
            i = 0
            while True:
                if i == 3: break
                t = ticker.receive()
                rlist.append(t)
                i += 1
            ticker.stop()

        tf = core.tasklet(f)()
        core.run()

        assert len(rlist) == 3
Esempio n. 40
0
    def test_simple(self):
        r_list = []

        def f():
            r_list.append(True)

        pid = spawn(f)
        assert isinstance(pid, ActorRef)
        assert pid.ref == 0
        assert hasattr(pid.actor, 'mailbox')

        sleep(0.1)
        core.run()

        assert r_list == [True]
        assert pid.actor is None
        assert pid.is_alive is False
Esempio n. 41
0
    def test_ticker(self):
        rlist = []

        def f():
            ticker = Ticker(0.1)
            i = 0
            while True:
                if i == 3: break
                t = ticker.receive()
                rlist.append(t)
                i += 1
            ticker.stop()

        tf = core.tasklet(f)()
        core.run()

        assert len(rlist) == 3
Esempio n. 42
0
    def test_cooperative(self):
        output = []
        def print_(*args):
            output.append(args)

        def Loop(i):
            for x in range(3):
                core.schedule()
                print_("schedule", i)

        core.tasklet(Loop)(1)
        core.tasklet(Loop)(2)
        core.run()

        assert output == [('schedule', 1), ('schedule', 2),
                          ('schedule', 1), ('schedule', 2),
                          ('schedule', 1), ('schedule', 2),]
Esempio n. 43
0
    def test_readable(self):
        (r, w) = os.pipe()

        ret = []

        def _read(fd):
            c = IOChannel(r, mode=0)
            c.receive()
            ret.append(os.read(fd, 10))
            c.stop()

        def _write(fd):
            os.write(fd, b"TEST")

        core.tasklet(_read)(r)
        core.tasklet(_write)(w)
        core.run()

        assert ret == [b"TEST"]
Esempio n. 44
0
def test_multiple_sleep():
    r1 = []
    def f():
        sleep(0.4)
        r1.append(time.time())

    r2 = []
    def f1():
        sleep(0.1)
        r2.append(time.time())

    tasklet(f)()
    tasklet(f1)()

    now = time.time()
    run()
    assert r1[0] > r2[0]
    assert (now + 0.39) <= r1[0] <= (now + 0.41), r1[0]
    assert (now + 0.09) <= r2[0] <= (now + 0.11), r2[0]
Esempio n. 45
0
    def test_wrap(self):
        r_list = []
        def f():
            r_list.append(True)

        t = core.tasklet(f)()
        assert not hasattr(t, 'mailbox')
        wrap(t)
        assert isinstance(t, Actor)
        assert hasattr(t, 'mailbox')
        assert hasattr(t, 'ref')

        pid = t.ref
        assert isinstance(pid, ActorRef)
        assert pid.ref == 1

        core.run()
        assert r_list == [True]
        assert pid.actor is None
        assert pid.is_alive is False
Esempio n. 46
0
    def test_wrap(self):
        r_list = []

        def f():
            r_list.append(True)

        t = core.tasklet(f)()
        assert not hasattr(t, 'mailbox')
        wrap(t)
        assert isinstance(t, Actor)
        assert hasattr(t, 'mailbox')
        assert hasattr(t, 'ref')

        pid = t.ref
        assert isinstance(pid, ActorRef)
        assert pid.ref == 1

        core.run()
        assert r_list == [True]
        assert pid.actor is None
        assert pid.is_alive is False
Esempio n. 47
0
def test_multiple_timer():
    r1 = []
    def f(now, t):
        r1.append(from_nanotime(now))

    r2 = []
    def f1(now, t):
        r2.append(from_nanotime(now))

    now = time.time()

    t = Timer(f, 0.4)
    t.start()

    t1 = Timer(f1, 0.1)
    t1.start()

    run()
    assert r1[0] > r2[0]
    assert (now + 0.39) <= r1[0] <= (now + 0.41), r1[0]
    assert (now + 0.09) <= r2[0] <= (now + 0.11), r2[0]
Esempio n. 48
0
def test_multiple_sleep():
    r1 = []

    def f():
        sleep(0.4)
        r1.append(time.time())

    r2 = []

    def f1():
        sleep(0.1)
        r2.append(time.time())

    tasklet(f)()
    tasklet(f1)()

    now = time.time()
    run()
    assert r1[0] > r2[0]
    assert (now + 0.39) <= r1[0] <= (now + 0.41), r1[0]
    assert (now + 0.09) <= r2[0] <= (now + 0.11), r2[0]
Esempio n. 49
0
    def test_task_with_channel(self):
        pref = {}
        pref[-1] = ['s0', 'r0', 's1', 'r1', 's2', 'r2',
                    's3', 'r3', 's4', 'r4', 's5', 'r5',
                    's6', 'r6', 's7', 'r7', 's8', 'r8',
                    's9', 'r9']
        pref[0] =  ['s0', 'r0', 's1', 's2', 'r1', 'r2',
                    's3', 's4', 'r3', 'r4', 's5', 's6',
                    'r5', 'r6', 's7', 's8', 'r7', 'r8',
                    's9', 'r9']
        pref[1] =  ['s0', 's1', 'r0', 's2', 'r1', 's3',
                    'r2', 's4', 'r3', 's5', 'r4', 's6',
                    'r5', 's7', 'r6', 's8', 'r7', 's9',
                    'r8', 'r9']
        rlist = []

        def f(outchan):
            for i in range(10):
                rlist.append('s%s' % i)
                outchan.send(i)
            outchan.send(-1)

        def g(inchan):
            while 1:
                val = inchan.receive()
                if val == -1:
                    break
                rlist.append('r%s' % val)

        for preference in [-1, 0, 1]:
            rlist = []
            ch = core.channel()
            ch.preference = preference
            t1 = core.tasklet(f)(ch)
            t2 = core.tasklet(g)(ch)

            core.run()

            assert len(rlist) == 20
            assert rlist == pref[preference]
Esempio n. 50
0
    def test_with_channel(self):
        pref = {}
        pref[-1] = ['s0', 'r0', 's1', 'r1', 's2', 'r2',
                    's3', 'r3', 's4', 'r4', 's5', 'r5',
                    's6', 'r6', 's7', 'r7', 's8', 'r8',
                    's9', 'r9']
        pref[0] =  ['s0', 'r0', 's1', 's2', 'r1', 'r2',
                    's3', 's4', 'r3', 'r4', 's5', 's6',
                    'r5', 'r6', 's7', 's8', 'r7', 'r8',
                    's9', 'r9']
        pref[1] =  ['s0', 's1', 'r0', 's2', 'r1', 's3',
                    'r2', 's4', 'r3', 's5', 'r4', 's6',
                    'r5', 's7', 'r6', 's8', 'r7', 's9',
                    'r8', 'r9']
        rlist = []

        def f(outchan):
            for i in range(10):
                rlist.append('s%s' % i)
                outchan.send(i)
            outchan.send(-1)

        def g(inchan):
            while 1:
                val = inchan.receive()
                if val == -1:
                    break
                rlist.append('r%s' % val)

        for preference in [-1, 0, 1]:
            rlist = []
            ch = core.channel()
            ch.preference = preference
            t1 = core.tasklet(f)(ch)
            t2 = core.tasklet(g)(ch)

            core.run()

            assert len(rlist) == 20
            assert rlist == pref[preference]
Esempio n. 51
0
    def test_construction(self):
        output = []

        def print_(*args):
            output.append(args)

        def aCallable(value):
            print_("aCallable:", value)

        task = core.tasklet(aCallable)
        task.setup('Inline using setup')

        core.run()
        assert output == [("aCallable:", 'Inline using setup')]

        del output[:]
        task = core.tasklet(aCallable)
        task('Inline using ()')

        core.run()
        assert output == [("aCallable:", 'Inline using ()')]

        del output[:]
        task = core.tasklet()
        task.bind(aCallable)
        task('Bind using ()')

        core.run()
        assert output == [("aCallable:", 'Bind using ()')]
Esempio n. 52
0
    def test_construction(self):
        output = []
        def print_(*args):
            output.append(args)

        def aCallable(value):
            print_("aCallable:", value)

        task = core.tasklet(aCallable)
        task.setup('Inline using setup')

        core.run()
        assert output == [("aCallable:", 'Inline using setup')]


        del output[:]
        task = core.tasklet(aCallable)
        task('Inline using ()')

        core.run()
        assert output == [("aCallable:", 'Inline using ()')]

        del output[:]
        task = core.tasklet()
        task.bind(aCallable)
        task('Bind using ()')

        core.run()
        assert output == [("aCallable:", 'Bind using ()')]
Esempio n. 53
0
    def test_send_counter(self):
        import random

        numbers = list(range(20))
        random.shuffle(numbers)

        def counter(n, ch):
            for i in xrange(n):
                core.schedule()
            ch.send(n)

        ch = core.channel()
        for each in numbers:
            core.tasklet(counter)(each, ch)

        core.run()

        rlist = []
        while ch.balance:
            rlist.append(ch.receive())

        numbers.sort()
        assert rlist == numbers
Esempio n. 54
0
def test_multiple_timer():
    r1 = []

    def f(now, t):
        r1.append(from_nanotime(now))

    r2 = []

    def f1(now, t):
        r2.append(from_nanotime(now))

    now = time.time()

    t = Timer(f, 0.4)
    t.start()

    t1 = Timer(f1, 0.1)
    t1.start()

    run()
    assert r1[0] > r2[0]
    assert (now + 0.39) <= r1[0] <= (now + 0.41), r1[0]
    assert (now + 0.09) <= r2[0] <= (now + 0.11), r2[0]
Esempio n. 55
0
    def test_schedule_callback(self):
        res = []
        cb = []
        def schedule_cb(prev, next):
            cb.append((prev, next))

        core.set_schedule_callback(schedule_cb)
        def f(i):
            res.append('A_%s' % i)
            core.schedule()
            res.append('B_%s' % i)

        t1 = core.tasklet(f)(1)
        t2 = core.tasklet(f)(2)
        maintask = core.getmain()
        core.run()
        assert res == ['A_1', 'A_2', 'B_1', 'B_2']
        assert len(cb) == 5
        assert cb[0] == (maintask, t1)
        assert cb[1] == (t1, t2)
        assert cb[2] == (t2, t1)
        assert cb[3] == (t1, t2)
        assert cb[4] == (t2, maintask)
Esempio n. 56
0
    def test_send_counter(self):
        import random

        numbers = list(range(20))
        random.shuffle(numbers)

        def counter(n, ch):
            for i in xrange(n):
                core.schedule()
            ch.send(n)

        ch = core.channel()
        for each in numbers:
            core.tasklet(counter)(each, ch)

        core.run()

        rlist = []
        while ch.balance:
            rlist.append(ch.receive())

        numbers.sort()
        assert rlist == numbers
Esempio n. 57
0
    def test_cooperative(self):
        output = []

        def print_(*args):
            output.append(args)

        def Loop(i):
            for x in range(3):
                core.schedule()
                print_("schedule", i)

        core.tasklet(Loop)(1)
        core.tasklet(Loop)(2)
        core.run()

        assert output == [
            ('schedule', 1),
            ('schedule', 2),
            ('schedule', 1),
            ('schedule', 2),
            ('schedule', 1),
            ('schedule', 2),
        ]
Esempio n. 58
0
    def test_mailbox(self):
        messages = []
        sources = []
        def f():
            while True:
                source, msg = receive()
                if not msg:
                    break
                if source.ref not in sources:
                    sources.append(source.ref)
                messages.append(msg)

        def f1(ref):
            msg = ['hello', ' ', 'world']
            for s in msg:
                send(ref, s)

        pid0 = spawn(f)
        pid1 = spawn(f1, pid0)

        core.run()

        assert messages == ['hello', ' ', 'world']
        assert sources == [3]