1717
1818import os
1919import uuid
20- import asyncio
2120
2221import pytest
2322import pytest_asyncio
@@ -75,55 +74,39 @@ async def test_send_event_and_poll(self, client: AsyncAgentex, agent_id: str):
7574 # Poll for the initial task creation message
7675 task_creation_message_found = False
7776
78- async def poll_for_task_creation () -> None :
79- nonlocal task_creation_message_found
80- async for message in poll_messages (
81- client = client ,
82- task_id = task .id ,
83- timeout = 30 ,
84- sleep_interval = 1.0 ,
85- ):
86- assert isinstance (message , TaskMessage )
87- if message .content and message .content .type == "text" and message .content .author == "agent" :
88- assert "Hello! I've received your task" in message .content .content
89- task_creation_message_found = True
90- break
91-
92- try :
93- await asyncio .wait_for (poll_for_task_creation (), timeout = 30 )
94- except asyncio .TimeoutError :
95- pytest .fail ("Polling timed out waiting for task creation message" )
77+ async for message in poll_messages (
78+ client = client ,
79+ task_id = task .id ,
80+ timeout = 30 ,
81+ sleep_interval = 1.0 ,
82+ ):
83+ assert isinstance (message , TaskMessage )
84+ if message .content and message .content .type == "text" and message .content .author == "agent" :
85+ assert "Hello! I've received your task" in message .content .content
86+ task_creation_message_found = True
87+ break
9688
9789 assert task_creation_message_found , "Task creation message not found"
9890
9991 # Send an event and poll for response
10092 user_message = "Hello, this is a test message!"
10193 agent_response_found = False
10294
103- async def poll_for_response () -> None :
104- nonlocal agent_response_found
105- async for message in send_event_and_poll_yielding (
106- client = client ,
107- agent_id = agent_id ,
108- task_id = task .id ,
109- user_message = user_message ,
110- timeout = 30 ,
111- sleep_interval = 1.0 ,
112- ):
113- assert isinstance (message , TaskMessage )
114- if message .content and message .content .type == "text" and message .content .author == "agent" :
115- if "Hello! I've received your message" in message .content .content :
116- agent_response_found = True
117- break
118-
119- try :
120- await asyncio .wait_for (poll_for_response (), timeout = 30 )
121- except asyncio .TimeoutError :
122- pytest .fail ("Polling timed out waiting for agent response" )
95+ async for message in send_event_and_poll_yielding (
96+ client = client ,
97+ agent_id = agent_id ,
98+ task_id = task .id ,
99+ user_message = user_message ,
100+ timeout = 30 ,
101+ sleep_interval = 1.0 ,
102+ ):
103+ assert isinstance (message , TaskMessage )
104+ if message .content and message .content .type == "text" and message .content .author == "agent" :
105+ assert "Hello! I've received your task" in message .content .content
106+ agent_response_found = True
107+ break
123108
124109 assert agent_response_found , "Agent response not found"
125-
126-
127110class TestStreamingEvents :
128111 """Test streaming event sending."""
129112
@@ -136,27 +119,19 @@ async def test_send_event_and_stream(self, client: AsyncAgentex, agent_id: str):
136119 assert task is not None
137120 task_creation_found = False
138121
139- # Poll for the initial task creation message
140- async def poll_for_task_creation () -> None :
141- nonlocal task_creation_found
142- async for message in poll_messages (
143- client = client ,
144- task_id = task .id ,
145- timeout = 30 ,
146- sleep_interval = 1.0 ,
147- ):
148- assert isinstance (message , TaskMessage )
149- if message .content and message .content .type == "text" and message .content .author == "agent" :
150- assert "Hello! I've received your task" in message .content .content
151- task_creation_found = True
152- break
153-
154- try :
155- await asyncio .wait_for (poll_for_task_creation (), timeout = 30 )
156- except asyncio .TimeoutError :
157- pytest .fail ("Polling timed out waiting for task creation message" )
158-
159- assert task_creation_found , "Task creation message not found in poll"
122+ async for message in poll_messages (
123+ client = client ,
124+ task_id = task .id ,
125+ timeout = 30 ,
126+ sleep_interval = 1.0 ,
127+ ):
128+ assert isinstance (message , TaskMessage )
129+ if message .content and message .content .type == "text" and message .content .author == "agent" :
130+ assert "Hello! I've received your task" in message .content .content
131+ task_creation_found = True
132+ break
133+
134+ assert task_creation_found , "Task creation message not found"
160135
161136 user_message = "Hello, this is a test message!"
162137 stream_timeout = 10
@@ -168,48 +143,36 @@ async def poll_for_task_creation() -> None:
168143 user_echo_found = False
169144 agent_response_found = False
170145
171- async def collect_stream_events () -> None :
172- nonlocal user_echo_found , agent_response_found
173-
174- async for event in stream_agent_response (
175- client = client ,
176- task_id = task .id ,
177- timeout = stream_timeout ,
178- ):
179- all_events .append (event )
180- # Check events as they arrive
181- event_type = event .get ("type" )
182- if event_type == "full" :
183- content = event .get ("content" , {})
184- if content .get ("content" ) is None :
185- continue # Skip empty content
186- if content .get ("type" ) == "text" and content .get ("author" ) == "agent" :
187- # Check for agent response to user message
188- if "Hello! I've received your message" in content .get ("content" , "" ):
189- # Agent response should come after user echo
190- assert user_echo_found , "Agent response arrived before user message echo (incorrect order)"
191- agent_response_found = True
192- elif content .get ("type" ) == "text" and content .get ("author" ) == "user" :
193- # Check for user message echo
194- if content .get ("content" ) == user_message :
195- user_echo_found = True
196-
197- # Exit early if we've found all expected messages
198- if user_echo_found and agent_response_found :
199- break
200-
201- # Start streaming task
202- stream_task = asyncio .create_task (collect_stream_events ())
203-
204146 # Send the event
205147 event_content = TextContentParam (type = "text" , author = "user" , content = user_message )
206148 await client .agents .send_event (agent_id = agent_id , params = {"task_id" : task .id , "content" : event_content })
207149
208- # Wait for the stream to complete (with timeout)
209- try :
210- await asyncio .wait_for (stream_task , timeout = stream_timeout )
211- except asyncio .TimeoutError :
212- pytest .fail (f"Stream timed out after { stream_timeout } s waiting for expected messages" )
150+ async for event in stream_agent_response (
151+ client = client ,
152+ task_id = task .id ,
153+ timeout = stream_timeout ,
154+ ):
155+ all_events .append (event )
156+ # Check events as they arrive
157+ event_type = event .get ("type" )
158+ if event_type == "full" :
159+ content = event .get ("content" , {})
160+ if content .get ("content" ) is None :
161+ continue # Skip empty content
162+ if content .get ("type" ) == "text" and content .get ("author" ) == "agent" :
163+ # Check for agent response to user message
164+ if "Hello! I've received your message" in content .get ("content" , "" ):
165+ # Agent response should come after user echo
166+ assert user_echo_found , "Agent response arrived before user message echo (incorrect order)"
167+ agent_response_found = True
168+ elif content .get ("type" ) == "text" and content .get ("author" ) == "user" :
169+ # Check for user message echo
170+ if content .get ("content" ) == user_message :
171+ user_echo_found = True
172+
173+ # Exit early if we've found all expected messages
174+ if user_echo_found and agent_response_found :
175+ break
213176
214177 # Verify all expected messages were received (fail if stream ended without finding them)
215178 assert user_echo_found , "User message echo not found in stream"
0 commit comments