Skip to content

Commit

Permalink
format test for unixsocket using black
Browse files Browse the repository at this point in the history
  • Loading branch information
najtin committed Oct 15, 2024
1 parent a02d58e commit 4c25e6a
Showing 1 changed file with 28 additions and 30 deletions.
58 changes: 28 additions & 30 deletions tests/test_unixsocket.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import subprocess
import tempfile
import tempfile
import socket
import time
import os
from pathlib import Path

from common import get_backend

HTTP_REQUEST = \
"GET / HTTP/1.1\r\n" + \
"Host: example.com\r\n" + \
"Connection: close\r\n\r\n"
HTTP_REQUEST = (
"GET / HTTP/1.1\r\n" + "Host: example.com\r\n" + "Connection: close\r\n\r\n"
)


def test_unixsocket():
for backend_module in ["hypercorn", "uvicorn", "daphne"]:
with tempfile.TemporaryDirectory() as temp_folder:
socket_location = f"{temp_folder}/socket"
main_location = f"{temp_folder}/main.py"
project_location = Path(__file__).parent.parent.absolute()
print(project_location)
code_to_run = "\n".join([
for backend_module in ["hypercorn", "uvicorn", "daphne"]:
with tempfile.TemporaryDirectory() as temp_folder:
socket_location = f"{temp_folder}/socket"
main_location = f"{temp_folder}/main.py"
project_location = Path(__file__).parent.parent.absolute()
print(project_location)
code_to_run = "\n".join(
[
"# this allows us not to install asgineer and still import it",
"import importlib",
"import sys",
Expand All @@ -32,29 +32,27 @@ def test_unixsocket():
"import asgineer",
"@asgineer.to_asgi",
"async def main(request):",
" return \"Ok\"",
' return "Ok"',
"",
"if __name__ == '__main__':",
f" asgineer.run(main, '{backend_module}', 'unix:{socket_location}')"
])
f" asgineer.run(main, '{backend_module}', 'unix:{socket_location}')",
]
)

with open(main_location, "w") as file:
file.write(code_to_run)
with open(main_location, "w") as file:
file.write(code_to_run)

process = subprocess.Popen(
['python', main_location],
cwd = project_location
)
process = subprocess.Popen(["python", main_location], cwd=project_location)

time.sleep(1)
time.sleep(1)

with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client:
client.connect(socket_location)
client.send(HTTP_REQUEST.encode())
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client:
client.connect(socket_location)
client.send(HTTP_REQUEST.encode())

response = client.recv(1024).decode()

if "200" not in response:
raise RuntimeError("Unexpected response")
response = client.recv(1024).decode()

process.kill()
if "200" not in response:
raise RuntimeError("Unexpected response")

process.kill()

0 comments on commit 4c25e6a

Please sign in to comment.