def testValueReturns(self): log = [] def f1(): yield 42 def f2(): yield f1() yield activity.resume() def f3(): yield f2() v = activity.resume() log.append(v) t = activity.TaskCell(f3) EventLoop.flush() self.assertEqual(log, [42]) log = [] def f1(): yield activity.Return(42) t = activity.TaskCell(f3) EventLoop.flush() self.assertEqual(log, [42])
def testRunAtomicallyInLoop(self): log = [] def f(): self.failUnless(self.ctrl.active) log.append(1) yield activity.Pause self.failUnless(self.ctrl.active) log.append(2) t = activity.TaskCell(f) self.assertEqual(log, []) t._loop.flush() self.assertEqual(log, [1]) t._loop.flush() self.assertEqual(log, [1, 2])
def testErrorPropagation(self): log = [] def f1(): raise DummyError def f2(): try: yield f1() activity.resume() except DummyError: log.append(True) else: pass t = activity.TaskCell(f2) self.assertEqual(log, []) EventLoop.flush() self.assertEqual(log, [True])
def testResumeOnlyOnceUntilFlushed(self): log = [] c1 = trellis.Value(1) c2 = trellis.Value(2) def f(): for i in range(3): c1.value, c2.value log.append(i) yield activity.Pause t = activity.TaskCell(f) self.assertEqual(log, []) EventLoop.flush() self.assertEqual(log, [0]) c1.value = 3 self.assertEqual(log, [0]) c2.value = 4 EventLoop.flush() self.assertEqual(log, [0, 1])
def testSendAndThrow(self): log = [] class SendThrowIter(object): count = 0 def next(self): if self.count == 0: self.count = 1 def f(): yield 99 return f() raise StopIteration def send(self, value): log.append(value) def f(): raise DummyError yield None return f() def throw(self, typ, val, tb): log.append(typ) log.append(val.__class__) # type(val) is instance in Py<2.5 log.append(type(tb)) raise StopIteration def fs(): yield SendThrowIter() t = activity.TaskCell(fs) self.assertEqual(log, []) EventLoop.flush() self.assertEqual(log, [99, DummyError, DummyError, types.TracebackType])
def testDependencyAndCallback(self): log = [] v = trellis.Value(42) v1 = trellis.Value(1) c1 = trellis.Cell(lambda: v1.value * 2) def f(): while v.value: log.append(v.value) v1.value = v.value yield activity.Pause t = activity.TaskCell(f) check = [] for j in 42, 57, 99, 106, 23, None: self.assertEqual(log, check) v.value = j if j: check.append(j) for i in range(5): t._loop.flush() if j: self.assertEqual(c1.value, j * 2) self.assertEqual(log, check)