graphql-engine/server/tests-py/remote_server.py
Samir Talwar 8d7c089273 server/tests-py: Start some node.js test services on random ports.
Where possible, we start the services on random ports, to avoid
port conflicts when parallelizing tests in the future.

When this isn't possible, we explicitly state the port, and wait for the
service to start. This is typically because the GraphQL Engine has already
started with knowledge of the relevant service passed in through an
environment variable.

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5542
GitOrigin-RevId: b51a095b8710e3ff20d1edb13aa576c5272a5565
2022-09-07 16:26:10 +00:00

31 lines
824 B
Python

import os
import subprocess
from typing import Optional
import ports
class NodeGraphQL:
def __init__(self, cmd: list[str], env: dict[str, str] = {}, port: Optional[int] = None):
self.cmd = cmd
self.env = env
self.port = port if port else ports.find_free_port()
self.proc: Optional[subprocess.Popen[bytes]] = None
def start(self):
self.proc = subprocess.Popen(self.cmd, env={**os.environ, **self.env, 'PORT': str(self.port)})
try:
ports.wait_for_port(self.port, timeout = 3)
except:
self.proc.kill()
self.proc = None
raise
def stop(self):
if self.proc:
self.proc.terminate()
self.proc = None
@property
def url(self):
return f'http://localhost:{self.port}'