1515- AGENT_NAME: Name of the agent to test (default: at000-hello-acp)
1616"""
1717
18+ from typing import Any
1819import os
1920import uuid
2021import asyncio
@@ -71,7 +72,7 @@ async def test_send_event_and_poll(self, client: AsyncAgentex, agent_id: str):
7172 task_response = await client .agents .create_task (agent_id , params = ParamsCreateTaskRequest (name = uuid .uuid1 ().hex ))
7273 task = task_response .result
7374 assert task is not None
74-
75+ task_creation_found = False
7576 # Poll for the initial task creation message
7677 async for message in poll_messages (
7778 client = client ,
@@ -82,8 +83,10 @@ async def test_send_event_and_poll(self, client: AsyncAgentex, agent_id: str):
8283 assert isinstance (message , TaskMessage )
8384 if message .content and message .content .type == "text" and message .content .author == "agent" :
8485 assert "Hello! I've received your task" in message .content .content
86+ task_creation_found = True
8587 break
86-
88+
89+ assert task_creation_found , "Task creation message not found in poll"
8790 await asyncio .sleep (1.5 )
8891 # Send an event and poll for response
8992 user_message = "Hello, this is a test message!"
@@ -110,23 +113,37 @@ async def test_send_event_and_stream(self, client: AsyncAgentex, agent_id: str):
110113 task = task_response .result
111114 assert task is not None
112115
116+ task_creation_found = False
117+ async for message in poll_messages (
118+ client = client ,
119+ task_id = task .id ,
120+ timeout = 30 ,
121+ sleep_interval = 1.0 ,
122+ ):
123+ assert isinstance (message , TaskMessage )
124+ if message .content and message .content .type == "text" and message .content .author == "agent" :
125+ assert "Hello! I've received your task" in message .content .content
126+ task_creation_found = True
127+ break
128+
129+ assert task_creation_found , "Task creation message not found in poll"
130+
113131 user_message = "Hello, this is a test message!"
114132
115133 # Collect events from stream
116- all_events = []
134+ all_events : list [ dict [ str , Any ]] = []
117135
118136 # Flags to track what we've received
119- task_creation_found = False
120137 user_echo_found = False
121138 agent_response_found = False
122-
139+ stream_timeout = 30
123140 async def collect_stream_events (): #noqa: ANN101
124- nonlocal task_creation_found , user_echo_found , agent_response_found
141+ nonlocal user_echo_found , agent_response_found
125142
126143 async for event in stream_agent_response (
127144 client = client ,
128145 task_id = task .id ,
129- timeout = 30 ,
146+ timeout = stream_timeout ,
130147 ):
131148 # Check events as they arrive
132149 event_type = event .get ("type" )
@@ -135,11 +152,8 @@ async def collect_stream_events(): #noqa: ANN101
135152 if content .get ("content" ) is None :
136153 continue # Skip empty content
137154 if content .get ("type" ) == "text" and content .get ("author" ) == "agent" :
138- # Check for initial task creation message
139- if "Hello! I've received your task" in content .get ("content" , "" ):
140- task_creation_found = True
141155 # Check for agent response to user message
142- elif "Hello! I've received your message" in content .get ("content" , "" ):
156+ if "Hello! I've received your message" in content .get ("content" , "" ):
143157 # Agent response should come after user echo
144158 assert user_echo_found , "Agent response arrived before user message echo (incorrect order)"
145159 agent_response_found = True
@@ -149,21 +163,25 @@ async def collect_stream_events(): #noqa: ANN101
149163 user_echo_found = True
150164
151165 # Exit early if we've found all expected messages
152- if task_creation_found and user_echo_found and agent_response_found :
166+ if user_echo_found and agent_response_found :
153167 break
154-
155- assert task_creation_found , "Task creation message not found in stream"
156- assert user_echo_found , "User message echo not found in stream"
157- assert agent_response_found , "Agent response not found in stream"
158-
159-
160168 # Start streaming task
161169 stream_task = asyncio .create_task (collect_stream_events ())
162170
163171 # Send the event
164172 event_content = TextContentParam (type = "text" , author = "user" , content = user_message )
165173 await client .agents .send_event (agent_id = agent_id , params = {"task_id" : task .id , "content" : event_content })
166174
175+ # Wait for the stream to complete (with timeout)
176+ try :
177+ await asyncio .wait_for (stream_task , timeout = stream_timeout )
178+ except asyncio .TimeoutError :
179+ pytest .fail (f"Stream timed out after { stream_timeout } s waiting for expected messages" )
180+
181+ # Verify all expected messages were received (fail if stream ended without finding them)
182+
183+ assert user_echo_found , "User message echo not found in stream"
184+ assert agent_response_found , "Agent response not found in stream"
167185 # Wait for streaming to complete
168186 await stream_task
169187
0 commit comments