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',)]
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()
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()
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
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()
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'
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'
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()
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)
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,)]
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, )]
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
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]
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)
def g(): rlist.append('bg') core.schedule() rlist.append('ag')
def h(): rlist.append('hb') core.schedule() rlist.append('ha')
def f(): core.schedule()
def g(): rlist.append('g') core.schedule()
def _func(now, handle): core.tasklet(f)(*args, **kwargs) core.schedule()
def Loop(i): for x in range(3): core.schedule() print_("schedule", i)
def g(): rlist.append('gb') core.schedule() rlist.append('ga')
def f(): rlist.append('fb') core.schedule() rlist.append('fa')
def task(val): value = core.schedule(val) assert value == val
def h(): rlist.append('bh') core.schedule() rlist.append('ah')
def counter(n, ch): for i in xrange(n): core.schedule() ch.receive() rlist.append(n)
def _on_read(self, handle, data, error): if error: self.cr.send_exception(IOError(error)) else: self.cr.send(data) schedule()
def f(i): res.append('A_%s' % i) core.schedule() res.append('B_%s' % i)
def ready(now, h): curr.blocked = False sched.append(curr) core.schedule()
def counter(n, ch): for i in xrange(n): core.schedule() ch.send(n)