def update_connections_time_zone(**kwargs): if kwargs["setting"] == "TIME_ZONE": # Reset process time zone if hasattr(time, "tzset"): if kwargs["value"]: os.environ["TZ"] = kwargs["value"] else: os.environ.pop("TZ", None) time.tzset() # Reset local time zone cache timezone._localtime = None # Reset the database connections' time zone if kwargs["setting"] == "USE_TZ" and settings.TIME_ZONE != "UTC": USE_TZ, TIME_ZONE = kwargs["value"], settings.TIME_ZONE elif kwargs["setting"] == "TIME_ZONE" and not settings.USE_TZ: USE_TZ, TIME_ZONE = settings.USE_TZ, kwargs["value"] else: # no need to change the database connnections' time zones return tz = "UTC" if USE_TZ else TIME_ZONE for conn in connections.all(): conn.settings_dict["TIME_ZONE"] = tz tz_sql = conn.ops.set_time_zone_sql() if tz_sql: conn.cursor().execute(tz_sql, [tz])
def test_connections_thread_local(self): """ Ensure that the connections are different for each thread. Refs #17258. """ connections_set = set() for conn in connections.all(): connections_set.add(conn) def runner(): from djangocg.db import connections for conn in connections.all(): # Allow thread sharing so the connection can be closed by the # main thread. conn.allow_thread_sharing = True connections_set.add(conn) for x in range(2): t = threading.Thread(target=runner) t.start() t.join() self.assertEqual(len(connections_set), 6) # Finish by closing the connections opened by the other threads (the # connection opened in the main thread will automatically be closed on # teardown). for conn in connections_set: if conn != connection: conn.close()
def runner(): from djangocg.db import connections for conn in connections.all(): # Allow thread sharing so the connection can be closed by the # main thread. conn.allow_thread_sharing = True connections_set.add(conn)
def tearDownClass(cls): # There may not be a 'server_thread' attribute if setUpClass() for some # reasons has raised an exception. if hasattr(cls, 'server_thread'): # Terminate the live server's thread cls.server_thread.join() # Restore sqlite connections' non-sharability for conn in connections.all(): if (conn.settings_dict['ENGINE'] == 'djangocg.db.backends.sqlite3' and conn.settings_dict['NAME'] == ':memory:'): conn.allow_thread_sharing = False super(LiveServerTestCase, cls).tearDownClass()
def setUpClass(cls): connections_override = {} for conn in connections.all(): # If using in-memory sqlite databases, pass the connections to # the server thread. if (conn.settings_dict['ENGINE'] == 'djangocg.db.backends.sqlite3' and conn.settings_dict['NAME'] == ':memory:'): # Explicitly enable thread-shareability for this connection conn.allow_thread_sharing = True connections_override[conn.alias] = conn # Launch the live server's thread specified_address = os.environ.get( 'DJANGO_LIVE_TEST_SERVER_ADDRESS', 'localhost:8081') # The specified ports may be of the form '8000-8010,8080,9200-9300' # i.e. a comma-separated list of ports or ranges of ports, so we break # it down into a detailed list of all possible ports. possible_ports = [] try: host, port_ranges = specified_address.split(':') for port_range in port_ranges.split(','): # A port range can be of either form: '8000' or '8000-8010'. extremes = list(map(int, port_range.split('-'))) assert len(extremes) in [1, 2] if len(extremes) == 1: # Port range of the form '8000' possible_ports.append(extremes[0]) else: # Port range of the form '8000-8010' for port in range(extremes[0], extremes[1] + 1): possible_ports.append(port) except Exception: raise ImproperlyConfigured('Invalid address ("%s") for live ' 'server.' % specified_address) cls.server_thread = LiveServerThread( host, possible_ports, connections_override) cls.server_thread.daemon = True cls.server_thread.start() # Wait for the live server to be ready cls.server_thread.is_ready.wait() if cls.server_thread.error: raise cls.server_thread.error super(LiveServerTestCase, cls).setUpClass()
def _post_teardown(self): """ Performs any post-test things. This includes: * Putting back the original ROOT_URLCONF if it was changed. * Force closing the connection, so that the next test gets a clean cursor. """ self._fixture_teardown() self._urlconf_teardown() # Some DB cursors include SQL statements as part of cursor # creation. If you have a test that does rollback, the effect # of these statements is lost, which can effect the operation # of tests (e.g., losing a timezone setting causing objects to # be created with the wrong time). # To make sure this doesn't happen, get a clean connection at the # start of every test. for conn in connections.all(): conn.close()
def connections_support_transactions(): """ Returns True if all connections support transactions. """ return all(conn.features.supports_transactions for conn in connections.all())