def testThread(): expect = dr.Struct(value=1) def consumer(result): assert result.getJobID() is None assert result.get() == expect.value expect.value += 2 ss = dr.SenderNoWx(dr.Handler(consumer)) import time def worker(sender=None): sender.sendResult(1) time.sleep(0.1) sender.sendResult(3) time.sleep(0.1) sender.sendResult(5) time.sleep(0.1) return 7 tt = dr.Producer(ss, worker, senderArg='sender') tt.start() while expect.value < 7: time.sleep(0.1) print '.'
def testSendExcept(): def consumer(result): try: result.get() raise RuntimeError('should have raised!') except AssertionError: pass ss = dr.SenderNoWx(dr.Handler(consumer)) ss.sendException(AssertionError('test'))
def testHandler(): def handler(b, d, a=None, c=None): assert a == 'a' assert b == 'b' assert c == 'c' assert d == 1 hh = dr.Handler(handler, 1, a='a') hh('b', c='c') def handler2(*args, **kwargs): assert args[0] == 3 assert args[1] == 1 assert kwargs['a'] == 'a' assert kwargs['b'] == 'b' hh2 = dr.Handler(handler2, 1, a='a') args = () hh3 = dr.Handler(hh2, b='b', *args) hh3(3)
def testPreProcChain(): # test when no chain def handler(dr): assert dr.getJobID() == 123 assert dr.get() == 321 pp = dr.PreProcessChain(handler) pp(dr.DelayedResult(321, jobID=123)) # test with chaining def handlerPP(chainTrav, n, a=None): print 'In handlerPP' assert n == 1 assert a == 'a' assert chainTrav.getJobID() == 321 res = chainTrav.get() assert res == 135 print 'Done handlerPP' def subStart1(handler): pp = dr.PreProcessChain(handler) pp.addSub(subEnd1, 1, b='b') subStart2(pp.clone()) def subEnd1(chainTrav, aa, b=None): print 'In subEnd1' assert aa == 1 assert b == 'b' assert chainTrav.getJobID() == 321 res = chainTrav.get() assert res == 246, 'res=%s' % res print 'Returning from subEnd1' return res - 111 def subStart2(preProc): preProc.addSub(subEnd2, 3, c='c') ss = dr.SenderNoWx(preProc, jobID=321) ss.sendResult(123) def subEnd2(chainTrav, a, c=None): print 'In subEnd2' assert a == 3 assert c == 'c' assert chainTrav.getJobID() == 321 res = chainTrav.get() assert res == 123 print 'Returning from subEnd2' return 123 * 2 subStart1(dr.Handler(handlerPP, 1, a='a'))
def testSender(): triplet = (1, 'a', 2.34) b = dr.Struct(called=False, which=1) assert not b.called def consumer(result, a, b=None): assert result.get() == 789 + b.which assert result.getJobID() == 456 assert a == 'a' b.called = True handler = dr.Handler(consumer, 'a', **dict(b=b)) ss = dr.SenderNoWx(handler, jobID=456) ss.sendResult(789 + 1) assert b.called