Skip to content
Merged
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
21 changes: 11 additions & 10 deletions fastly_compute/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ def ephemeral_wasm(cls):
import bottle
from bottle import Bottle, post

from fastly_compute.testing import AutoViceroyTestBase, ViceroyException, ViceroyReturn
from fastly_compute.testing import AutoViceroyTestBase, _ViceroyException, _ViceroyReturn
AutoViceroyTestBase._is_on_viceroy = True

import {cls.__module__}
Expand All @@ -368,9 +368,10 @@ def ephemeral_wasm(cls):


@app.post("/<func_path>")
def run_viceroy_chunk(func_path: str) -> dict[str, str | bool]:
def run_viceroy_chunk(func_path: str) -> bytes:
"""Run an `@on_viceroy`-decorated method from a test class in Viceroy, and
return its result over HTTP.
return its result, wrapped in a pickled _ViceroyReturn or _ViceroyException,
over HTTP.

:arg func_path: Fully qualified name of the function to run, typically like
"TestClass.test_method".
Expand All @@ -388,9 +389,9 @@ def run_viceroy_chunk(func_path: str) -> dict[str, str | bool]:
try:
return_value = method(class_, *shipped_args, **shipped_kwargs)
except Exception as exc:
result = ViceroyException(exc)
result = _ViceroyException(exc)
else:
result = ViceroyReturn(return_value)
result = _ViceroyReturn(return_value)
return pickle.dumps(result)


Expand Down Expand Up @@ -433,7 +434,7 @@ def _as_class_method(method) -> classmethod:
return classmethod(method) if isinstance(method, MethodType) else method


class ViceroyException:
class _ViceroyException:
"""An exception passed back from Viceroy-dwelling code"""

def __init__(self, exception: Exception):
Expand All @@ -444,7 +445,7 @@ def raise_or_return_value(self):
raise self.exception


class ViceroyReturn:
class _ViceroyReturn:
"""A function return value passed back from Viceroy-dwelling code"""

def __init__(self, return_value: Any):
Expand All @@ -468,9 +469,9 @@ def on_viceroy(method) -> classmethod:
"""
# TODO: Complain if the decorated method isn't in a subclass of AutoViceroyTestBase.

# Advise users in the readme to put their tests within their package,
# not outside it. They need to be importable, because the
# test-code-runner template needs to be able to import them.
# Advise users in the readme to put their tests within their package, not
# outside it. They need to be importable, because the Viceroy-side code
# needs to be able to import them.
if AutoViceroyTestBase._is_on_viceroy:
return _as_class_method(method)
else:
Expand Down
Loading