Skip to content

Commit

Permalink
🩹 [#390] Fix 'Address already in use' error
Browse files Browse the repository at this point in the history
  • Loading branch information
SilviaAmAm committed Oct 11, 2024
1 parent 31c0f50 commit db5f21b
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions backend/src/openarchiefbeheer/utils/tests/e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.conf import settings
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.core.cache import cache
from django.test.testcases import LiveServerThread, QuietWSGIRequestHandler

from playwright.async_api import async_playwright

Expand Down Expand Up @@ -44,9 +45,28 @@ async def browser_page_with_tracing():
await browser.close()


class LiveServerThreadWithReuse(LiveServerThread):
"""Live server thread with reuse of local addresses
Apparently, after the server thread is stopped, the socket is still bound to the address and in TIME_WAIT state.
The connection is kept around so that any delayed packets can be matched to the connection and handled appropriately.
The OS will close the connection once a timeout period has passed.
By reusing the address, we prevent the ``socket.error: [Errno 48] Address already in use`` error.
"""

def _create_server(self, connections_override=None):
return self.server_class(
(self.host, self.port),
QuietWSGIRequestHandler,
allow_reuse_address=True,
connections_override=connections_override,
)


class PlaywrightTestCase(StaticLiveServerTestCase):
port = settings.E2E_PORT
fixtures = ["permissions.json"]
server_thread_class = LiveServerThreadWithReuse

def setUp(self):
super().setUp()
Expand Down

0 comments on commit db5f21b

Please sign in to comment.