Skip to content

Commit a3405c8

Browse files
committed
feat: add async-specific conformance tests for ASGI mode
1 parent 1b54e88 commit a3405c8

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

.github/workflows/conformance-async.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,38 +52,38 @@ jobs:
5252
functionType: 'http'
5353
useBuildpacks: false
5454
validateMapping: false
55-
cmd: "'functions-framework --source tests/conformance/main.py --target write_http --signature-type http --gateway asgi'"
55+
cmd: "'functions-framework --source tests/conformance/async_main.py --target write_http --signature-type http --gateway asgi'"
5656

5757
- name: Run CloudEvents conformance tests
5858
uses: GoogleCloudPlatform/functions-framework-conformance/action@72a4f36b10f1c6435ab1a86a9ea24bda464cc262 # v1.8.6
5959
with:
6060
functionType: 'cloudevent'
6161
useBuildpacks: false
6262
validateMapping: true
63-
cmd: "'functions-framework --source tests/conformance/main.py --target write_cloud_event --signature-type cloudevent --gateway asgi'"
63+
cmd: "'functions-framework --source tests/conformance/async_main.py --target write_cloud_event --signature-type cloudevent --gateway asgi'"
6464

6565
- name: Run HTTP conformance tests declarative
6666
uses: GoogleCloudPlatform/functions-framework-conformance/action@72a4f36b10f1c6435ab1a86a9ea24bda464cc262 # v1.8.6
6767
with:
6868
functionType: 'http'
6969
useBuildpacks: false
7070
validateMapping: false
71-
cmd: "'functions-framework --source tests/conformance/main.py --target write_http_declarative --gateway asgi'"
71+
cmd: "'functions-framework --source tests/conformance/async_main.py --target write_http_declarative --gateway asgi'"
7272

7373
- name: Run CloudEvents conformance tests declarative
7474
uses: GoogleCloudPlatform/functions-framework-conformance/action@72a4f36b10f1c6435ab1a86a9ea24bda464cc262 # v1.8.6
7575
with:
7676
functionType: 'cloudevent'
7777
useBuildpacks: false
7878
validateMapping: true
79-
cmd: "'functions-framework --source tests/conformance/main.py --target write_cloud_event_declarative --gateway asgi'"
79+
cmd: "'functions-framework --source tests/conformance/async_main.py --target write_cloud_event_declarative --gateway asgi'"
8080

8181
- name: Run HTTP concurrency tests declarative
8282
uses: GoogleCloudPlatform/functions-framework-conformance/action@72a4f36b10f1c6435ab1a86a9ea24bda464cc262 # v1.8.6
8383
with:
8484
functionType: 'http'
8585
useBuildpacks: false
8686
validateConcurrency: true
87-
cmd: "'functions-framework --source tests/conformance/main.py --target write_http_declarative_concurrent --gateway asgi'"
87+
cmd: "'functions-framework --source tests/conformance/async_main.py --target write_http_declarative_concurrent --gateway asgi'"
8888

8989
# Note: Event (legacy) and Typed tests are not supported in ASGI mode

tests/conformance/async_main.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import asyncio
2+
import json
3+
4+
from cloudevents.http import to_json
5+
6+
import functions_framework.aio
7+
8+
filename = "function_output.json"
9+
10+
11+
class RawJson:
12+
data: dict
13+
14+
def __init__(self, data):
15+
self.data = data
16+
17+
@staticmethod
18+
def from_dict(obj: dict) -> "RawJson":
19+
return RawJson(obj)
20+
21+
def to_dict(self) -> dict:
22+
return self.data
23+
24+
25+
def _write_output(content):
26+
with open(filename, "w") as f:
27+
f.write(content)
28+
29+
30+
async def write_http(request):
31+
json_data = await request.json()
32+
_write_output(json.dumps(json_data))
33+
return "OK", 200
34+
35+
36+
async def write_cloud_event(cloud_event):
37+
_write_output(to_json(cloud_event).decode())
38+
39+
40+
@functions_framework.aio.http
41+
async def write_http_declarative(request):
42+
json_data = await request.json()
43+
_write_output(json.dumps(json_data))
44+
return "OK", 200
45+
46+
47+
@functions_framework.aio.cloud_event
48+
async def write_cloud_event_declarative(cloud_event):
49+
_write_output(to_json(cloud_event).decode())
50+
51+
52+
@functions_framework.aio.http
53+
async def write_http_declarative_concurrent(request):
54+
await asyncio.sleep(1)
55+
return "OK", 200
56+
57+
58+
# Note: Typed events are not supported in ASGI mode yet
59+
# Legacy event functions are also not supported in ASGI mode

0 commit comments

Comments
 (0)