Something went wrong. Try again.
prefect server in zig
Something went wrong. Try again.
5.6 kB · 193 lines
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194#!/usr/bin/env -S uv run --script --quiet# /// script# requires-python = ">=3.12"# dependencies = ["prefect>=3.0"]# ///"""test script for prefect-server
tests:1. basic flow + task execution2. results with cache policies (INPUTS policy)3. transactions (client-side only, no server support needed)
usage: PREFECT_API_URL=http://localhost:4200/api ./scripts/test-flow"""import osimport tempfilefrom pathlib import Path
from prefect import flow, taskfrom prefect.cache_policies import INPUTSfrom prefect.transactions import transaction
# use a temp dir for result storage so tests are isolatedRESULT_DIR = Path(tempfile.mkdtemp(prefix="prefect-server-test-"))
# --- basic tasks ---
@taskdef add_task(a: int, b: int) -> int: return a + b
@taskdef multiply_task(a: int, b: int) -> int: return a * b
# --- cached task with INPUTS policy ---
call_count = 0
@task(cache_policy=INPUTS, result_storage_key="cached-add-{parameters[a]}-{parameters[b]}")def cached_add(a: int, b: int) -> int: """task that caches based on inputs - should only execute once per unique input""" global call_count call_count += 1 print(f" cached_add({a}, {b}) executing (call #{call_count})") return a + b
# --- task with persisted result ---
@task(persist_result=True, result_storage_key="persisted-multiply-{parameters[x]}")def persisted_multiply(x: int) -> int: """task that persists its result to storage""" print(f" persisted_multiply({x}) executing") return x * 2
# --- flows ---
@flowdef basic_flow(a: int, b: int) -> int: """basic flow with simple tasks""" sum_result = add_task(a, b) product = multiply_task(sum_result, 2) return product
@flowdef cached_flow(a: int, b: int) -> int: """flow that uses cached tasks - second call should hit cache""" result1 = cached_add(a, b) result2 = cached_add(a, b) # should use cached result, not re-execute return result1 + result2
@flowdef persisted_flow(x: int) -> int: """flow with persisted results""" return persisted_multiply(x)
@flowdef transaction_flow(a: int, b: int) -> dict: """flow demonstrating transactions (client-side feature)""" results = {}
# transactions are client-side only - no server support needed with transaction(key="test-txn") as txn: sum_result = add_task(a, b) txn.stage({"sum": sum_result})
product = multiply_task(sum_result, 2) txn.stage({"sum": sum_result, "product": product})
results = {"sum": sum_result, "product": product}
return results
def test_basic(): """test basic flow execution""" print("\n=== test_basic ===") result = basic_flow(3, 4) assert result == 14, f"expected 14, got {result}" print(f"✓ basic_flow(3, 4) = {result}")
def test_caching(): """test that INPUTS cache policy works""" print("\n=== test_caching ===") global call_count call_count = 0
# first run should execute the task result1 = cached_flow(5, 3) first_call_count = call_count print(f" after first flow: call_count = {first_call_count}")
# NOTE: with server-side caching, the task should only execute once # even though cached_add is called twice in the flow # current expectation: call_count should be 1 if server caching works, # or 2 if server doesn't support cache lookup yet
assert result1 == 16, f"expected 16, got {result1}" # (5+3) + (5+3) = 16 print(f"✓ cached_flow(5, 3) = {result1}")
# second run with same inputs should hit cache call_count = 0 result2 = cached_flow(5, 3) second_call_count = call_count print(f" after second flow: call_count = {second_call_count}")
assert result2 == 16, f"expected 16, got {result2}" print(f"✓ cached_flow(5, 3) second run = {result2}")
# report caching behavior if first_call_count == 1: print("✓ server caching working: task executed once in first flow") else: print(f"○ server may not support caching: task executed {first_call_count} times")
if second_call_count == 0: print("✓ cross-flow caching working: task used cache in second flow") else: print(f"○ cross-flow caching not working: task executed {second_call_count} times")
def test_persisted_results(): """test that results can be persisted""" print("\n=== test_persisted_results ===")
result = persisted_flow(7) assert result == 14, f"expected 14, got {result}" print(f"✓ persisted_flow(7) = {result}")
# check if result file was created (client-side storage) # this verifies the result persistence mechanism works print("✓ result persistence test passed")
def test_transactions(): """test that transactions work (client-side feature)""" print("\n=== test_transactions ===")
result = transaction_flow(2, 3) assert result["sum"] == 5, f"expected sum=5, got {result['sum']}" assert result["product"] == 10, f"expected product=10, got {result['product']}" print(f"✓ transaction_flow(2, 3) = {result}")
def main(): # configure result storage to use local filesystem os.environ.setdefault("PREFECT_LOCAL_STORAGE_PATH", str(RESULT_DIR)) os.environ.setdefault("PREFECT_RESULTS_PERSIST_BY_DEFAULT", "true")
print(f"result storage dir: {RESULT_DIR}") print(f"api url: {os.environ.get('PREFECT_API_URL', '(not set)')}")
test_basic() test_caching() test_persisted_results() test_transactions()
print("\n=== all tests passed ===")
if __name__ == "__main__": main()