U e@sfdZddlZddlmZmZddlmZmZddddd gZda d dZ e \Z Z d d Z d d dZdS)av Twisted integration ------------------- This module provides a very simple way to integrate your tests with the Twisted_ event loop. You must import this module *before* importing anything from Twisted itself! Example:: from nose.twistedtools import reactor, deferred @deferred() def test_resolve(): return reactor.resolve("www.python.org") Or, more realistically:: @deferred(timeout=5.0) def test_resolve(): d = reactor.resolve("www.python.org") def check_ip(ip): assert ip == "67.15.36.43" d.addCallback(check_ip) return d .. _Twisted: http://twistedmatrix.com/trac/ N)QueueEmpty)make_decorator TimeExpiredthreaded_reactorreactordeferredr stop_reactorcspzddlmWntk r&YdSXtshddlm}ddlm}|fdddatd t tfS) z Start the Twisted reactor in a separate thread, if not already done. Returns the reactor. The thread will automatically be destroyed when all the tests are done. rr)NN) threadable)Threadcs jddS)NF)ZinstallSignalHandlers)runr r?/opt/hc_python/lib/python3.8/site-packages/nose/twistedtools.py8sz"threaded_reactor..)targetT) Ztwisted.internetr ImportError_twisted_threadZtwisted.pythonr threadingr setDaemonstart)r r rr rr*s   cCs@dd}t|ttD]}|r"|q"dadS)aStop the reactor and join the reactor thread until it stops. Call this function in teardown at the module or package level to reset the twisted system after your tests. You *must* do this if you mix tests using these tools and tests using twisted.trial. cSs tdS)z0Helper for calling stop from withing the thread.N)rstoprrrrr Jsz"stop_reactor..stop_reactorN)rcallFromThreadreactor_threadjoinZgetDelayedCallsactivecancelr)r prrrr Bs   cs^t\}dkrtdzdkp*dWntk rJtdYnXfdd}|S)a By wrapping a test function with this decorator, you can return a twisted Deferred and the test will wait for the deferred to be triggered. The whole test function will run inside the Twisted event loop. The optional timeout parameter specifies the maximum duration of the test. The difference with timed() is that timed() will still wait for the test to end, while deferred() will stop the test when its timeout has expired. The latter is more desireable when dealing with network tests, because the result may actually never arrive. If the callback is triggered, the test has passed. If the errback is triggered or the timeout expires, the test has failed. Example:: @deferred(timeout=5.0) def test_resolve(): return reactor.resolve("www.python.org") Attention! If you combine this decorator with other decorators (like "raises"), deferred() must be called *first*! In other words, this is good:: @raises(DNSLookupError) @deferred() def test_error(): return reactor.resolve("xxxjhjhj.biz") and this is bad:: @deferred() @raises(DNSLookupError) def test_error(): return reactor.resolve("xxxjhjhj.biz") Nz1twisted is not available or could not be importedrz+'timeout' argument must be a number or Nonecs fdd}t|}|S)Ncstfddfddfdd}|zjd}Wn tk rntdYnX|dk r|\}}}|||dS) NcsddSN)put)valueqrrcallbacksz=deferred..decorate..wrapper..callbackcs,z |WntYnXdSr)ZraiseExceptionrsysexc_info)Zfailurer!rrerrbacks z.decorate..wrapper..errbackcs\z<}z|Wntk r8tdYnXWntYnXdS)Nz7you must return a twisted Deferred from your test case!)Z addCallbacksAttributeError TypeErrorrr$r%)d)argsr#r&funckargsr"rrgs z6deferred..decorate..wrapper..g)timeoutz*timeout expired before end of test (%f s.))rrgetrrwith_traceback)r*r,r-errorexc_type exc_valuetb)r+rr.)r*r#r&r,r"rwrappers     z+deferred..decorate..wrapper)r)r+r5rr.)r+rdecorates" zdeferred..decorate)rrr()r.rr7rr6rrVs& %)N)__doc__r$queuerrZ nose.toolsrr__all__rrrrr rrrrrs