def testQueueFuture(self): q = tasklets.QueueFuture() @tasklets.tasklet def produce_one(i): yield tasklets.sleep(i * 0.01) raise tasklets.Return(i) @tasklets.tasklet def producer(): q.putq(0) for i in range(1, 10): q.add_dependent(produce_one(i)) q.complete() @tasklets.tasklet def consumer(): for i in range(10): val = yield q.getq() self.assertEqual(val, i) yield q self.assertRaises(EOFError, q.getq().get_result) @tasklets.tasklet def foo(): yield producer(), consumer() foo().get_result()
def testQueueFuture_ItemException(self): qf = tasklets.QueueFuture() qf.putq(1) f2 = Future() qf.putq(f2) f3 = Future() f3.set_result(3) self.ev.run() qf.putq(f3) self.ev.run() f4 = Future() f4.set_exception(ZeroDivisionError()) self.ev.run() qf.putq(f4) f5 = Future() qf.putq(f5) self.ev.run() qf.complete() self.ev.run() f2.set_result(2) self.ev.run() f5.set_exception(KeyError()) self.ev.run() # Futures are returned in the order of completion, which should be # f1, f3, f4, f2, f5. These produce 1, 3, ZeroDivisionError, 2, # KeyError, respectively. After that EOFError is raised. self.assertEqual(qf.getq().get_result(), 1) self.assertEqual(qf.getq().get_result(), 3) self.assertRaises(ZeroDivisionError, qf.getq().get_result) self.assertEqual(qf.getq().get_result(), 2) self.assertRaises(KeyError, qf.getq().get_result) self.assertRaises(EOFError, qf.getq().get_result) self.assertRaises(EOFError, qf.getq().get_result)
def testQueueFuture_SetException(self): qf = tasklets.QueueFuture() f1 = Future() f1.set_result(1) qf.putq(f1) qf.putq(f1) self.ev.run() qf.putq(2) self.ev.run() f3 = Future() f3.set_exception(ZeroDivisionError()) qf.putq(f3) self.ev.run() f4 = Future() qf.putq(f4) self.ev.run() qf.set_exception(KeyError()) f4.set_result(4) self.ev.run() self.assertRaises(KeyError, qf.get_result) # Futures are returned in the order of completion, which should be # f1, f2, f3, f4. These produce 1, 2, ZeroDivisionError, 4, # respectively. After that KeyError (the exception set on qf # itself) is raised. self.assertEqual(qf.getq().get_result(), 1) self.assertEqual(qf.getq().get_result(), 2) self.assertRaises(ZeroDivisionError, qf.getq().get_result) self.assertEqual(qf.getq().get_result(), 4) self.assertRaises(KeyError, qf.getq().get_result) self.assertRaises(KeyError, qf.getq().get_result)
def testQueueFuture_Complete(self): qf = tasklets.QueueFuture() qf.putq(1) f2 = Future() qf.putq(f2) self.ev.run() g1 = qf.getq() g2 = qf.getq() g3 = qf.getq() f2.set_result(2) self.ev.run() qf.complete() self.ev.run() self.assertEqual(g1.get_result(), 1) self.assertEqual(g2.get_result(), 2) self.assertRaises(EOFError, g3.get_result) self.assertRaises(EOFError, qf.getq().get_result)
def testContext_MapQuery_CustomFuture(self): mfut = tasklets.QueueFuture() @tasklets.tasklet def callback(ent): return ent.key.flat()[-1] @tasklets.tasklet def foo(): yield self.create_entities() qry = query.Query(kind='Foo') res = yield self.ctx.map_query(qry, callback, merge_future=mfut) self.assertEqual(res, None) vals = set() for i in range(3): val = yield mfut.getq() vals.add(val) fail = mfut.getq() self.assertRaises(EOFError, fail.get_result) raise tasklets.Return(vals) res = foo().get_result() self.assertEqual(res, set([1, 2, 3]))
def testQueueFuture_SetExceptionAlternative(self): qf = tasklets.QueueFuture() g1 = qf.getq() qf.set_exception(KeyError()) self.ev.run() self.assertRaises(KeyError, g1.get_result)