|
1 | 1 | import os |
2 | 2 | from pathlib import Path |
3 | | -from unittest.mock import patch |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +import pytest |
4 | 6 |
|
5 | 7 | from _tilebox.grpc.replay import open_recording_channel, open_replay_channel |
6 | 8 | from tilebox.workflows import ExecutionContext, Task |
7 | 9 | from tilebox.workflows.cache import InMemoryCache, JobCache |
8 | 10 | from tilebox.workflows.client import Client |
9 | | -from tilebox.workflows.data import JobState |
| 11 | +from tilebox.workflows.data import JobState, RunnerContext |
| 12 | +from tilebox.workflows.runner.task_runner import TaskRunner |
10 | 13 |
|
11 | 14 |
|
12 | 15 | def int_to_bytes(n: int) -> bytes: |
@@ -143,3 +146,59 @@ def record_client(recording_file: str) -> Client: |
143 | 146 | open_channel_mock.assert_called_once() |
144 | 147 |
|
145 | 148 | return client |
| 149 | + |
| 150 | + |
| 151 | +class ExplicitIdentifierTaskV1(Task): |
| 152 | + @classmethod |
| 153 | + def identifier(cls) -> tuple[str, str]: |
| 154 | + return "tilebox.com/explicit", "v1.0" |
| 155 | + |
| 156 | + def execute(self, context: ExecutionContext) -> None: |
| 157 | + pass |
| 158 | + |
| 159 | + |
| 160 | +class ExplicitIdentifierTaskV2(Task): |
| 161 | + @classmethod |
| 162 | + def identifier(cls) -> tuple[str, str]: |
| 163 | + return "tilebox.com/explicit", "v2.0" |
| 164 | + |
| 165 | + def execute(self, context: ExecutionContext) -> None: |
| 166 | + pass |
| 167 | + |
| 168 | + |
| 169 | +def test_runner_disallow_duplicate_task_identifiers() -> None: |
| 170 | + runner = TaskRunner( |
| 171 | + MagicMock(), |
| 172 | + "dummy-cluster", |
| 173 | + InMemoryCache(), |
| 174 | + None, |
| 175 | + None, |
| 176 | + MagicMock(), |
| 177 | + RunnerContext(), |
| 178 | + ) |
| 179 | + |
| 180 | + runner.register(FlakyTask) |
| 181 | + with pytest.raises( |
| 182 | + ValueError, match="Duplicate task identifier: A task 'FlakyTask' with version 'v0.0' is already registered." |
| 183 | + ): |
| 184 | + runner.register(FlakyTask) |
| 185 | + |
| 186 | + runner.register(SumResultTask) |
| 187 | + with pytest.raises( |
| 188 | + ValueError, match="Duplicate task identifier: A task 'SumResultTask' with version 'v0.0' is already registered." |
| 189 | + ): |
| 190 | + runner.register(SumResultTask) |
| 191 | + |
| 192 | + runner.register(ExplicitIdentifierTaskV1) |
| 193 | + with pytest.raises( |
| 194 | + ValueError, |
| 195 | + match="Duplicate task identifier: A task 'tilebox.com/explicit' with version 'v1.0' is already registered.", |
| 196 | + ): |
| 197 | + runner.register(ExplicitIdentifierTaskV1) |
| 198 | + |
| 199 | + runner.register(ExplicitIdentifierTaskV2) # this one has a different version, so it's fine |
| 200 | + with pytest.raises( |
| 201 | + ValueError, |
| 202 | + match="Duplicate task identifier: A task 'tilebox.com/explicit' with version 'v2.0' is already registered.", |
| 203 | + ): |
| 204 | + runner.register(ExplicitIdentifierTaskV2) |
0 commit comments