Beispiel #1
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',)]
Beispiel #2
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()
Beispiel #3
0
    def on_recv(self, handler, addr, data, error):
        with self._lock:
            if addr in self.conns:
                conn = self.conns[addr]

                if error:
                    if error == 1:
                        msg = ""
                    else:
                        msg = bomb(IOError, IOError("uv error: %s" % error))
                else:
                    msg = data

                # emit last message
                conn.queue.append(msg)
                if conn.cr.balance < 0:
                    # someone is waiting, return last message
                    conn.cr.send(self.queue.popleft())

            elif len(self.listeners):
                listener = self.listeners.popleft()
                if error:
                    listener.c.send_exception(IOError, "uv error: %s" % error)
                else:
                    conn = UDPConn(addr)
                    conn.queue.append(data)
                    self.conns[addr] = conn
                    listener.c.send(conn, error)
            else:
                # we should probably do something there to drop connections
                self.task.throw(NoMoreListener)


            schedule()
Beispiel #4
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',)]
Beispiel #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
Beispiel #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()
Beispiel #7
0
 def test_schedule_return(self):
     def f():pass
     t1= core.tasklet(f)()
     r = core.schedule()
     assert r is core.getmain()
     t2 = core.tasklet(f)()
     r = core.schedule('test')
     assert r == 'test'
Beispiel #8
0
    def test_schedule_return(self):
        def f():
            pass

        t1 = core.tasklet(f)()
        r = core.schedule()
        assert r is core.getmain()
        t2 = core.tasklet(f)()
        r = core.schedule('test')
        assert r == 'test'
Beispiel #9
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()
Beispiel #10
0
    def _on_connection(self, client, addr):
        if len(self.listeners):
            listener = self.listeners.popleft()

            self.uv.wakeup()

            # return a new connection object to the listener
            conn = SockConn(client, self.addr, addr)
            listener.c.send((conn, None))
            schedule()
        else:
            # we should probably do something there to drop connections
            self.task.throw(NoMoreListener)
Beispiel #11
0
    def _on_connection(self, client, addr):
        if len(self.listeners):
            listener = self.listeners.popleft()

            self.uv.wakeup()

            # return a new connection object to the listener
            conn =  SockConn(client, self.addr, addr)
            listener.c.send((conn, None))
            schedule()
        else:
            # we should probably do something there to drop connections
            self.task.throw(NoMoreListener)
Beispiel #12
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()
Beispiel #13
0
    def test_schedule(self):
        output = []
        def print_(*args):
            output.append(args)

        def f(i):
            print_(i)

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

        assert output == [(1,), (2,)]
Beispiel #14
0
    def test_schedule(self):
        output = []

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

        def f(i):
            print_(i)

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

        assert output == [(1, ), (2, )]
Beispiel #15
0
        def f():
            try:
                core.schedule()
            except TaskletExit:
                global TaskletExit
                taskletexit = True
                raise

            t = core.tasklet(f)()
            t.run()
            assert t.alive
            t.kill()
            assert not t.alive
            assert taskletexit
Beispiel #16
0
        def f():
            try:
                core.schedule()
            except TaskletExit:
                global TaskletExit
                taskletexit = True
                raise

            t =  core.tasklet(f)()
            t.run()
            assert t.alive
            t.kill()
            assert not t.alive
            assert taskletexit
Beispiel #17
0
    def test_local(self):
        d = local()
        d.a = 1

        r_list = []
        def f():
            try:
                d.a
            except AttributeError:
                r_list.append(True)

        core.tasklet(f)()
        core.schedule()

        assert r_list == [True]
Beispiel #18
0
    def test_local(self):
        d = local()
        d.a = 1

        r_list = []

        def f():
            try:
                d.a
            except AttributeError:
                r_list.append(True)

        core.tasklet(f)()
        core.schedule()

        assert r_list == [True]
Beispiel #19
0
    def on_connection(self, server, error):
        if len(self.listeners):
            listener = self.listeners.popleft()

            # accept the connection
            client = pyuv.TCP(server.loop)
            server.accept(client)

            self.uv.wakeup()
            # return a new connection object to the listener
            conn = self.CONN_CLASS(client)
            listener.c.send((conn, error))
            schedule()
        else:
            # we should probably do something there to drop connections
            self.task.throw(NoMoreListener)
Beispiel #20
0
    def on_connection(self, server, error):
        if len(self.listeners):
            listener = self.listeners.popleft()

            # accept the connection
            client = pyuv.TCP(server.loop)
            server.accept(client)

            self.uv.wakeup()
            # return a new connection object to the listener
            conn = self.CONN_CLASS(client)
            listener.c.send((conn, error))
            schedule()
        else:
            # we should probably do something there to drop connections
            self.task.throw(NoMoreListener)
Beispiel #21
0
 def g():
     rlist.append('bg')
     core.schedule()
     rlist.append('ag')
Beispiel #22
0
 def h():
     rlist.append('hb')
     core.schedule()
     rlist.append('ha')
Beispiel #23
0
 def f():
     core.schedule()
Beispiel #24
0
 def g():
     rlist.append('g')
     core.schedule()
Beispiel #25
0
 def _func(now, handle):
     core.tasklet(f)(*args, **kwargs)
     core.schedule()
Beispiel #26
0
 def f():
     core.schedule()
Beispiel #27
0
 def Loop(i):
     for x in range(3):
         core.schedule()
         print_("schedule", i)
Beispiel #28
0
 def Loop(i):
     for x in range(3):
         core.schedule()
         print_("schedule", i)
Beispiel #29
0
 def h():
     rlist.append('hb')
     core.schedule()
     rlist.append('ha')
Beispiel #30
0
 def g():
     rlist.append('gb')
     core.schedule()
     rlist.append('ga')
Beispiel #31
0
 def f():
     rlist.append('fb')
     core.schedule()
     rlist.append('fa')
Beispiel #32
0
 def task(val):
     value = core.schedule(val)
     assert value == val
Beispiel #33
0
 def h():
     rlist.append('bh')
     core.schedule()
     rlist.append('ah')
Beispiel #34
0
 def counter(n, ch):
     for i in xrange(n):
         core.schedule()
     ch.receive()
     rlist.append(n)
Beispiel #35
0
 def task(val):
     value = core.schedule(val)
     assert value == val
Beispiel #36
0
 def g():
     rlist.append('g')
     core.schedule()
Beispiel #37
0
 def _on_read(self, handle, data, error):
     if error:
         self.cr.send_exception(IOError(error))
     else:
         self.cr.send(data)
     schedule()
Beispiel #38
0
 def _func(now, handle):
     core.tasklet(f)(*args, **kwargs)
     core.schedule()
Beispiel #39
0
 def f(i):
     res.append('A_%s' % i)
     core.schedule()
     res.append('B_%s' % i)
Beispiel #40
0
 def f(i):
     res.append('A_%s' % i)
     core.schedule()
     res.append('B_%s' % i)
Beispiel #41
0
 def g():
     rlist.append('bg')
     core.schedule()
     rlist.append('ag')
Beispiel #42
0
 def ready(now, h):
     curr.blocked = False
     sched.append(curr)
     core.schedule()
Beispiel #43
0
 def h():
     rlist.append('bh')
     core.schedule()
     rlist.append('ah')
Beispiel #44
0
 def counter(n, ch):
     for i in xrange(n):
         core.schedule()
     ch.send(n)
Beispiel #45
0
 def counter(n, ch):
     for i in xrange(n):
         core.schedule()
     ch.receive()
     rlist.append(n)
Beispiel #46
0
 def ready(now, h):
     curr.blocked = False
     sched.append(curr)
     core.schedule()
Beispiel #47
0
 def f():
     rlist.append('fb')
     core.schedule()
     rlist.append('fa')
Beispiel #48
0
 def g():
     rlist.append('gb')
     core.schedule()
     rlist.append('ga')
Beispiel #49
0
 def counter(n, ch):
     for i in xrange(n):
         core.schedule()
     ch.send(n)