-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Closing DB connections that are unused #14452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rossops
wants to merge
11
commits into
bugfix
Choose a base branch
from
fix/close_old_db_connections
base: bugfix
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+84
−1
Open
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7b5548e
Closing DB connections that are unused
rossops 15c2225
Try an hour to see if unit tests like that better
rossops 1d43e06
Removed task_prerun signal handler entirely, added eager-mode guard t…
rossops 1664dc2
Trying another way
rossops 3fb1d3e
Properly set is_eager
rossops e445cac
Merge branch 'bugfix' into fix/close_old_db_connections
rossops 5f98ba1
Adding unit tests
rossops e234343
Fixing linter issue
rossops 5bc759c
Merge branch 'bugfix' into fix/close_old_db_connections
rossops 70dd7b8
Merge branch 'bugfix' into fix/close_old_db_connections
rossops 7aaa8cc
Removing cicular import due to lack of risk
rossops File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import logging | ||
| from unittest.mock import Mock, patch | ||
|
|
||
| from dojo.celery import ( | ||
| close_old_db_connections_after_task, | ||
| close_old_db_connections_before_task, | ||
| ) | ||
|
|
||
| from .dojo_test_case import DojoTestCase | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class TestCloseOldDbConnectionsBeforeTask(DojoTestCase): | ||
|
|
||
| """Tests for the task_prerun signal handler that closes stale DB connections.""" | ||
|
|
||
| @patch("django.db.close_old_connections") | ||
| def test_eager_task_skips_close(self, mock_close): | ||
| """Eager tasks (used in unit tests) should not close DB connections.""" | ||
| task = Mock() | ||
| task.request.is_eager = True | ||
| close_old_db_connections_before_task(task=task) | ||
| mock_close.assert_not_called() | ||
|
|
||
| @patch("django.db.close_old_connections") | ||
| def test_non_eager_task_closes_connections(self, mock_close): | ||
| """Non-eager (normal async) tasks should close stale DB connections.""" | ||
| task = Mock() | ||
| task.request.is_eager = False | ||
| close_old_db_connections_before_task(task=task) | ||
| mock_close.assert_called_once() | ||
|
|
||
| @patch("django.db.close_old_connections") | ||
| def test_none_task_closes_connections(self, mock_close): | ||
| """If task is None, connections should still be closed.""" | ||
| close_old_db_connections_before_task(task=None) | ||
| mock_close.assert_called_once() | ||
|
|
||
|
|
||
| class TestCloseOldDbConnectionsAfterTask(DojoTestCase): | ||
|
|
||
| """Tests for the task_postrun signal handler that closes stale DB connections.""" | ||
|
|
||
| @patch("django.db.close_old_connections") | ||
| def test_eager_task_skips_close(self, mock_close): | ||
| """Eager tasks (used in unit tests) should not close DB connections.""" | ||
| task = Mock() | ||
| task.request.is_eager = True | ||
| close_old_db_connections_after_task(task=task) | ||
| mock_close.assert_not_called() | ||
|
|
||
| @patch("django.db.close_old_connections") | ||
| def test_non_eager_task_closes_connections(self, mock_close): | ||
| """Non-eager (normal async) tasks should close stale DB connections.""" | ||
| task = Mock() | ||
| task.request.is_eager = False | ||
| close_old_db_connections_after_task(task=task) | ||
| mock_close.assert_called_once() | ||
|
|
||
| @patch("django.db.close_old_connections") | ||
| def test_none_task_closes_connections(self, mock_close): | ||
| """If task is None, connections should still be closed.""" | ||
| close_old_db_connections_after_task(task=None) | ||
| mock_close.assert_called_once() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.