55import resource_alias1
66import resource_borrow_in_record
77import componentize_py_async_support
8+ import streams_and_futures as my_streams_and_futures
89
910from componentize_py_types import Result , Ok , Err
1011from tests import exports , imports
1112from tests .imports import resource_borrow_import
1213from tests .imports import simple_import_and_export
1314from tests .imports import simple_async_import_and_export
15+ from tests .imports import host_thing_interface
1416from tests .exports import resource_alias2
1517from tests .exports import streams_and_futures
1618from typing import Tuple , List , Optional
@@ -147,6 +149,42 @@ async def pipe_things(rx: StreamReader[streams_and_futures.Thing], tx: StreamWri
147149 # Write the things all at once. The host will read them only one at a time,
148150 # forcing us to re-take ownership of any unwritten items between writes.
149151 await tx .write_all (things )
152+
153+ async def pipe_host_things (rx : StreamReader [host_thing_interface .HostThing ], tx : StreamWriter [host_thing_interface .HostThing ]):
154+ # Read the things one at a time, forcing the host to re-take ownership of
155+ # any unwritten items between writes.
156+ things = []
157+ while not rx .writer_dropped :
158+ things += await rx .read (1 )
159+
160+ # Write the things all at once. The host will read them only one at a time,
161+ # forcing us to re-take ownership of any unwritten items between writes.
162+ await tx .write_all (things )
163+
164+ async def write_thing (thing : my_streams_and_futures .Thing ,
165+ tx1 : FutureWriter [streams_and_futures .Thing ],
166+ tx2 : FutureWriter [streams_and_futures .Thing ]):
167+ # The host will drop the first reader without reading, which should give us
168+ # back ownership of `thing`.
169+ wrote = await tx1 .write (thing )
170+ assert not wrote
171+ # The host will read from the second reader, though.
172+ wrote = await tx2 .write (thing )
173+ assert wrote
174+
175+ async def write_host_thing (thing : host_thing_interface .HostThing ,
176+ tx1 : FutureWriter [host_thing_interface .HostThing ],
177+ tx2 : FutureWriter [host_thing_interface .HostThing ]):
178+ # The host will drop the first reader without reading, which should give us
179+ # back ownership of `thing`.
180+ wrote = await tx1 .write (thing )
181+ assert not wrote
182+ # The host will read from the second reader, though.
183+ wrote = await tx2 .write (thing )
184+ assert wrote
185+
186+ def unreachable () -> str :
187+ raise AssertionError
150188
151189class StreamsAndFutures (exports .StreamsAndFutures ):
152190 async def echo_stream_u8 (self , stream : ByteStreamReader ) -> ByteStreamReader :
@@ -155,8 +193,6 @@ async def echo_stream_u8(self, stream: ByteStreamReader) -> ByteStreamReader:
155193 return rx
156194
157195 async def echo_future_string (self , future : FutureReader [str ]) -> FutureReader [str ]:
158- def unreachable () -> str :
159- raise AssertionError
160196 tx , rx = tests .string_future (unreachable )
161197 componentize_py_async_support .spawn (pipe_strings (future , tx ))
162198 return rx
@@ -166,6 +202,23 @@ async def short_reads(self, stream: StreamReader[streams_and_futures.Thing]) ->
166202 componentize_py_async_support .spawn (pipe_things (stream , tx ))
167203 return rx
168204
205+ async def short_reads_host (self , stream : StreamReader [host_thing_interface .HostThing ]) -> StreamReader [host_thing_interface .HostThing ]:
206+ tx , rx = tests .host_thing_interface_host_thing_stream ()
207+ componentize_py_async_support .spawn (pipe_host_things (stream , tx ))
208+ return rx
209+
210+ async def dropped_future_reader (self , value : str ) -> tuple [FutureReader [streams_and_futures .Thing ], FutureReader [streams_and_futures .Thing ]]:
211+ tx1 , rx1 = tests .streams_and_futures_thing_future (unreachable )
212+ tx2 , rx2 = tests .streams_and_futures_thing_future (unreachable )
213+ componentize_py_async_support .spawn (write_thing (my_streams_and_futures .Thing (value ), tx1 , tx2 ))
214+ return (rx1 , rx2 )
215+
216+ async def dropped_future_reader_host (self , value : str ) -> tuple [FutureReader [host_thing_interface .HostThing ], FutureReader [host_thing_interface .HostThing ]]:
217+ tx1 , rx1 = tests .host_thing_interface_host_thing_future (unreachable )
218+ tx2 , rx2 = tests .host_thing_interface_host_thing_future (unreachable )
219+ componentize_py_async_support .spawn (write_host_thing (host_thing_interface .HostThing (value ), tx1 , tx2 ))
220+ return (rx1 , rx2 )
221+
169222class Tests (tests .Tests ):
170223 def test_resource_borrow_import (self , v : int ) -> int :
171224 return resource_borrow_import .foo (resource_borrow_import .Thing (v + 1 )) + 4
0 commit comments