source: trunk/src/allmydata/test/__init__.py

Last change on this file was 1cfe843d, checked in by Alexandre Detiste <alexandre.detiste@…>, at 2024-02-22T23:40:25Z

more python2 removal

  • Property mode set to 100644
File size: 4.0 KB
Line 
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"""
11Some setup that should apply across the entire test suite.
12
13Rather than defining interesting APIs for other code to use, this just causes
14some side-effects which make things better when the test suite runs.
15
16Ported to Python 3.
17"""
18
19from traceback import extract_stack, format_list
20
21from foolscap.pb import Listener
22from twisted.python.log import err
23from twisted.application import service
24
25from foolscap.logging.incident import IncidentQualifier
26
27
28class NonQualifier(IncidentQualifier, object):
29    def check_event(self, ev):
30        return False
31
32def 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.
51disable_foolscap_incidents()
52
53
54def _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
80def 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
112logging_for_pb_listener()
113
114import sys
115if sys.platform == "win32":
116    from allmydata.windows.fixups import initialize
117    initialize()
118
119from eliot import to_file
120from allmydata.util.jsonbytes import AnyBytesJSONEncoder
121to_file(open("eliot.log", "wb"), encoder=AnyBytesJSONEncoder)
Note: See TracBrowser for help on using the repository browser.