def testReducerFuture(self): @tasklets.tasklet def sum_tasklet(arg): yield tasklets.sleep(0.01) raise tasklets.Return(sum(arg)) @tasklets.tasklet def produce_one(i): yield tasklets.sleep(i * 0.01) raise tasklets.Return(i) @tasklets.tasklet def producer(): for i in range(10): q.add_dependent(produce_one(i)) q.complete() @tasklets.tasklet def consumer(): total = yield q self.assertEqual(total, sum(range(10))) @tasklets.tasklet def foo(): yield producer(), consumer() q = tasklets.ReducingFuture(sum_tasklet, batch_size=3) foo().get_result() q = tasklets.ReducingFuture(sum, batch_size=3) foo().get_result()
def testReducingFuture_Empty(self): def reducer(arg): self.fail() rf = tasklets.ReducingFuture(reducer) rf.complete() self.assertEqual(rf.get_result(), None)
def testReducingFuture_OneItem(self): def reducer(arg): self.fail() rf = tasklets.ReducingFuture(reducer) rf.putq(1) rf.complete() self.assertEqual(rf.get_result(), 1)
def testReducingFuture_ReducerException_1(self): def reducer(arg): raise ZeroDivisionError rf = tasklets.ReducingFuture(reducer) rf.putq(1) rf.putq(1) rf.complete() self.assertRaises(ZeroDivisionError, rf.get_result)
def testReducingFuture_ItemException(self): def reducer(arg): return sum(arg) rf = tasklets.ReducingFuture(reducer) f1 = Future() f1.set_exception(ZeroDivisionError()) rf.putq(f1) rf.complete() self.assertRaises(ZeroDivisionError, rf.get_result)
def testReducingFuture_ReducerFuture_1(self): def reducer(arg): f = Future() f.set_result(sum(arg)) return f rf = tasklets.ReducingFuture(reducer, batch_size=2) rf.putq(1) rf.putq(1) rf.complete() self.assertEqual(rf.get_result(), 2)
def testReducingFuture(self): def reducer(arg): return sum(arg) rf = tasklets.ReducingFuture(reducer, batch_size=10) for i in range(10): rf.putq(i) for i in range(10, 20): f = Future() rf.putq(f) f.set_result(i) rf.complete() self.assertEqual(rf.get_result(), sum(range(20)))
def testReducingFuture_ReducerFuture_2(self): # Weird hack to reach _internal_add_dependent() call in _mark_finished(). def reducer(arg): res = sum(arg) if len(arg) < 3: f = Future() f.set_result(res) res = f return res rf = tasklets.ReducingFuture(reducer, batch_size=3) rf.putq(1) rf.putq(1) rf.putq(1) rf.putq(1) rf.complete() self.assertEqual(rf.get_result(), 4)