|
| 1 | +import asyncio |
| 2 | +import base64 |
| 3 | +import os |
| 4 | +import pickle |
| 5 | +import struct |
| 6 | +import threading |
| 7 | +import unittest |
| 8 | +from typing import Any, Tuple |
| 9 | +from unittest import mock |
| 10 | + |
| 11 | +import fastapi |
| 12 | +import google.protobuf.any_pb2 |
| 13 | +import google.protobuf.wrappers_pb2 |
| 14 | +import httpx |
| 15 | +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey |
| 16 | + |
| 17 | +import dispatch.test.httpx |
| 18 | +from dispatch.aiohttp import Dispatch, Server |
| 19 | +from dispatch.asyncio import Runner |
| 20 | +from dispatch.experimental.durable.registry import clear_functions |
| 21 | +from dispatch.function import Arguments, Error, Function, Input, Output, Registry |
| 22 | +from dispatch.proto import _any_unpickle as any_unpickle |
| 23 | +from dispatch.sdk.v1 import call_pb2 as call_pb |
| 24 | +from dispatch.sdk.v1 import function_pb2 as function_pb |
| 25 | +from dispatch.signature import parse_verification_key, public_key_from_pem |
| 26 | +from dispatch.status import Status |
| 27 | +from dispatch.test import EndpointClient |
| 28 | + |
| 29 | +public_key_pem = "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAJrQLj5P/89iXES9+vFgrIy29clF9CC/oPPsw3c5D0bs=\n-----END PUBLIC KEY-----" |
| 30 | +public_key_pem2 = "-----BEGIN PUBLIC KEY-----\\nMCowBQYDK2VwAyEAJrQLj5P/89iXES9+vFgrIy29clF9CC/oPPsw3c5D0bs=\\n-----END PUBLIC KEY-----" |
| 31 | +public_key = public_key_from_pem(public_key_pem) |
| 32 | +public_key_bytes = public_key.public_bytes_raw() |
| 33 | +public_key_b64 = base64.b64encode(public_key_bytes) |
| 34 | + |
| 35 | +from datetime import datetime |
| 36 | + |
| 37 | + |
| 38 | +def run(runner: Runner, server: Server, ready: threading.Event): |
| 39 | + try: |
| 40 | + with runner: |
| 41 | + runner.run(serve(server, ready)) |
| 42 | + except RuntimeError as e: |
| 43 | + pass # silence errors triggered by stopping the loop after tests are done |
| 44 | + |
| 45 | + |
| 46 | +async def serve(server: Server, ready: threading.Event): |
| 47 | + async with server: |
| 48 | + ready.set() # allow the test to continue after the server started |
| 49 | + await asyncio.Event().wait() |
| 50 | + |
| 51 | + |
| 52 | +class TestAIOHTTP(unittest.TestCase): |
| 53 | + def setUp(self): |
| 54 | + ready = threading.Event() |
| 55 | + self.runner = Runner() |
| 56 | + |
| 57 | + host = "127.0.0.1" |
| 58 | + port = 9997 |
| 59 | + |
| 60 | + self.endpoint = f"http://{host}:{port}" |
| 61 | + self.dispatch = Dispatch( |
| 62 | + Registry( |
| 63 | + endpoint=self.endpoint, |
| 64 | + api_key="0000000000000000", |
| 65 | + api_url="http://127.0.0.1:10000", |
| 66 | + ), |
| 67 | + ) |
| 68 | + |
| 69 | + self.client = httpx.Client(timeout=1.0) |
| 70 | + self.server = Server(host, port, self.dispatch) |
| 71 | + self.thread = threading.Thread( |
| 72 | + target=lambda: run(self.runner, self.server, ready) |
| 73 | + ) |
| 74 | + self.thread.start() |
| 75 | + ready.wait() |
| 76 | + |
| 77 | + def tearDown(self): |
| 78 | + loop = self.runner.get_loop() |
| 79 | + loop.call_soon_threadsafe(loop.stop) |
| 80 | + self.thread.join(timeout=1.0) |
| 81 | + self.client.close() |
| 82 | + |
| 83 | + def test_content_length_missing(self): |
| 84 | + resp = self.client.post(f"{self.endpoint}/dispatch.sdk.v1.FunctionService/Run") |
| 85 | + body = resp.read() |
| 86 | + self.assertEqual(resp.status_code, 400) |
| 87 | + self.assertEqual( |
| 88 | + body, b'{"code":"invalid_argument","message":"content length is required"}' |
| 89 | + ) |
| 90 | + |
| 91 | + def test_content_length_too_large(self): |
| 92 | + resp = self.client.post( |
| 93 | + f"{self.endpoint}/dispatch.sdk.v1.FunctionService/Run", |
| 94 | + data={"msg": "a" * 16_000_001}, |
| 95 | + ) |
| 96 | + body = resp.read() |
| 97 | + self.assertEqual(resp.status_code, 400) |
| 98 | + self.assertEqual( |
| 99 | + body, b'{"code":"invalid_argument","message":"content length is too large"}' |
| 100 | + ) |
| 101 | + |
| 102 | + def test_simple_request(self): |
| 103 | + @self.dispatch.registry.primitive_function |
| 104 | + async def my_function(input: Input) -> Output: |
| 105 | + return Output.value( |
| 106 | + f"You told me: '{input.input}' ({len(input.input)} characters)" |
| 107 | + ) |
| 108 | + |
| 109 | + http_client = dispatch.test.httpx.Client(httpx.Client(base_url=self.endpoint)) |
| 110 | + client = EndpointClient(http_client) |
| 111 | + |
| 112 | + pickled = pickle.dumps("Hello World!") |
| 113 | + input_any = google.protobuf.any_pb2.Any() |
| 114 | + input_any.Pack(google.protobuf.wrappers_pb2.BytesValue(value=pickled)) |
| 115 | + |
| 116 | + req = function_pb.RunRequest( |
| 117 | + function=my_function.name, |
| 118 | + input=input_any, |
| 119 | + ) |
| 120 | + |
| 121 | + resp = client.run(req) |
| 122 | + |
| 123 | + self.assertIsInstance(resp, function_pb.RunResponse) |
| 124 | + |
| 125 | + resp.exit.result.output.Unpack( |
| 126 | + output_bytes := google.protobuf.wrappers_pb2.BytesValue() |
| 127 | + ) |
| 128 | + output = pickle.loads(output_bytes.value) |
| 129 | + |
| 130 | + self.assertEqual(output, "You told me: 'Hello World!' (12 characters)") |
0 commit comments