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 testMultiFuture_Repr(self): mf = tasklets.MultiFuture('info') r1 = repr(mf) mf.putq(1) r2 = repr(mf) f2 = Future() f2.set_result(2) mf.putq(2) r3 = repr(mf) self.ev.run() r4 = repr(mf) f3 = Future() mf.putq(f3) r5 = repr(mf) mf.complete() r6 = repr(mf) f3.set_result(3) self.ev.run() r7 = repr(mf) for r in r1, r2, r3, r4, r5, r6, r7: self.assertTrue( re.match( r'<MultiFuture [\da-f]+ created by ' r'(testMultiFuture_Repr\(tasklets_test.py:\d+\)|\?) for info; ', r)) if r is r7: self.assertTrue('result' in r) else: self.assertTrue('pending' in r)
def testSerialQueueFuture_PutQ_1(self): sqf = tasklets.SerialQueueFuture() f1 = Future() sqf.putq(f1) sqf.complete() f1.set_result(1) self.assertEqual(sqf.getq().get_result(), 1)
def reducer(arg): res = sum(arg) if len(arg) < 3: f = Future() f.set_result(res) res = f return res
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 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 testMultiFuture_ItemException(self): mf = tasklets.MultiFuture() f1 = Future() f2 = Future() f3 = Future() f2.set_result(2) mf.putq(f1) f1.set_exception(ZeroDivisionError()) mf.putq(f2) mf.putq(f3) f3.set_result(3) self.ev.run() mf.complete() self.assertRaises(ZeroDivisionError, mf.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 reducer(arg): f = Future() f.set_result(sum(arg)) return f