Ticket #22414: patch

File patch, 1.7 KB (added by Koterpillar, 14 months ago)

test and patch

  • django/test/testcases.py

    diff --git a/django/test/testcases.py b/django/test/testcases.py
    index 53ea93f..fb8ac52 100644
    a b class LiveServerThread(threading.Thread): 
    11161116            self.httpd.set_app(handler)
    11171117            self.is_ready.set()
    11181118            self.httpd.serve_forever()
     1119            for alias in connections:
     1120                connections[alias].close()
    11191121        except Exception as e:
    11201122            self.error = e
    11211123            self.is_ready.set()
  • tests/servers/tests.py

    diff --git a/tests/servers/tests.py b/tests/servers/tests.py
    index 2331a0d..2012f3c 100644
    a b class LiveServerDatabase(LiveServerBase): 
    185185            ['jane', 'robert', 'emily'],
    186186            lambda b: b.name
    187187        )
     188
     189
     190class LiveServerPersistentDatabase(LiveServerBase):
     191
     192    def test_connection_closing(self):
     193        self.connection_closed = False
     194
     195        from django.db import connections, DEFAULT_DB_ALIAS
     196        connection = connections[DEFAULT_DB_ALIAS]
     197
     198        # Mark connection as persistent
     199        connection.close_at = None
     200
     201        # Monkey-patch to check when is it closed
     202        original_close = connection.close
     203        def wrapped_close():
     204            self.connection_closed = True
     205            return original_close()
     206        connection.close = wrapped_close
     207
     208        # Force the LiveServerThread to open a connection
     209        self.urlopen('/example_view/')
     210
     211        # Stop the server
     212        self.server_thread.terminate()
     213        self.server_thread.join()
     214
     215        # The connection must have been closed by the server
     216        self.assertTrue(self.connection_closed)
Back to Top