2222import ast
2323import asyncio
2424import hashlib
25+ import importlib
2526import importlib .util
2627import json
2728import os
3334
3435import cloudpickle
3536
36- from sagemaker .serve .spec .inference_base import CustomOrchestrator
37+ from sagemaker .serve .spec .inference_base import AsyncCustomOrchestrator , CustomOrchestrator
3738
38- INFERENCE_SOURCE_PATH = Path (
39- importlib .util .find_spec ("sagemaker.serve.model_server.smd.custom_execution_inference" ).origin
40- )
39+ MODULE_NAME = "sagemaker.serve.model_server.smd.custom_execution_inference"
40+ INFERENCE_SOURCE_PATH = Path (importlib .util .find_spec (MODULE_NAME ).origin )
4141
4242
4343class EchoOrchestrator (CustomOrchestrator ):
@@ -47,6 +47,13 @@ def handle(self, data, context=None):
4747 return {"echo" : data }
4848
4949
50+ class AsyncEchoOrchestrator (AsyncCustomOrchestrator ):
51+ """Minimal async orchestrator, to exercise the awaited handler branch."""
52+
53+ async def handle (self , data , context = None ):
54+ return {"async_echo" : data }
55+
56+
5057class _Request :
5158 def __init__ (self , body ):
5259 self .body = body
@@ -62,15 +69,16 @@ def _write_artifacts(code_dir: Path, obj=None):
6269 )
6370
6471
65- def _exec_inference (code_dir : Path ) -> dict :
66- """Exec the entrypoint the way the container does and return its namespace."""
67- namespace = {"__name__" : "inference" , "__file__" : str (INFERENCE_SOURCE_PATH )}
72+ def _load_inference (code_dir : Path ):
73+ """Import the entrypoint against code_dir, running its import-time preflight.
74+
75+ The model server imports this file as ``inference``; importing the module for real
76+ (rather than exec-ing its source) keeps the import-time work attributable to the
77+ module under test.
78+ """
6879 with patch .dict (os .environ , {"SAGEMAKER_INFERENCE_CODE_DIRECTORY" : str (code_dir )}):
69- exec ( # noqa: S102 - mirrors how the model server imports inference.py
70- compile (INFERENCE_SOURCE_PATH .read_text (), str (INFERENCE_SOURCE_PATH ), "exec" ),
71- namespace ,
72- )
73- return namespace
80+ module = importlib .import_module (MODULE_NAME )
81+ return importlib .reload (module )
7482
7583
7684class TestCustomExecutionInferenceIsSelfContained (unittest .TestCase ):
@@ -106,18 +114,18 @@ def setUp(self):
106114 self .temp_dir = tempfile .mkdtemp ()
107115 self .code_dir = Path (self .temp_dir ) / "code"
108116 _write_artifacts (self .code_dir )
109- self .namespace = _exec_inference (self .code_dir )
117+ self .module = _load_inference (self .code_dir )
110118
111119 def tearDown (self ):
112120 shutil .rmtree (self .temp_dir , ignore_errors = True )
113121
114122 def test_uses_env_var_when_set (self ):
115123 with patch .dict (os .environ , {"SAGEMAKER_INFERENCE_CODE_DIRECTORY" : "/some/code" }):
116- self .assertEqual (str (self .namespace [ " _get_code_directory" ] ()), "/some/code" )
124+ self .assertEqual (str (self .module . _get_code_directory ()), "/some/code" )
117125
118126 def test_falls_back_to_default (self ):
119127 with patch .dict (os .environ , {}, clear = True ):
120- self .assertEqual (str (self .namespace [ " _get_code_directory" ] ()), "/opt/ml/model/code" )
128+ self .assertEqual (str (self .module . _get_code_directory ()), "/opt/ml/model/code" )
121129
122130
123131class TestPerformIntegrityCheck (unittest .TestCase ):
@@ -127,8 +135,8 @@ def setUp(self):
127135 self .temp_dir = tempfile .mkdtemp ()
128136 self .code_dir = Path (self .temp_dir ) / "code"
129137 _write_artifacts (self .code_dir )
130- self .namespace = _exec_inference (self .code_dir )
131- self .check = self .namespace [ " _perform_integrity_check" ]
138+ self .module = _load_inference (self .code_dir )
139+ self .check = self .module . _perform_integrity_check
132140 self .metadata_path = self .code_dir / "metadata.json"
133141 self .buffer = (self .code_dir / "serve.pkl" ).read_bytes ()
134142
@@ -179,19 +187,28 @@ def tearDown(self):
179187 def test_startup_and_handle (self ):
180188 _write_artifacts (self .code_dir )
181189
182- namespace = _exec_inference (self .code_dir )
190+ module = _load_inference (self .code_dir )
183191
184- self .assertIsInstance (namespace [ " custom_orchestrator" ] , EchoOrchestrator )
185- result = asyncio .run (namespace [ " handler" ] (_Request (b"hello" )))
192+ self .assertIsInstance (module . custom_orchestrator , EchoOrchestrator )
193+ result = asyncio .run (module . handler (_Request (b"hello" )))
186194 self .assertEqual (result , {"echo" : b"hello" })
187195
196+ def test_startup_and_handle_async (self ):
197+ _write_artifacts (self .code_dir , obj = (AsyncEchoOrchestrator (), None ))
198+
199+ module = _load_inference (self .code_dir )
200+
201+ self .assertIsInstance (module .custom_orchestrator , AsyncEchoOrchestrator )
202+ result = asyncio .run (module .handler (_Request (b"hello" )))
203+ self .assertEqual (result , {"async_echo" : b"hello" })
204+
188205 def test_startup_fails_on_tampered_pickle (self ):
189206 _write_artifacts (self .code_dir )
190207 serve_pkl = self .code_dir / "serve.pkl"
191208 serve_pkl .write_bytes (serve_pkl .read_bytes () + b"x" )
192209
193210 with self .assertRaises (ValueError ):
194- _exec_inference (self .code_dir )
211+ _load_inference (self .code_dir )
195212
196213
197214if __name__ == "__main__" :
0 commit comments