K idZddlZddlmZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlmZddlmZmZddlmZddlmZmZddlmZdd lmZdd lmZdd lmZdd lm Z m!Z!dd l"m#Z#ddl$Z$ddl$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,ddl-m.Z.e$j^re%e+e(e0e+e0e+e.fZ1eZ2 d-de3de4de%e je5ffdZ6de7fdZ8Gdde jrZ:Gdde:Z;Gdde;Ze$jzd"e'de*edffde'd fd#Z> d.d"e+e'de*edffde+e7de*e'd e'e'de*edffge'd fffd$Z>de>_?Gd%d&ejZAd'e jrd(e&de&fd)ZBd*e&ddfd+ZCeDd,k(reCyy)/aSupport classes for automated testing. * `AsyncTestCase` and `AsyncHTTPTestCase`: Subclasses of unittest.TestCase with additional support for testing asynchronous (`.IOLoop`-based) code. * `ExpectLog`: Make test logs less spammy. * `main()`: A simple test runner (wrapper around unittest.main()) with support for the tornado.autoreload module to rerun the tests when code changes. N) Generator)gen)AsyncHTTPClient HTTPResponse) HTTPServer)IOLoop TimeoutError)netutil)AsyncIOMainLoop) Subprocess)app_log)raise_exc_infobasestring_type) Application)TupleAnyCallableTypeDictUnionOptional Coroutine) TracebackTypeF reuse_portaddressreturnctjd|tj|d}|j d}||fS)aIBinds a server socket to an available port on localhost. Returns a tuple (socket, port). .. versionchanged:: 4.4 Always binds to ``127.0.0.1`` without resolving the name ``localhost``. .. versionchanged:: 6.2 Added optional ``address`` argument to override the default "127.0.0.1". r)familyr)r bind_socketssocketAF_INET getsockname)rrsockports U/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/tornado/testing.pybind_unused_portr'1sH    76>>j  D    a D :c|tjjd}| t|Sy#t$rYywxYw)z}Get the global timeout setting for async tests. Returns a float, the timeout in seconds. .. versionadded:: 3.1 ASYNC_TEST_TIMEOUT)osenvirongetfloat ValueError)envs r&get_async_test_timeoutr2GsG **..- .C  :      s / ;;ceZdZdZddeddffd Zdfd Zdfd ZdefdZ d e e d e d e de fd Zdd Z ddeej$deej$ffd ZdeddfdZddededdfdZ ddeede fdeedefdZxZS) AsyncTestCasea/`~unittest.TestCase` subclass for testing `.IOLoop`-based asynchronous code. The unittest framework is synchronous, so the test must be complete by the time the test method returns. This means that asynchronous code cannot be used in quite the same way as usual and must be adapted to fit. To write your tests with coroutines, decorate your test methods with `tornado.testing.gen_test` instead of `tornado.gen.coroutine`. This class also provides the (deprecated) `stop()` and `wait()` methods for a more manual style of testing. The test method itself must call ``self.wait()``, and asynchronous callbacks should call ``self.stop()`` to signal completion. By default, a new `.IOLoop` is constructed for each test and is available as ``self.io_loop``. If the code being tested requires a reused global `.IOLoop`, subclasses should override `get_new_ioloop` to return it, although this is deprecated as of Tornado 6.3. The `.IOLoop`'s ``start`` and ``stop`` methods should not be called directly. Instead, use `self.stop ` and `self.wait `. Arguments passed to ``self.stop`` are returned from ``self.wait``. It is possible to have multiple ``wait``/``stop`` cycles in the same test. Example:: # This test uses coroutine style. class MyTestCase(AsyncTestCase): @tornado.testing.gen_test def test_http_fetch(self): client = AsyncHTTPClient() response = yield client.fetch("http://www.tornadoweb.org") # Test contents of response self.assertIn("FriendFeed", response.body) # This test uses argument passing between self.stop and self.wait. class MyTestCase2(AsyncTestCase): def test_http_fetch(self): client = AsyncHTTPClient() client.fetch("http://www.tornadoweb.org/", self.stop) response = self.wait() # Test contents of response self.assertIn("FriendFeed", response.body) methodNamerNcxt||d|_d|_d|_d|_d|_d|_y)NF)super__init___AsyncTestCase__stopped_AsyncTestCase__running_AsyncTestCase__failure_AsyncTestCase__stop_args_AsyncTestCase__timeout_test_generator)selfr5 __class__s r&r8zAsyncTestCase.__init__s> $ $r(ctj}d|cxkrdksnd|cxkrdkr>nn;t|tjtj ddt dt|!t|jtjurtjd t |j|_ tj|jj y) N) r)rBrC )rB r)rBrErignorezThere is no current event loopz tornado\..*)messagecategorymodulezget_new_ioloop is deprecated)sys version_infosetup_with_context_managerwarningscatch_warningsfilterwarningsDeprecationWarningr7setUptypeget_new_ioloopr4warnio_loopasyncioset_event_loop asyncio_loop)r?py_verr@s r&rQzAsyncTestCase.setUps!! & -: -:3U:3U 'tX-D-D-F G  # #8+%     : $ $M,H,H H MM8:L M**, t||889r(c|jj}tj|Dcgc]}|j r|c}D]}|j r=|jj fd\}}|rJ|D]} |jtjtjdt|jts|jjdt|A|j#ycc}w#tj$rYwxYw)Nc.tjSN)rVwait)taskssr&z(AsyncTestCase.tearDown..s',,u:Mr(T)all_fds)rUrXrV all_tasksdonecancelrun_syncresultCancelledErrorr uninitializerW isinstance_NON_OWNED_IOLOOPScloser7tearDown_AsyncTestCase__rethrow)r?rXtrbpendingfr^r@s @r&rkzAsyncTestCase.tearDowns ||00 !!,/"2q2 A HHJ   LL112MNMD' ; HHJ  !t$$,,(:; LL  t  ,  C3 --sD#D#D((D>=D>ctdS)aReturns the `.IOLoop` to use for this test. By default, a new `.IOLoop` is created for each test. Subclasses may override this method to return `.IOLoop.current()` if it is not appropriate to use a new `.IOLoop` in each tests (for example, if there are global singletons using the default `.IOLoop`) or if a per-test event loop is being provided by another system (such as ``pytest-asyncio``). .. deprecated:: 6.3 This method will be removed in Tornado 7.0. F) make_current)rr?s r&rSzAsyncTestCase.get_new_ioloops5))r(typvaluetbc|j |||f|_ntjd|||f|jy)Nz%multiple unhandled exceptions in test)exc_infoT)r;r errorstopr?rsrtrus r&_handle_exceptionzAsyncTestCase._handle_exceptionsB >> !!5"-DN MM73rBR  r(cZ|j|j}d|_t|yyr\)r;r)r?failures r& __rethrowzAsyncTestCase.__rethrows) >> %nnG!DN 7 # &r(recFt||}|j|Sr\)r7runrl)r?reretr@s r&rzAsyncTestCase.runs$gk&!  r(methodc|}t|tstj|r t d|t d|zy)axRun the given test method, raising an error if it returns non-None. Failure to decorate asynchronous test methods with ``@gen_test`` can lead to tests incorrectly passing. Remove this override when Python 3.10 support is dropped. This check (in the form of a DeprecationWarning) became a part of the standard library in 3.11. Note that ``_callTestMethod`` is not documented as a public interface. However, it is present in all supported versions of Python (3.8+), and if it goes away in the future that's OK because we can just remove this override as noted above. zVGenerator and coroutine test methods should be decorated with tornado.testing.gen_testNz)Return value from test method ignored: %r)rhrinspect iscoroutine TypeErrorr0)r?rres r&_callTestMethodzAsyncTestCase._callTestMethodsR fi (G,?,?,G;  H6QR R r(_argkwargsc ||rJ|xs||_|jr!|jjd|_d|_y)aIStops the `.IOLoop`, causing one pending (or future) call to `wait()` to return. Keyword arguments or a single positional argument passed to `stop()` are saved and will be returned by `wait()`. .. deprecated:: 5.1 `stop` and `wait` are deprecated; use ``@gen_test`` instead. NFT)r<r:rUryr9)r?rrs r&ryzAsyncTestCase.stopsB|6))!>T >> LL   "DNr( condition.timeoutc tjsrCdfd }jjjj z|_ d_jjj ||rn8j ,jjj d_jsJd_jj}d_ |S)aRuns the `.IOLoop` until stop is called or timeout has passed. In the event of a timeout, an exception will be thrown. The default timeout is 5 seconds; it may be overridden with a ``timeout`` keyword argument or globally with the ``ASYNC_TEST_TIMEOUT`` environment variable. If ``condition`` is not ``None``, the `.IOLoop` will be restarted after `stop()` until ``condition()`` returns ``True``. .. versionchanged:: 3.1 Added the ``ASYNC_TEST_TIMEOUT`` environment variable. .. deprecated:: 5.1 `stop` and `wait` are deprecated; use ``@gen_test`` instead. Nc jdz#t$rtj_YnwxYwj y)Nz*Async operation timed out after %s seconds)failureException ExceptionrJrwr;ry)r?rsr& timeout_funcz(AsyncTestCase.wait..timeout_funcCsJ8"33H7R%8),8IIKs "<<TFrN) r2r9rU add_timeouttimer=r:startr;remove_timeoutrlr<)r?rrrres` ` r&r]zAsyncTestCase.wait's, ?,.G~~ "&!9!9LL%%''1<"!% ""$>>-1Bik  ~~) ++DNN;!%~~~ !! r()runTestrr\NN)__name__ __module__ __qualname____doc__strr8rQrkrrSrrrboolr{rlrunittest TestResultrrrrryr/r] __classcell__r@s@r&r4r4Ws-^ $3 $t $:$*X**  ? +4 :G   $7; x223  (%% & ShS4S0st(48#'5HS$Y/05%5 5r(r4c eZdZdZdfd ZdefdZdefdZde fdZ dde de d e defd Zdee e ffd Zdefd Zde fd Zde de fdZdfd ZxZS)AsyncHTTPTestCasearA test case that starts up an HTTP server. Subclasses must override `get_app()`, which returns the `tornado.web.Application` (or other `.HTTPServer` callback) to be tested. Tests will typically use the provided ``self.http_client`` to fetch URLs from this server. Example, assuming the "Hello, world" example from the user guide is in ``hello.py``:: import hello class TestHelloApp(AsyncHTTPTestCase): def get_app(self): return hello.make_app() def test_homepage(self): response = self.fetch('/') self.assertEqual(response.code, 200) self.assertEqual(response.body, 'Hello, world') That call to ``self.fetch()`` is equivalent to :: self.http_client.fetch(self.get_url('/'), self.stop) response = self.wait() which illustrates how AsyncTestCase can turn an asynchronous operation, like ``http_client.fetch()``, into a synchronous operation. If you need to do other asynchronous operations in tests, you'll probably need to use ``stop()`` and ``wait()`` yourself. rct|t\}}||_|j |_|j |_|j|_ |jj|gyr\) r7rQr'_AsyncHTTPTestCase__portget_http_client http_clientget_app_appget_http_server http_server add_sockets)r?r$r%r@s r&rQzAsyncHTTPTestCase.setUpsd  %' d //1LLN //1 $$dV,r(ctSr\)rrrs r&rz!AsyncHTTPTestCase.get_http_clients   r(cJt|jfi|jSr\)rrget_httpserver_optionsrrs r&rz!AsyncHTTPTestCase.get_http_servers$))Et'B'B'DEEr(ct)zzShould be overridden by subclasses to return a `tornado.web.Application` or other `.HTTPServer` callback. )NotImplementedErrorrrs r&rzAsyncHTTPTestCase.get_apps "##r(path raise_errorrc |jjdr|nj|jj fdt S)aEConvenience method to synchronously fetch a URL. The given path will be appended to the local server's host and port. Any additional keyword arguments will be passed directly to `.AsyncHTTPClient.fetch` (and so could be used to pass ``method="POST"``, ``body="..."``, etc). If the path begins with http:// or https://, it will be treated as a full URL and will be fetched as-is. If ``raise_error`` is ``True``, a `tornado.httpclient.HTTPError` will be raised if the response code is not 200. This is the same behavior as the ``raise_error`` argument to `.AsyncHTTPClient.fetch`, but the default is ``False`` here (it's ``True`` in `.AsyncHTTPClient`) because tests often need to deal with non-200 response codes. .. versionchanged:: 5.0 Added support for absolute URLs. .. versionchanged:: 5.1 Added the ``raise_error`` argument. .. deprecated:: 5.1 This method currently turns any exception into an `.HTTPResponse` with status code 599. In Tornado 6.0, errors other than `tornado.httpclient.HTTPError` will be passed through, and ``raise_error=False`` will only suppress errors that would be raised due to non-200 response codes. )zhttp://zhttps://cBjjfdiS)Nr)rfetch)rrr?urlsr&r_z)AsyncHTTPTestCase.fetch..s%*D$$**3RKR6Rr(r)lower startswithget_urlrUrdr2)r?rrrrs` ``@r&rzAsyncHTTPTestCase.fetchsSH ::< " "#: ;C,,t$C||$$ R*,%  r(ciS)zgMay be overridden by subclasses to return additional keyword arguments for the server. rrs r&rz(AsyncHTTPTestCase.get_httpserver_optionss  r(c|jS)zZReturns the port used by the server. A new port is chosen for each test. )rrrs r& get_http_portzAsyncHTTPTestCase.get_http_ports {{r(cy)Nhttprrrs r& get_protocolzAsyncHTTPTestCase.get_protocolsr(cL|jd|j|S)z>Returns an absolute url for the given path on the test server.z ://127.0.0.1:)rr)r?rs r&rzAsyncHTTPTestCase.get_urls+##%&mD4F4F4H3I$PPr(c|jj|jj|jjt |j j|`|`t|)y)Nr) rryrUrdclose_all_connectionsr2rrjrr7rk)r?r@s r&rkzAsyncHTTPTestCase.tearDownsk      2 2A+ + ZS#X scQCQCQr(rcteZdZdZdefdZdeeeffdZ deeeffdZ e deeeffdZ defdZ y) AsyncHTTPSTestCasezjA test case that starts an HTTPS server. Interface is generally the same as `AsyncHTTPTestCase`. rc0tdtdS)NTF) validate_cert)force_instancedefaults)rdictrrs r&rz"AsyncHTTPSTestCase.get_http_clientsdTPU=VWWr(c6t|jS)N) ssl_options)rget_ssl_optionsrrs r&rz)AsyncHTTPSTestCase.get_httpserver_optionss 4 4 677r(c*tjS)zMay be overridden by subclasses to select SSL options. By default includes a self-signed testing certificate. )rdefault_ssl_optionsrrs r&rz"AsyncHTTPSTestCase.get_ssl_optionss "5577r(ctjjt}t tjj |ddtjj |ddS)Ntestztest.crtztest.key)certfilekeyfile)r,rdirname__file__rjoin) module_dirs r&rz&AsyncHTTPSTestCase.default_ssl_optionssKWW__X. WW\\*fjAGGLLVZ@  r(cy)Nhttpsrrrs r&rzAsyncHTTPSTestCase.get_protocolsr(N)rrrrrrrrrrr staticmethodrrrr(r&rrsl XX8S#X88c3h8  c3h    cr(rrr.r.Ncyr\rrs r&gen_testrs r(funccyr\r)rs r&rr sr(c tdtdttdffdtdffd }|||S|S)a1Testing equivalent of ``@gen.coroutine``, to be applied to test methods. ``@gen.coroutine`` cannot be used on tests because the `.IOLoop` is not already running. ``@gen_test`` should be applied to test methods on subclasses of `AsyncTestCase`. Example:: class MyTest(AsyncHTTPTestCase): @gen_test def test_something(self): response = yield self.http_client.fetch(self.get_url('/')) By default, ``@gen_test`` times out after 5 seconds. The timeout may be overridden globally with the ``ASYNC_TEST_TIMEOUT`` environment variable, or for each test with the ``timeout`` keyword argument:: class MyTest(AsyncHTTPTestCase): @gen_test(timeout=10) def test_something_slow(self): response = yield self.http_client.fetch(self.get_url('/')) Note that ``@gen_test`` is incompatible with `AsyncTestCase.stop`, `AsyncTestCase.wait`, and `AsyncHTTPTestCase.fetch`. Use ``yield self.http_client.fetch(self.get_url())`` as shown above instead. .. versionadded:: 3.1 The ``timeout`` argument and ``ASYNC_TEST_TIMEOUT`` environment variable. .. versionchanged:: 4.0 The wrapper now passes along ``*args, **kwargs`` so it can be used on functions with arguments. ro.rrrctjfd}tjr|nt j |tjfd}|S)Nc|g|i|}t|tstj|r ||_|Sd|_|Sr\)rhrrrr>)r?argsrreros r& pre_coroutinez-gen_test..wrap..pre_coroutineHsNt-d-f-F&),0C0CF0K'-$M(,$Mr(c |jjtj|g|i|S#t$rD}|j 2t |j ddr|j j|d}~wwxYw)Nr cr_runningT)rUrd functoolspartialr r>getattrthrow)r?rrecorors r&post_coroutinez.gen_test..wrap..post_coroutineXs ||,,%%dDB4B6BG-  ''3((,9((..q1  s48 B?BB)rwrapsriscoroutinefunctionr coroutine)rorrrrs` @r&wrapzgen_test..wrap?sf       & &q ) D==/D     .r()r2rrr)rrrs ` r&rrs[T(*1eI{$:;;<1)AT1f  Dz r(c eZdZdZ ddeej efdede de e ddf dZ d ejde fd Zdd Zd d de ede eddfdZy) ExpectLogaContext manager to capture and suppress expected log output. Useful to make tests of error conditions less noisy, while still leaving unexpected log entries visible. *Not thread safe.* The attribute ``logged_stack`` is set to ``True`` if any exception stack trace was logged. Usage:: with ExpectLog('tornado.application', "Uncaught exception"): error_response = self.fetch("/some_page") .. versionchanged:: 4.3 Added the ``logged_stack`` attribute. Nloggerregexrequiredlevelrct|trtj|}||_t j ||_||_d|_ d|_ d|_ ||_ d|_ y)aConstructs an ExpectLog context manager. :param logger: Logger object (or name of logger) to watch. Pass an empty string to watch the root logger. :param regex: Regular expression to match. Any log entries on the specified logger that match this regex will be suppressed. :param required: If true, an exception will be raised if the end of the ``with`` statement is reached without matching any log entries. :param level: A constant from the ``logging`` module indicating the expected log level. If this parameter is provided, only log messages at this level will be considered to match. Additionally, the supplied ``logger`` will have its level adjusted if necessary (for the duration of the ``ExpectLog`` to enable the expected message. .. versionchanged:: 6.1 Added the ``level`` parameter. .. deprecated:: 6.3 In Tornado 7.0, only ``WARNING`` and higher logging levels will be matched by default. To match ``INFO`` and lower levels, the ``level`` argument must be used. This is changing to minimize differences between ``tornado.testing.main`` (which enables ``INFO`` logs by default) and most other test runners (including those in IDEs) which have ``INFO`` logs disabled by default. rFN)rhrlogging getLoggerrrecompilerrmatcheddeprecated_level_matched logged_stackr orig_level)r?rrrrs r&r8zExpectLog.__init__sb@ fo .&&v.F ZZ&    ()%! r(recordc |jrd|_|j}|jj |r|j 2|j tjkr|xjdz c_ |j `|j |j k7rGtjd|dtj|j d|jdy|xjdz c_yy)NTrzGot expected log message z at unexpected level (z vs )F)rwr getMessagermatchrlevelnorWARNINGrr warning getLevelName levelnamer)r?rrGs r&filterzExpectLog.filters ?? $D ##% ::  G $zz!fnnw&F --2-zz%&..DJJ*F 4 4TZZ @&BRBRT LLA Lr(c"|jg|j|jjkr@|jj|_|jj |j|jj ||Sr\)rrgetEffectiveLevelrsetLevel addFilterrrs r& __enter__zExpectLog.__enter__sb :: !djj4;;3P3P3R&R"kk//DO KK  , d# r(rszOptional[Type[BaseException]]rtrucn|j%|jj|j|jj||s#|jr|j s t d|sB|jr5|j|j k\rtjdtyyyy)Nz did not get expected log messagez9ExpectLog matched at INFO or below without level argument) rrr removeFilterrrrrrMrTrPrzs r&__exit__zExpectLog.__exit__s ?? & KK  1   &t}}T\\>? ? ..$,,> MMK" ?r()TN)rr)rrrrrrLoggerrrrrrr8 LogRecordrr BaseExceptionrrrr(r&rrs*# *gnno56** * } *  *XW..4, , & ] #   r(rtestcasecmcb|j}|j|jddd|S)a[Use a context manager to setUp a test case. Example:: def setUp(self): setup_with_context_manager(self, warnings.catch_warnings()) warnings.filterwarnings("ignore", category=DeprecationWarning) # The catch_warnings context manager will be deactivated # automatically in tearDown. N)r addCleanupr)rrvals r&rLrLs, ,,.C  T46 Jr(rc Lddlm}m}m}|dtdd|dt|d t|d t|d t|d tt j dg|t j z}|js2tjtjtj|jd|d<|jd|d<|jd|d <|jd|d<|jd|d <t dk(r>t#|dk(r0t%dt j&t j(dt#|dkDrt+j,dd |d|y t+j,dd|d|y )aA simple test runner. This test runner is essentially equivalent to `unittest.main` from the standard library, but adds support for Tornado-style option parsing and log formatting. It is *not* necessary to use this `main` function to run tests using `AsyncTestCase`; these tests are self-contained and can run with any test runner. The easiest way to run a test is via the command line:: python -m tornado.testing tornado.test.web_test See the standard library ``unittest`` module for ways in which tests can be specified. Projects with many tests may wish to define a test script like ``tornado/test/runtests.py``. This script should define a method ``all()`` which returns a test suite and then call `tornado.testing.main()`. Note that even when a test script is used, the ``all()`` test suite may be overridden by naming a single test on the command line:: # Runs all tests python -m tornado.test.runtests # Runs one test python -m tornado.test.runtests tornado.test.web_test Additional keyword arguments passed through to ``unittest.main()``. For example, use ``tornado.testing.main(verbosity=2)`` to show many test details as they are run. See http://docs.python.org/library/unittest.html#unittest.main for full argument list. .. versionchanged:: 5.0 This function produces no output of its own; only that produced by the `unittest` module (previously it would add a PASS or FAIL log message). r)defineoptionsparse_command_lineexception_on_interruptTzIf true (default), ctrl-c raises a KeyboardInterrupt exception. This prints a stack trace but cannot interrupt certain operations. If false, the process is more reliably killed, but does not print a stack trace.)rRdefaulthelpverbose)rRquietfailfastcatchbufferN verbosity catchbreak__main__rzNo tests specified)file)rIargvall) defaultTestr1r)tornado.optionsr!r"r#rrJr1r$signalSIGINTSIG_DFLr'r(r)r*r+rlenprintstderrexitrmain)rr!r"r#r1s r&r<r<s^PDC   8  94  7 :D! 7 8$ HHQK=-chh7 7D  ) ) fmmV^^4"{}} {#!z}} #|~~!x:#d)q. "4   4y1} 7T77 =%d=f=r(r/)Fz 127.0.0.1r)ErrVcollections.abcrrrrr,rr5r!rJrrMtornadortornado.httpclientrrtornado.httpserverrtornado.iolooprr r tornado.platform.asyncior tornado.processr tornado.logr tornado.utilrr tornado.webrtypingrrrrrrrrtypesr TYPE_CHECKINGr _ExcInfoTuplerirrrr'r/r2TestCaser4rroverloadr__test__FilterrrLr<rrr(r&rOs % <)/4&8# OOO m$%x '>@WWM %.9'* 6==# ,    EH%%EP@ @F!*!H"&   xU9k#9::;QQR   8Cy+'=!>>? HYDW  DH#h 8Cy+'=!>>? @h e_h  Y hsE)["899: ;Xi=P PQShZnnd):):"Y>3Y>4Y>x zFr(