@@ -1310,6 +1310,63 @@ def test(i):
1310
1310
assert counts_by_worker .setdefault ("gw1" , 0 ) in (0 , num_tests )
1311
1311
assert counts_by_worker ["gw0" ] + counts_by_worker ["gw1" ] == num_tests
1312
1312
1313
+ def test_multi_scope_with_insufficient_fence (
1314
+ self , pytester : pytest .Pytester
1315
+ ) -> None :
1316
+ """
1317
+ When there are not enough fence tests from subsequent scope(s),
1318
+ this scheduler resorts to shutting down the excess workers in order to
1319
+ execute the final tests in each worker. isoscope allocates at least two
1320
+ tests per worker from the active scope, unless the scope has only one
1321
+ test.
1322
+ """
1323
+ test_file1 = f"""
1324
+ import pytest
1325
+ # 6 tests should distribute 2 per worker for 3 workers due to the
1326
+ # min-2 scope tests per worker rule.
1327
+ @pytest.mark.parametrize('i', range(6))
1328
+ def test(i):
1329
+ pass
1330
+ """
1331
+ test_file2 = f"""
1332
+ import pytest
1333
+ class FenceA:
1334
+ def test(self):
1335
+ pass
1336
+
1337
+ class FenceB:
1338
+ # Two tests are only enough for one fence due to min-2 scope
1339
+ # tests per worker rule
1340
+ def test1(self):
1341
+ pass
1342
+ def test1(self):
1343
+ pass
1344
+ """
1345
+ pytester .makepyfile (test_a = test_file1 , fence_tests = test_file2 )
1346
+ result = pytester .runpytest ("-n3" , "--dist=isoscope" , "-v" )
1347
+
1348
+ counts_by_worker_a = get_workers_and_test_count_by_prefix (
1349
+ "test_a.py::test" , result .outlines
1350
+ )
1351
+ # 6 tests should distribute 2 per worker for 3 workers due to the
1352
+ # min-2 scope tests per worker rule.
1353
+ assert sum (counts_by_worker_a .values ()) == 6
1354
+ for worker in ['gw0' , 'gw1' , 'gw2' ]:
1355
+ assert counts_by_worker_a [worker ] == 2
1356
+
1357
+ counts_by_worker_fence_a = get_workers_and_test_count_by_prefix (
1358
+ "fence_tests.py::FenceA" , result .outlines
1359
+ )
1360
+ counts_by_worker_fence_b = get_workers_and_test_count_by_prefix (
1361
+ "fence_tests.py::FenceB" , result .outlines
1362
+ )
1363
+
1364
+ assert len (counts_by_worker_fence_a ) == 1
1365
+ assert list (counts_by_worker_fence_a .values ())[0 ] == 1
1366
+
1367
+ assert len (counts_by_worker_fence_b ) == 1
1368
+ assert list (counts_by_worker_fence_b .values ())[0 ] == 2
1369
+
1313
1370
1314
1371
class TestLoadScope :
1315
1372
def test_by_module (self , pytester : pytest .Pytester ) -> None :
0 commit comments