|
| 1 | +"""Unit tests for concurrent task execution.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from marvin import Task |
| 8 | +from marvin.agents.agent import Agent |
| 9 | +from marvin.fns.run import _tasks_are_independent, run_tasks, run_tasks_async |
| 10 | + |
| 11 | + |
| 12 | +class TestTaskIndependenceDetection: |
| 13 | + """Test the independence detection logic.""" |
| 14 | + |
| 15 | + def test_independent_tasks(self): |
| 16 | + """Test that truly independent tasks are detected as such.""" |
| 17 | + task1 = Task("Say 'one'", result_type=str) |
| 18 | + task2 = Task("Say 'two'", result_type=str) |
| 19 | + task3 = Task("Say 'three'", result_type=str) |
| 20 | + |
| 21 | + assert _tasks_are_independent([task1, task2, task3]) |
| 22 | + |
| 23 | + def test_dependent_tasks_depends_on(self): |
| 24 | + """Test that tasks with depends_on are not independent.""" |
| 25 | + task1 = Task("Say 'one'", result_type=str) |
| 26 | + task2 = Task("Say 'two'", result_type=str, depends_on=[task1]) |
| 27 | + |
| 28 | + assert not _tasks_are_independent([task1, task2]) |
| 29 | + |
| 30 | + def test_dependent_tasks_parent_child(self): |
| 31 | + """Test that parent-child tasks are not independent.""" |
| 32 | + parent = Task("Parent task", result_type=str) |
| 33 | + child = Task("Child task", result_type=str) |
| 34 | + parent.subtasks.add(child) # subtasks is a set, not list |
| 35 | + child.parent = parent |
| 36 | + |
| 37 | + assert not _tasks_are_independent([parent, child]) |
| 38 | + |
| 39 | + def test_single_task_is_independent(self): |
| 40 | + """Test that a single task is considered independent.""" |
| 41 | + task = Task("Solo task", result_type=str) |
| 42 | + assert _tasks_are_independent([task]) |
| 43 | + |
| 44 | + def test_empty_task_list(self): |
| 45 | + """Test empty task list.""" |
| 46 | + assert _tasks_are_independent([]) |
| 47 | + |
| 48 | + |
| 49 | +class TestConcurrentExecution: |
| 50 | + """Test actual concurrent execution behavior.""" |
| 51 | + |
| 52 | + @pytest.mark.asyncio |
| 53 | + async def test_independent_tasks_run_without_errors(self): |
| 54 | + """Test that independent tasks run without Multiple EndTurn warnings or errors.""" |
| 55 | + task1 = Task("Say 'one'", result_type=str) |
| 56 | + task2 = Task("Say 'two'", result_type=str) |
| 57 | + task3 = Task("Say 'three'", result_type=str) |
| 58 | + |
| 59 | + # Should not raise any errors (no Multiple EndTurn warnings, no infinite loops) |
| 60 | + results = await run_tasks_async([task1, task2, task3]) |
| 61 | + |
| 62 | + assert len(results) == 3 |
| 63 | + assert all(task.is_successful() for task in results) |
| 64 | + |
| 65 | + # Verify results |
| 66 | + result_values = [task.result for task in results] |
| 67 | + assert set(result_values) == {"one", "two", "three"} |
| 68 | + |
| 69 | + @pytest.mark.asyncio |
| 70 | + async def test_dependent_tasks_run_in_order(self): |
| 71 | + """Test that dependent tasks run in correct order.""" |
| 72 | + task1 = Task("Say 'A'", result_type=str) |
| 73 | + task2 = Task("Say 'B'", result_type=str, depends_on=[task1]) |
| 74 | + task3 = Task("Say 'C'", result_type=str, depends_on=[task2]) |
| 75 | + |
| 76 | + results = await run_tasks_async([task1, task2, task3]) |
| 77 | + |
| 78 | + assert len(results) == 3 |
| 79 | + assert all(task.is_successful() for task in results) |
| 80 | + |
| 81 | + # Verify correct order |
| 82 | + result_values = [task.result for task in results] |
| 83 | + assert result_values == ["A", "B", "C"] |
| 84 | + |
| 85 | + def test_sync_run_tasks_independent(self): |
| 86 | + """Test synchronous run_tasks with independent tasks.""" |
| 87 | + task1 = Task("Say 'one'", result_type=str) |
| 88 | + task2 = Task("Say 'two'", result_type=str) |
| 89 | + |
| 90 | + results = run_tasks([task1, task2]) |
| 91 | + |
| 92 | + assert len(results) == 2 |
| 93 | + assert all(task.is_successful() for task in results) |
| 94 | + assert set(t.result for t in results) == {"one", "two"} |
| 95 | + |
| 96 | + def test_sync_run_tasks_dependent(self): |
| 97 | + """Test synchronous run_tasks with dependent tasks.""" |
| 98 | + task1 = Task("Say 'A'", result_type=str) |
| 99 | + task2 = Task("Say 'B'", result_type=str, depends_on=[task1]) |
| 100 | + |
| 101 | + results = run_tasks([task1, task2]) |
| 102 | + |
| 103 | + assert len(results) == 2 |
| 104 | + assert all(task.is_successful() for task in results) |
| 105 | + |
| 106 | + # Verify correct order |
| 107 | + result_values = [task.result for task in results] |
| 108 | + assert result_values == ["A", "B"] |
| 109 | + |
| 110 | + |
| 111 | +class TestAsyncioGatherCompatibility: |
| 112 | + """Test that asyncio.gather works without ContextVar errors.""" |
| 113 | + |
| 114 | + @pytest.mark.asyncio |
| 115 | + async def test_asyncio_gather_no_context_errors(self): |
| 116 | + """Test that asyncio.gather doesn't throw ContextVar errors.""" |
| 117 | + task1 = Task("Say 'async1'", result_type=str) |
| 118 | + task2 = Task("Say 'async2'", result_type=str) |
| 119 | + task3 = Task("Say 'async3'", result_type=str) |
| 120 | + |
| 121 | + # This should not raise ContextVar token errors |
| 122 | + results = await asyncio.gather( |
| 123 | + task1.run_async(), task2.run_async(), task3.run_async() |
| 124 | + ) |
| 125 | + |
| 126 | + assert len(results) == 3 |
| 127 | + assert set(results) == {"async1", "async2", "async3"} |
| 128 | + |
| 129 | + @pytest.mark.asyncio |
| 130 | + async def test_mixed_execution_patterns(self): |
| 131 | + """Test mixing run_tasks_async and asyncio.gather in same event loop.""" |
| 132 | + # First batch via run_tasks_async |
| 133 | + task1 = Task("Say 'batch1'", result_type=str) |
| 134 | + task2 = Task("Say 'batch2'", result_type=str) |
| 135 | + batch1_results = await run_tasks_async([task1, task2]) |
| 136 | + |
| 137 | + # Second batch via asyncio.gather |
| 138 | + task3 = Task("Say 'gather1'", result_type=str) |
| 139 | + task4 = Task("Say 'gather2'", result_type=str) |
| 140 | + batch2_results = await asyncio.gather(task3.run_async(), task4.run_async()) |
| 141 | + |
| 142 | + # Both should work without errors |
| 143 | + assert len(batch1_results) == 2 |
| 144 | + assert all(task.is_successful() for task in batch1_results) |
| 145 | + assert len(batch2_results) == 2 |
| 146 | + assert set(batch2_results) == {"gather1", "gather2"} |
| 147 | + |
| 148 | + |
| 149 | +class TestContextVarHandling: |
| 150 | + """Test ContextVar token handling across async contexts.""" |
| 151 | + |
| 152 | + @pytest.mark.asyncio |
| 153 | + async def test_actor_context_across_asyncio_gather(self): |
| 154 | + """Test that Actor context management handles asyncio.gather correctly.""" |
| 155 | + from marvin.agents.actor import _current_actor |
| 156 | + |
| 157 | + async def task_with_actor(name): |
| 158 | + actor = Agent(name=f"Agent_{name}") |
| 159 | + # This should not raise an error even with asyncio.gather |
| 160 | + with actor: |
| 161 | + assert _current_actor.get() == actor |
| 162 | + await asyncio.sleep(0.1) # Simulate async work |
| 163 | + # Context should be reset without errors |
| 164 | + return name |
| 165 | + |
| 166 | + # Test that concurrent context management works |
| 167 | + results = await asyncio.gather( |
| 168 | + task_with_actor("1"), task_with_actor("2"), task_with_actor("3") |
| 169 | + ) |
| 170 | + |
| 171 | + assert results == ["1", "2", "3"] |
| 172 | + # Context should be None after all tasks complete |
| 173 | + assert _current_actor.get() is None |
| 174 | + |
| 175 | + def test_actor_context_sequential(self): |
| 176 | + """Test that Actor context works normally in sequential execution.""" |
| 177 | + from marvin.agents.actor import _current_actor |
| 178 | + |
| 179 | + actor1 = Agent(name="Sequential_1") |
| 180 | + actor2 = Agent(name="Sequential_2") |
| 181 | + |
| 182 | + # Test nested contexts work correctly |
| 183 | + assert _current_actor.get() is None |
| 184 | + |
| 185 | + with actor1: |
| 186 | + assert _current_actor.get() == actor1 |
| 187 | + with actor2: |
| 188 | + assert _current_actor.get() == actor2 |
| 189 | + assert _current_actor.get() == actor1 |
| 190 | + |
| 191 | + assert _current_actor.get() is None |
0 commit comments