def testSchedule(self): res1 = [] res2 = [] def ch1(): for i in range(10): res1.append((i, stackless.getruncount())) stackless.schedule() def ch2(): for i in range(10): res2.append((i, stackless.getruncount())) stackless.schedule() child1 = stackless.tasklet(ch1)() child2 = stackless.tasklet(ch2)() self.assertEquals(3, stackless.getruncount()) #main + ch1, ch2 while stackless.getruncount() > 1: stackless.schedule() self.assertEquals([(0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (7, 3), (8, 3), (9, 3)], res1) self.assertEquals([(0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (7, 3), (8, 3), (9, 3)], res2) self.assertEquals(1, stackless.getruncount()) #main
def testChannel(self): c = stackless.channel() recvd = [] def ch1(): for i in range(10): recvd.append(c.receive()) def ch2(): for i in range(10): c.send(i) child1 = stackless.tasklet(ch1)() child2 = stackless.tasklet(ch2)() self.assertEquals(3, stackless.getruncount()) #main while stackless.getruncount() > 1: stackless.schedule() self.assertEquals(range(10), recvd) self.assertEquals(1, stackless.getruncount()) #main
def child(r): r.b1 = c.balance r.rc1 = stackless.getruncount() r.cur1 = stackless.getcurrent() r.blocked1 = r.cur1.blocked r.alive1 = r.cur1.alive try: c.receive() finally: r.b2 = c.balance r.rc2 = stackless.getruncount() r.cur2 = stackless.getcurrent() r.blocked2 = r.cur2.blocked r.alive2 = r.cur2.alive
def child1(): i = 0 while True: i += 1 #print 'c', i r.rc1 = stackless.getruncount() r.cur1 = stackless.getcurrent() r.blocked1 = r.cur1.blocked r.alive1 = r.cur1.alive try: stackless.schedule() except TaskletExit: #print 'kill' r.rc2 = stackless.getruncount() r.cur2 = stackless.getcurrent() r.blocked2 = r.cur2.blocked r.alive2 = r.cur2.alive raise
def ch2(): for i in range(10): res2.append((i, stackless.getruncount())) stackless.schedule()
class TestStackless(unittest.TestCase): def setUp(self): logging.debug(self) def testSchedule(self): res1 = [] res2 = [] def ch1(): for i in range(10): res1.append((i, stackless.getruncount())) stackless.schedule() def ch2(): for i in range(10): res2.append((i, stackless.getruncount())) stackless.schedule() child1 = stackless.tasklet(ch1)() child2 = stackless.tasklet(ch2)() self.assertEquals(3, stackless.getruncount()) #main + ch1, ch2 while stackless.getruncount() > 1: stackless.schedule() self.assertEquals([(0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (7, 3), (8, 3), (9, 3)], res1) self.assertEquals([(0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (7, 3), (8, 3), (9, 3)], res2) self.assertEquals(1, stackless.getruncount()) #main def testChannel(self): c = stackless.channel() recvd = [] def ch1(): for i in range(10): recvd.append(c.receive()) def ch2(): for i in range(10): c.send(i) child1 = stackless.tasklet(ch1)() child2 = stackless.tasklet(ch2)() self.assertEquals(3, stackless.getruncount()) #main while stackless.getruncount() > 1: stackless.schedule() self.assertEquals(range(10), recvd) self.assertEquals(1, stackless.getruncount()) #main def testChannelException(self): c = stackless.channel() result = [] def ch1(): for i in range(2): try: result.append(c.receive()) except TestError, te: result.append(te) def ch2(): c.send(True) c.send_exception(TestError, "test") child1 = stackless.tasklet(ch1)() child2 = stackless.tasklet(ch2)() self.assertEquals(3, stackless.getruncount()) #main + ch1 + ch2 while stackless.getruncount() > 1: stackless.schedule() self.assertEquals(True, result[0]) self.assertTrue(isinstance(result[1], TestError)) self.assertEquals(1, stackless.getruncount()) #main