|
| 1 | +import asyncio |
1 | 2 | import re |
2 | 3 | import pytest |
3 | 4 | from unittest.mock import MagicMock, patch |
@@ -86,7 +87,7 @@ def test_agent_custom_model() -> Agent: |
86 | 87 | name="test_agent_custom_model", |
87 | 88 | instructions="You are a helpful test assistant.", |
88 | 89 | # the model could be agents.OpenAIChatCompletionsModel() |
89 | | - model=MagicMock(spec=agents.Model, model="my-custom-model"), |
| 90 | + model="my-custom-model", |
90 | 91 | model_settings=ModelSettings( |
91 | 92 | max_tokens=100, |
92 | 93 | temperature=0.7, |
@@ -650,3 +651,45 @@ async def test_error_handling(sentry_init, capture_events, test_agent): |
650 | 651 | assert ai_client_span["description"] == "chat gpt-4" |
651 | 652 | assert ai_client_span["origin"] == "auto.ai.openai_agents" |
652 | 653 | assert ai_client_span["status"] == "internal_error" |
| 654 | + |
| 655 | + |
| 656 | +@pytest.mark.asyncio |
| 657 | +async def test_multiple_agents_asyncio( |
| 658 | + sentry_init, capture_events, test_agent, mock_model_response |
| 659 | +): |
| 660 | + """ |
| 661 | + Test that multiple agents can be run at the same time in asyncio tasks |
| 662 | + without interfering with each other. |
| 663 | + """ |
| 664 | + |
| 665 | + with patch.dict(os.environ, {"OPENAI_API_KEY": "test-key"}): |
| 666 | + with patch( |
| 667 | + "agents.models.openai_responses.OpenAIResponsesModel.get_response" |
| 668 | + ) as mock_get_response: |
| 669 | + mock_get_response.return_value = mock_model_response |
| 670 | + |
| 671 | + sentry_init( |
| 672 | + integrations=[OpenAIAgentsIntegration()], |
| 673 | + traces_sample_rate=1.0, |
| 674 | + ) |
| 675 | + |
| 676 | + events = capture_events() |
| 677 | + |
| 678 | + async def run(): |
| 679 | + await agents.Runner.run( |
| 680 | + starting_agent=test_agent, |
| 681 | + input="Test input", |
| 682 | + run_config=test_run_config, |
| 683 | + ) |
| 684 | + |
| 685 | + await asyncio.gather(*[run() for _ in range(3)]) |
| 686 | + |
| 687 | + assert len(events) == 3 |
| 688 | + txn1, txn2, txn3 = events |
| 689 | + |
| 690 | + assert txn1["type"] == "transaction" |
| 691 | + assert txn1["transaction"] == "test_agent workflow" |
| 692 | + assert txn2["type"] == "transaction" |
| 693 | + assert txn2["transaction"] == "test_agent workflow" |
| 694 | + assert txn3["type"] == "transaction" |
| 695 | + assert txn3["transaction"] == "test_agent workflow" |
0 commit comments