|
7 | 7 |
|
8 | 8 | import asyncio |
9 | 9 | import datetime |
| 10 | +import random |
10 | 11 | from collections.abc import Awaitable, Callable |
| 12 | +from typing import cast |
11 | 13 |
|
| 14 | +import arrow |
12 | 15 | import pytest |
13 | 16 | from _helpers import PublishedProject |
14 | 17 | from faker import Faker |
|
23 | 26 | UserNotFoundError, |
24 | 27 | ) |
25 | 28 | from simcore_service_director_v2.models.comp_runs import CompRunsAtDB, RunMetadataDict |
| 29 | +from simcore_service_director_v2.modules.comp_scheduler._constants import ( |
| 30 | + SCHEDULER_INTERVAL, |
| 31 | +) |
26 | 32 | from simcore_service_director_v2.modules.db.repositories.comp_runs import ( |
27 | 33 | CompRunsRepository, |
28 | 34 | ) |
@@ -117,14 +123,87 @@ async def test_list( |
117 | 123 | ) |
118 | 124 | == [] |
119 | 125 | ) |
120 | | - |
121 | 126 | assert sorted( |
122 | 127 | await CompRunsRepository(aiopg_engine).list( |
123 | 128 | filter_by_state={RunningState.PUBLISHED} |
124 | 129 | ), |
125 | 130 | key=lambda x: x.iteration, |
126 | 131 | ) == sorted(created, key=lambda x: x.iteration) |
127 | 132 |
|
| 133 | + # test with never scheduled filter, let's create a bunch of scheduled entries, |
| 134 | + assert sorted( |
| 135 | + await CompRunsRepository(aiopg_engine).list(never_scheduled=True), |
| 136 | + key=lambda x: x.iteration, |
| 137 | + ) == sorted(created, key=lambda x: x.iteration) |
| 138 | + comp_runs_marked_for_scheduling = random.sample(created, k=25) |
| 139 | + await asyncio.gather( |
| 140 | + *( |
| 141 | + CompRunsRepository(aiopg_engine).mark_for_scheduling( |
| 142 | + user_id=comp_run.user_id, |
| 143 | + project_id=comp_run.project_uuid, |
| 144 | + iteration=comp_run.iteration, |
| 145 | + ) |
| 146 | + for comp_run in comp_runs_marked_for_scheduling |
| 147 | + ) |
| 148 | + ) |
| 149 | + # filter them away |
| 150 | + created = [r for r in created if r not in comp_runs_marked_for_scheduling] |
| 151 | + assert sorted( |
| 152 | + await CompRunsRepository(aiopg_engine).list(never_scheduled=True), |
| 153 | + key=lambda x: x.iteration, |
| 154 | + ) == sorted(created, key=lambda x: x.iteration) |
| 155 | + |
| 156 | + # now mark a few of them as processed |
| 157 | + comp_runs_marked_as_processed = random.sample(comp_runs_marked_for_scheduling, k=11) |
| 158 | + await asyncio.gather( |
| 159 | + *( |
| 160 | + CompRunsRepository(aiopg_engine).mark_as_processed( |
| 161 | + user_id=comp_run.user_id, |
| 162 | + project_id=comp_run.project_uuid, |
| 163 | + iteration=comp_run.iteration, |
| 164 | + ) |
| 165 | + for comp_run in comp_runs_marked_as_processed |
| 166 | + ) |
| 167 | + ) |
| 168 | + # filter them away |
| 169 | + comp_runs_marked_for_scheduling = [ |
| 170 | + r |
| 171 | + for r in comp_runs_marked_for_scheduling |
| 172 | + if r not in comp_runs_marked_as_processed |
| 173 | + ] |
| 174 | + # since they were just marked as processed now, we will get nothing |
| 175 | + assert ( |
| 176 | + sorted( |
| 177 | + await CompRunsRepository(aiopg_engine).list( |
| 178 | + never_scheduled=False, processed_since=SCHEDULER_INTERVAL |
| 179 | + ), |
| 180 | + key=lambda x: x.iteration, |
| 181 | + ) |
| 182 | + == [] |
| 183 | + ) |
| 184 | + # now we artificially change the processed time and set it 2x the scheduler interval |
| 185 | + fake_processed_time = arrow.utcnow().datetime - 2 * SCHEDULER_INTERVAL |
| 186 | + comp_runs_marked_as_processed = await asyncio.gather( |
| 187 | + *( |
| 188 | + CompRunsRepository(aiopg_engine).update( |
| 189 | + user_id=comp_run.user_id, |
| 190 | + project_id=comp_run.project_uuid, |
| 191 | + iteration=comp_run.iteration, |
| 192 | + processed=fake_processed_time, |
| 193 | + ) |
| 194 | + for comp_run in comp_runs_marked_as_processed |
| 195 | + ) |
| 196 | + ) |
| 197 | + # now we should get them |
| 198 | + assert sorted( |
| 199 | + await CompRunsRepository(aiopg_engine).list( |
| 200 | + never_scheduled=False, processed_since=SCHEDULER_INTERVAL |
| 201 | + ), |
| 202 | + key=lambda x: x.iteration, |
| 203 | + ) == sorted( |
| 204 | + comp_runs_marked_as_processed, key=lambda x: cast(CompRunsAtDB, x).iteration |
| 205 | + ) |
| 206 | + |
128 | 207 |
|
129 | 208 | async def test_create( |
130 | 209 | aiopg_engine, |
@@ -387,7 +466,7 @@ async def test_mark_scheduling_done( |
387 | 466 | assert created.scheduled is None |
388 | 467 | assert created.processed is None |
389 | 468 |
|
390 | | - updated = await CompRunsRepository(aiopg_engine).mark_scheduling_done( |
| 469 | + updated = await CompRunsRepository(aiopg_engine).mark_as_processed( |
391 | 470 | user_id=created.user_id, |
392 | 471 | project_id=created.project_uuid, |
393 | 472 | iteration=created.iteration, |
|
0 commit comments