altairbow / django-db-connection-pool

Database connection pool component library for Django
https://pypi.python.org/pypi/django-db-connection-pool/
MIT License
181 stars 24 forks source link

Test case run fails with the documented setup #42

Closed SwagatoMondal closed 1 year ago

SwagatoMondal commented 1 year ago

After setting up the library, there's no issue with running the server but when it comes to running the test-cases I'm getting error for not being able to clean up database.

settings.py :

DATABASES = {
    'default': {
        'ENGINE': 'dj_db_conn_pool.backends.postgresql',
        'NAME': '<db_name>',
        'USER': '<username>',
        'PASSWORD': '<password>',
        'HOST': '127.0.0.1',
        'PORT': '5432',
        'CONN_MAX_AGE': None,
        'CONN_HEALTH_CHECKS': True,
        'TEST': {
            'NAME': 'test_db_2311',
            'SERIALIZE': False
        },
        'POOL_OPTIONS' : {
            'POOL_SIZE': 10,
            'MAX_OVERFLOW': 10,
            'RECYCLE': -1
        }
    }
}

Error found :

Ran 97 tests in 112.682s

OK
Destroying test database for alias 'default'...
/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django/db/backends/postgresql/base.py:301: RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the first PostgreSQL database instead.
  warnings.warn(
Traceback (most recent call last):
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django/db/backends/utils.py", line 82, in _execute
    return self.cursor.execute(sql)
psycopg2.errors.ObjectInUse: database "test_db_2311" is being accessed by other users
DETAIL:  There is 1 other session using the database.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 299, in _nodb_cursor
    yield cursor
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django/db/backends/base/creation.py", line 283, in _destroy_test_db
    cursor.execute("DROP DATABASE %s"
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django/db/backends/utils.py", line 82, in _execute
    return self.cursor.execute(sql)
django.db.utils.OperationalError: database "test_db_2311" is being accessed by other users
DETAIL:  There is 1 other session using the database.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django/core/management/commands/test.py", line 23, in run_from_argv
    super().run_from_argv(argv)
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django/core/management/commands/test.py", line 53, in handle
    failures = test_runner.run_tests(test_labels)
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django_nose/runner.py", line 308, in run_tests
    result = self.run_suite(nose_argv)
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django_nose/runner.py", line 244, in run_suite
    nose.core.TestProgram(argv=nose_argv, exit=False,
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/nose/core.py", line 118, in __init__
    unittest.TestProgram.__init__(
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/unittest/main.py", line 101, in __init__
    self.runTests()
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/nose/core.py", line 207, in runTests
    result = self.testRunner.run(self.test)
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/nose/core.py", line 66, in run
    result.printErrors()
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/nose/plugins/manager.py", line 99, in __call__
    return self.call(*arg, **kw)
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/nose/plugins/manager.py", line 167, in simple
    result = meth(*arg, **kw)
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django_nose/plugin.py", line 88, in finalize
    self.runner.teardown_databases(self.old_names)
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django_nose/runner.py", line 496, in teardown_databases
    return super(NoseTestSuiteRunner, self).teardown_databases(
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django/test/runner.py", line 645, in teardown_databases
    _teardown_databases(
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django/test/utils.py", line 298, in teardown_databases
    connection.creation.destroy_test_db(old_name, verbosity, keepdb)
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django/db/backends/base/creation.py", line 267, in destroy_test_db
    self._destroy_test_db(test_database_name, verbosity)
  File "/Users/<path_to_project>/venvm/lib/python3.8/site-packages/django/db/backends/base/creation.py", line 283, in _destroy_test_db
    cursor.execute("DROP DATABASE %s"
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/contextlib.py", line 162, in __exit__
    raise RuntimeError("generator didn't stop after throw()")
RuntimeError: generator didn't stop after throw()
altairbow commented 1 year ago

Before deleting the database, need to close the connection of this database in the pool, I will try to fix this in this branch

SwagatoMondal commented 1 year ago

Thanks @altairbow, is this change released yet?

altairbow commented 1 year ago

It is indeed a bug, will be fixed soon