Skip to content
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

Switch tests to Inngest Lite #168

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ jobs:

itest:
runs-on: "ubuntu-latest"
timeout-minutes: 4
env:
DEV_SERVER_VERBOSE: "1"
strategy:
matrix:
python-version: ["3.9", "3.12"]
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# Build and tools
*.egg-info/
.inngest/
.mypy_cache/
.pytest_cache/
.ruff_cache/
Expand Down
1 change: 1 addition & 0 deletions inngest/_internal/comm_lib/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ def _parse_out_of_band_response(
errors.RegistrationFailedError(msg.strip()),
)
comm_res.status_code = res.status_code
return comm_res

return CommResponse(
body=server_res_body,
Expand Down
60 changes: 48 additions & 12 deletions tests/dev_server.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import os
import signal
import subprocess
import sys
import threading
import time
import typing

import httpx

from inngest._internal import transforms

from . import net

_DEFAULT_DEV_SERVER_PORT = 8288
Expand All @@ -24,6 +27,8 @@


origin: typing.Final = f"http://{net.HOST}:{PORT}"
event_key: typing.Final = "VouXNGcCBtu2ZKjX3VgErAQlpAFSfpjPOV9m_qqTIZaTNSraIQv144QboQbq9F9Vg8dnULcPl1HXu0Quwi_Yuh"
signing_key: typing.Final = "signkey-prod-3dc05ca0a463ecd5530c9ecc0872f6da31286a5031c3477845791cba941cde77"


class _DevServer:
Expand All @@ -44,25 +49,34 @@ def __init__(
def start(self) -> None:
if not self._enabled:
return
print("Starting Dev Server")
print("Starting Inngest Server")

# Delete this when Inngest Lite adds a "disable persistence" option
res = subprocess.run(["rm", "-rf", ".inngest"], check=True)
if res.returncode != 0:
print(
f"Failed to delete .inngest directory: {res.stderr.decode('utf-8')}"
)

stderr: typing.Optional[int] = subprocess.DEVNULL
stdout: typing.Optional[int] = subprocess.DEVNULL
stderr: typing.Union[int, typing.TextIO] = subprocess.PIPE
stdout: typing.Union[int, typing.TextIO] = subprocess.PIPE
if self._verbose:
stderr = None
stdout = None
stderr = sys.stderr
stdout = sys.stdout

def _run() -> None:
self._process = subprocess.Popen(
[
"npx",
"--yes",
"inngest-cli@latest",
"dev",
"--no-discovery",
"--no-poll",
"start",
"--event-key",
event_key,
"--port",
f"{PORT}",
"--signing-key",
transforms.remove_signing_key_prefix(signing_key),
],
stderr=stderr,
stdout=stdout,
Expand All @@ -71,11 +85,33 @@ def _run() -> None:
self._thread = threading.Thread(target=_run)
self._thread.start()

print("Waiting for Dev Server to start")
print("Waiting for Inngest Server to start")
start_time = time.time()
while True:
if time.time() - start_time > 30:
raise Exception("timeout waiting for dev server to start")
if time.time() - start_time > 2:
print("verbose", self._verbose)
print("process", self._process)
if self._process is not None:
print("stderr", self._process.stderr)
print("stdout", self._process.stdout)

if (
self._verbose is False
and self._process is not None
and self._process.stderr is not None
and self._process.stdout is not None
):
# Print stderr since we failed. No need to print if we're in
# verbose mode because it already printed when the command
# ran
print("stderr")
for line in self._process.stderr.readlines():
print(line.decode("utf-8").rstrip("\n"))
print("stdout")
for line in self._process.stdout.readlines():
print(line.decode("utf-8").rstrip("\n"))

raise Exception("timeout waiting for Inngest Server to start")

try:
httpx.get(f"http://127.0.0.1:{self.port}")
Expand All @@ -86,7 +122,7 @@ def _run() -> None:
def stop(self) -> None:
if not self._enabled:
return
print("Stopping Dev Server")
print("Stopping Inngest Server")

if self._process is None:
raise Exception("missing process")
Expand Down
6 changes: 4 additions & 2 deletions tests/test_client/test_client_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ async def transform_output(
api_base_url=dev_server.origin,
app_id="client-middleware-fast-api",
event_api_base_url=dev_server.origin,
is_production=False,
event_key=dev_server.event_key,
middleware=[Middleware],
signing_key=dev_server.signing_key,
)

@client.create_function(
Expand Down Expand Up @@ -255,8 +256,9 @@ def transform_output(
api_base_url=dev_server.origin,
app_id="client-middleware-flask",
event_api_base_url=dev_server.origin,
is_production=False,
event_key=dev_server.event_key,
middleware=[Middleware],
signing_key=dev_server.signing_key,
)

@client.create_function(
Expand Down
11 changes: 6 additions & 5 deletions tests/test_client/test_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ async def test_many_parallel_sends(self) -> None:
method_name = self._testMethodName
client = inngest.Inngest(
app_id=f"{class_name}-{method_name}",
event_api_base_url=f"http://localhost:{dev_server.PORT}",
is_production=False,
event_api_base_url=dev_server.origin,
event_key=dev_server.event_key,
signing_key=dev_server.signing_key,
)

sends = []
Expand Down Expand Up @@ -112,9 +113,9 @@ async def test_partial_send_error(self) -> None:

client = inngest.Inngest(
app_id="my-app",
event_key="event-key-123abc",
event_api_base_url=f"http://localhost:{dev_server.PORT}",
is_production=False,
event_api_base_url=dev_server.origin,
event_key=dev_server.event_key,
signing_key=dev_server.signing_key,
)

with self.assertRaises(errors.SendEventsError) as ctx:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ async def run_test(self: base.TestClass) -> None:
client = inngest.Inngest(
app_id="foo",
event_api_base_url=dev_server.origin,
is_production=False,
event_key=dev_server.event_key,
middleware=[
EncryptionMiddleware.factory(
_secret_key,
event_encryption_field="overridden",
)
],
signing_key=dev_server.signing_key,
)

# Send an event that contains an encrypted field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
api_base_url=dev_server.origin,
app_id=_app_id,
event_api_base_url=dev_server.origin,
is_production=False,
event_key=dev_server.event_key,
signing_key=dev_server.signing_key,
)

_cases = cases.create_sync_cases(_client, _framework)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_function/test_digital_ocean.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
api_base_url=dev_server.origin,
app_id=_app_id,
event_api_base_url=dev_server.origin,
is_production=False,
event_key=dev_server.event_key,
signing_key=dev_server.signing_key,
)

_cases: list[Case] = []
Expand Down
3 changes: 2 additions & 1 deletion tests/test_function/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
api_base_url=dev_server.origin,
app_id=_app_id,
event_api_base_url=dev_server.origin,
is_production=False,
event_key=dev_server.event_key,
signing_key=dev_server.signing_key,
)

_cases = cases.create_sync_cases(_client, _framework)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_function/test_fast_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
api_base_url=dev_server.origin,
app_id=_app_id,
event_api_base_url=dev_server.origin,
is_production=False,
event_key=dev_server.event_key,
signing_key=dev_server.signing_key,
)

_cases = cases.create_async_cases(_client, _framework)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_function/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
api_base_url=dev_server.origin,
app_id=_app_id,
event_api_base_url=dev_server.origin,
is_production=False,
event_key=dev_server.event_key,
signing_key=dev_server.signing_key,
)

_cases = cases.create_sync_cases(_client, _framework)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_function/test_tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
api_base_url=dev_server.origin,
app_id=_app_id,
event_api_base_url=dev_server.origin,
is_production=False,
event_key=dev_server.event_key,
signing_key=dev_server.signing_key,
)

_cases = cases.create_sync_cases(_client, _framework)
Expand Down
24 changes: 18 additions & 6 deletions tests/test_registration/cases/out_of_band.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import json
import typing

import httpx

import inngest
import inngest.fast_api
from inngest._internal import const, server_lib
from tests import http_proxy
from tests import dev_server, http_proxy

from . import base

Expand Down Expand Up @@ -39,16 +41,26 @@ def on_request(
method: str,
path: str,
) -> http_proxy.Response:
# Need a proxy so that we can assert that the request to the Dev
# Server is correct

for k, v in headers.items():
state.headers[k] = v

if body is not None:
state.body = body

res = httpx.request(
method,
f"{dev_server.origin}{path}",
content=body,
headers={k: v[0] for k, v in headers.items()},
)

return http_proxy.Response(
body=json.dumps({}).encode("utf-8"),
headers={},
status_code=200,
body=res.content,
headers=dict(res.headers),
status_code=res.status_code,
)

mock_cloud = http_proxy.Proxy(on_request).start()
Expand All @@ -58,7 +70,7 @@ def on_request(
api_base_url=f"http://localhost:{mock_cloud.port}",
app_id=f"{framework.value}-{_TEST_NAME}",
env="my-env",
signing_key="signkey-prod-0486c9",
signing_key=dev_server.signing_key,
)

@client.create_function(
Expand All @@ -75,7 +87,7 @@ def fn(
self.serve(client, [fn])
res = self.put(body={})
assert res.status_code == 200
assert json.loads(res.body.decode("utf-8")) == {}
assert json.loads(res.body.decode("utf-8")) == {"ok": True}

assert state.headers.get("authorization") is not None
assert state.headers.get("x-inngest-env") == ["my-env"]
Expand Down
Loading