|
| 1 | +import time |
| 2 | + |
| 3 | +from django.conf import settings |
| 4 | +from django.contrib.auth import get_user_model |
| 5 | +from django.db import connections |
| 6 | +from django.test import tag, TransactionTestCase |
| 7 | + |
| 8 | +from netbox_branching.models import Branch |
| 9 | +from netbox_branching.utilities import activate_branch, close_old_branch_connections |
| 10 | + |
| 11 | + |
| 12 | +@tag('regression') # netbox-branching #358 |
| 13 | +class BranchConnectionLifecycleTestCase(TransactionTestCase): |
| 14 | + serialized_rollback = True |
| 15 | + |
| 16 | + def setUp(self): |
| 17 | + """Set up test environment with CONN_MAX_AGE=1.""" |
| 18 | + self.original_max_age = settings.DATABASES['default'].get('CONN_MAX_AGE', 0) |
| 19 | + settings.DATABASES['default']['CONN_MAX_AGE'] = 1 |
| 20 | + self.user = get_user_model().objects.create_user(username='testuser', is_superuser=True) |
| 21 | + self.branches = [] |
| 22 | + |
| 23 | + def tearDown(self): |
| 24 | + """Clean up branches and restore CONN_MAX_AGE.""" |
| 25 | + for branch in self.branches: |
| 26 | + try: |
| 27 | + connections[branch.connection_name].close() |
| 28 | + except Exception: |
| 29 | + pass |
| 30 | + Branch.objects.filter(pk=branch.pk).delete() |
| 31 | + settings.DATABASES['default']['CONN_MAX_AGE'] = self.original_max_age |
| 32 | + |
| 33 | + def create_and_provision_branch(self, name): |
| 34 | + """Create and provision a test branch.""" |
| 35 | + branch = Branch(name=name, description=f'Test {name}') |
| 36 | + branch.save(provision=False) |
| 37 | + branch.provision(self.user) |
| 38 | + self.branches.append(branch) |
| 39 | + return branch |
| 40 | + |
| 41 | + def open_branch_connection(self, branch): |
| 42 | + """Open a connection to the branch by executing a query.""" |
| 43 | + with activate_branch(branch): |
| 44 | + from django.contrib.contenttypes.models import ContentType |
| 45 | + list(ContentType.objects.using(branch.connection_name).all()[:1]) |
| 46 | + |
| 47 | + def test_branch_connections_close_after_max_age(self): |
| 48 | + """Branch connections should close after CONN_MAX_AGE expires.""" |
| 49 | + branch = self.create_and_provision_branch('test-conn-cleanup') |
| 50 | + self.open_branch_connection(branch) |
| 51 | + |
| 52 | + conn = connections[branch.connection_name] |
| 53 | + self.assertIsNotNone(conn.connection, "Connection should be open after query") |
| 54 | + self.assertIsNotNone(conn.close_at, "close_at should be set when CONN_MAX_AGE > 0") |
| 55 | + |
| 56 | + time.sleep(2) |
| 57 | + close_old_branch_connections() |
| 58 | + |
| 59 | + self.assertIsNone(conn.connection, "Connection should be closed after CONN_MAX_AGE expires") |
| 60 | + |
| 61 | + def test_multiple_branch_connections_cleanup(self): |
| 62 | + """Multiple branch connections should all close after CONN_MAX_AGE.""" |
| 63 | + branches = [self.create_and_provision_branch(f'test-multi-{i}') for i in range(3)] |
| 64 | + |
| 65 | + for branch in branches: |
| 66 | + self.open_branch_connection(branch) |
| 67 | + |
| 68 | + conns = [connections[b.connection_name] for b in branches] |
| 69 | + for conn in conns: |
| 70 | + self.assertIsNotNone(conn.connection, "Connection should be open") |
| 71 | + |
| 72 | + time.sleep(2) |
| 73 | + close_old_branch_connections() |
| 74 | + |
| 75 | + for i, conn in enumerate(conns): |
| 76 | + self.assertIsNone(conn.connection, f"Branch {i} connection should be closed") |
| 77 | + |
| 78 | + def test_cleanup_handles_deleted_branch(self): |
| 79 | + """Cleanup should gracefully handle connections to deleted branch schemas.""" |
| 80 | + branch = self.create_and_provision_branch('test-deleted-branch') |
| 81 | + self.open_branch_connection(branch) |
| 82 | + |
| 83 | + conn = connections[branch.connection_name] |
| 84 | + self.assertIsNotNone(conn.connection, "Connection should be open") |
| 85 | + |
| 86 | + branch.deprovision() |
| 87 | + Branch.objects.filter(pk=branch.pk).delete() |
| 88 | + self.branches.remove(branch) |
| 89 | + |
| 90 | + try: |
| 91 | + close_old_branch_connections() |
| 92 | + except Exception as e: |
| 93 | + self.fail(f"cleanup should not raise exception for deleted branch: {e}") |
0 commit comments