| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # Tahoe-LAFS -- secure, distributed storage grid |
|---|
| 3 | # |
|---|
| 4 | # Copyright © 2020 The Tahoe-LAFS Software Foundation |
|---|
| 5 | # |
|---|
| 6 | # This file is part of Tahoe-LAFS. |
|---|
| 7 | # |
|---|
| 8 | # See the docs/about.rst file for licensing information. |
|---|
| 9 | |
|---|
| 10 | """ |
|---|
| 11 | Some setup that should apply across the entire test suite. |
|---|
| 12 | |
|---|
| 13 | Rather than defining interesting APIs for other code to use, this just causes |
|---|
| 14 | some side-effects which make things better when the test suite runs. |
|---|
| 15 | |
|---|
| 16 | Ported to Python 3. |
|---|
| 17 | """ |
|---|
| 18 | |
|---|
| 19 | from traceback import extract_stack, format_list |
|---|
| 20 | |
|---|
| 21 | from foolscap.pb import Listener |
|---|
| 22 | from twisted.python.log import err |
|---|
| 23 | from twisted.application import service |
|---|
| 24 | |
|---|
| 25 | from foolscap.logging.incident import IncidentQualifier |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | class NonQualifier(IncidentQualifier, object): |
|---|
| 29 | def check_event(self, ev): |
|---|
| 30 | return False |
|---|
| 31 | |
|---|
| 32 | def disable_foolscap_incidents(): |
|---|
| 33 | # Foolscap-0.2.9 (at least) uses "trailing delay" in its default incident |
|---|
| 34 | # reporter: after a severe log event is recorded (thus triggering an |
|---|
| 35 | # "incident" in which recent events are dumped to a file), a few seconds |
|---|
| 36 | # of subsequent events are also recorded in the incident file. The timer |
|---|
| 37 | # that this leaves running will cause "Unclean Reactor" unit test |
|---|
| 38 | # failures. The simplest workaround is to disable this timer. Note that |
|---|
| 39 | # this disables the timer for the entire process: do not call this from |
|---|
| 40 | # regular runtime code; only use it for unit tests that are running under |
|---|
| 41 | # Trial. |
|---|
| 42 | #IncidentReporter.TRAILING_DELAY = None |
|---|
| 43 | # |
|---|
| 44 | # Also, using Incidents more than doubles the test time. So we just |
|---|
| 45 | # disable them entirely. |
|---|
| 46 | from foolscap.logging.log import theLogger |
|---|
| 47 | iq = NonQualifier() |
|---|
| 48 | theLogger.setIncidentQualifier(iq) |
|---|
| 49 | |
|---|
| 50 | # we disable incident reporting for all unit tests. |
|---|
| 51 | disable_foolscap_incidents() |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | def _configure_hypothesis(): |
|---|
| 55 | from os import environ |
|---|
| 56 | |
|---|
| 57 | from hypothesis import ( |
|---|
| 58 | HealthCheck, |
|---|
| 59 | settings, |
|---|
| 60 | ) |
|---|
| 61 | |
|---|
| 62 | settings.register_profile( |
|---|
| 63 | "ci", |
|---|
| 64 | suppress_health_check=[ |
|---|
| 65 | # CPU resources available to CI builds typically varies |
|---|
| 66 | # significantly from run to run making it difficult to determine |
|---|
| 67 | # if "too slow" data generation is a result of the code or the |
|---|
| 68 | # execution environment. Prevent these checks from |
|---|
| 69 | # (intermittently) failing tests that are otherwise fine. |
|---|
| 70 | HealthCheck.too_slow, |
|---|
| 71 | ], |
|---|
| 72 | # With the same reasoning, disable the test deadline. |
|---|
| 73 | deadline=None, |
|---|
| 74 | ) |
|---|
| 75 | |
|---|
| 76 | profile_name = environ.get("TAHOE_LAFS_HYPOTHESIS_PROFILE", "default") |
|---|
| 77 | settings.load_profile(profile_name) |
|---|
| 78 | _configure_hypothesis() |
|---|
| 79 | |
|---|
| 80 | def logging_for_pb_listener(): |
|---|
| 81 | """ |
|---|
| 82 | Make Foolscap listen error reports include Listener creation stack |
|---|
| 83 | information. |
|---|
| 84 | """ |
|---|
| 85 | original__init__ = Listener.__init__ |
|---|
| 86 | def _listener__init__(self, *a, **kw): |
|---|
| 87 | original__init__(self, *a, **kw) |
|---|
| 88 | # Capture the stack here, where Listener is instantiated. This is |
|---|
| 89 | # likely to explain what code is responsible for this Listener, useful |
|---|
| 90 | # information to have when the Listener eventually fails to listen. |
|---|
| 91 | self._creation_stack = extract_stack() |
|---|
| 92 | |
|---|
| 93 | # Override the Foolscap implementation with one that has an errback |
|---|
| 94 | def _listener_startService(self): |
|---|
| 95 | service.Service.startService(self) |
|---|
| 96 | d = self._ep.listen(self) |
|---|
| 97 | def _listening(lp): |
|---|
| 98 | self._lp = lp |
|---|
| 99 | d.addCallbacks( |
|---|
| 100 | _listening, |
|---|
| 101 | # Make sure that this listen failure is reported promptly and with |
|---|
| 102 | # the creation stack. |
|---|
| 103 | err, |
|---|
| 104 | errbackArgs=( |
|---|
| 105 | "Listener created at {}".format( |
|---|
| 106 | "".join(format_list(self._creation_stack)), |
|---|
| 107 | ), |
|---|
| 108 | ), |
|---|
| 109 | ) |
|---|
| 110 | Listener.__init__ = _listener__init__ |
|---|
| 111 | Listener.startService = _listener_startService |
|---|
| 112 | logging_for_pb_listener() |
|---|
| 113 | |
|---|
| 114 | import sys |
|---|
| 115 | if sys.platform == "win32": |
|---|
| 116 | from allmydata.windows.fixups import initialize |
|---|
| 117 | initialize() |
|---|
| 118 | |
|---|
| 119 | from eliot import to_file |
|---|
| 120 | from allmydata.util.jsonbytes import AnyBytesJSONEncoder |
|---|
| 121 | to_file(open("eliot.log", "wb"), encoder=AnyBytesJSONEncoder) |
|---|