test_scheduling.py 24.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

###############################################################################
#                                                                             #
# Copyright (c) 2019 Idiap Research Institute, http://www.idiap.ch/           #
# Contact: beat.support@idiap.ch                                              #
#                                                                             #
# This file is part of the beat.web module of the BEAT platform.              #
#                                                                             #
# Commercial License Usage                                                    #
# Licensees holding valid commercial BEAT licenses may use this file in       #
# accordance with the terms contained in a written agreement between you      #
# and Idiap. For further information contact tto@idiap.ch                     #
#                                                                             #
# Alternatively, this file may be used under the terms of the GNU Affero      #
# Public License version 3 as published by the Free Software and appearing    #
# in the file LICENSE.AGPL included in the packaging of this file.            #
# The BEAT platform is distributed in the hope that it will be useful, but    #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  #
# or FITNESS FOR A PARTICULAR PURPOSE.                                        #
#                                                                             #
# You should have received a copy of the GNU Affero Public License along      #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/.           #
#                                                                             #
###############################################################################

import multiprocessing
29
import os
30 31
import tempfile
import time
32 33
from unittest import skipIf

34
from django import db
35 36
from django.conf import settings
from django.core.management import call_command
37
from django.test import TransactionTestCase
38 39

from beat.core.utils import find_free_port
40 41
from beat.web.backend.helpers import cancel_experiment
from beat.web.backend.helpers import schedule_experiment
42 43
from beat.web.backend.models.worker import Worker
from beat.web.backend.tests.common import ONE_QUEUE_TWO_WORKERS
44
from beat.web.backend.tests.common import BackendUtilitiesMixin
45 46
from beat.web.backend.utils import setup_backend
from beat.web.experiments.models import Block
47
from beat.web.experiments.models import Experiment
48 49 50 51 52 53 54


def start_broker(port, verbosity=0):
    db.connections.close_all()
    call_command("broker", port=port, verbosity=verbosity)


55
def start_worker(worker_name, broker_address, prefix, cache, verbosity=0, docker=False):
56 57 58 59 60 61 62
    call_command(
        "worker",
        name=worker_name,
        broker_address=broker_address,
        prefix=prefix,
        cache=cache,
        verbosity=verbosity,
63
        docker=docker,
64 65 66
    )


67
def start_scheduler(broker_address, verbosity=0):
68
    db.connections.close_all()
69
    call_command("scheduler", broker_address=broker_address, verbosity=verbosity)
70 71


72
@skipIf(settings.RUNNING_ON_CI, "Not runnable on the CI")
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
class WorkerRegistration(TransactionTestCase):
    def setUp(self):
        db_worker = Worker(name="test_worker1", active=False, cores=1, memory=12)
        db_worker.save()
        db_worker2 = Worker(name="test_worker2", active=False, cores=1, memory=12)
        db_worker2.save()

        self.db_workers = [db_worker, db_worker2]
        self.port = find_free_port()
        self.broker_address = "tcp://localhost:{}".format(self.port)

    def test_worker_registration(self):
        prefix = tempfile.TemporaryDirectory(prefix="beat.scheduling")
        cache = os.path.join(prefix.name, "cache")
        os.mkdir(cache)

        broker = multiprocessing.Process(target=start_broker, args=(self.port, 0))
        workers = []
        for db_worker in self.db_workers:
            worker = multiprocessing.Process(
                target=start_worker,
                args=(db_worker.name, self.broker_address, prefix.name, cache, 0),
            )
            workers.append(worker)

        broker.start()

        for worker in workers:
            worker.start()

        max_rounds = 5
        for db_worker in self.db_workers:
            db_worker.refresh_from_db()

        while max_rounds > 0 and not all([worker.active for worker in self.db_workers]):
            max_rounds -= 1
            worker.join(5)  # give time to have heartbeat sent and gone detected
            for db_worker in self.db_workers:
                db_worker.refresh_from_db()

        was_active = all([db_worker.active for worker in self.db_workers])

        for worker in workers:
            worker.terminate()
            worker.join()

        max_rounds = 5
        while max_rounds > 0 and not all(
            [not worker.active for worker in self.db_workers]
        ):
            max_rounds -= 1
            broker.join(5)  # give time to have heartbeat sent and gone detected
            for db_worker in self.db_workers:
                db_worker.refresh_from_db()

        broker.terminate()
        broker.join()

        self.assertTrue(was_active)
        self.assertTrue(all([not db_worker.active for worker in self.db_workers]))


# ----------------------------------------------------------


138
@skipIf(settings.RUNNING_ON_CI, "Not runnable on the CI")
139 140 141 142 143 144 145 146 147 148 149
class TestSchedulerBase(TransactionTestCase, BackendUtilitiesMixin):
    def __init__(self, methodName="runTest"):
        super(TestSchedulerBase, self).__init__(methodName)
        self.port = find_free_port()
        self.broker_address = "tcp://localhost:{}".format(self.port)

        self.scheduler = None
        self.broker = None
        self.workers = {}

        self.command_verbosity = 3
150
        self.timeout = settings.DB_REFRESH_TIMEOUT  # Failure timeout
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

    @classmethod
    def setUpTestData(cls):
        cls.setup_test_data()

    def setUp(self):
        self.shutdown_everything()
        self.clean_cache()

        TestSchedulerBase.setup_test_data()
        setup_backend(ONE_QUEUE_TWO_WORKERS)
        Worker.objects.update(active=False)

    def tearDown(self):
        self.shutdown_everything()
        self.clean_cache()

    def start_scheduling(self, verbosity=None):
        if verbosity is None:
            verbosity = self.command_verbosity

        self.start_broker(verbosity)
        self.start_scheduler(verbosity)

    def shutdown_everything(self):
        for name in list(self.workers.keys()):
            self.stop_worker(name)

        self.workers = {}

        self.stop_scheduler()
        self.stop_broker()

    def start_scheduler(self, verbosity=None):
        if verbosity is None:
            verbosity = self.command_verbosity

        self.scheduler = multiprocessing.Process(
189
            target=start_scheduler, args=(self.broker_address, verbosity)
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        )
        self.scheduler.start()

    def stop_scheduler(self):
        if self.scheduler is not None:
            self.scheduler.terminate()
            self.scheduler.join()
            self.scheduler = None

    def start_broker(self, verbosity=None):
        if verbosity is None:
            verbosity = self.command_verbosity

        self.broker = multiprocessing.Process(
            target=start_broker, args=(self.port, verbosity)
        )
        self.broker.start()

    def stop_broker(self):
        if self.broker is not None:
            self.broker.terminate()
            self.broker.join()
            self.broker = None

214
    def start_worker(self, name, verbosity=None, docker=False):
215 216 217 218 219 220 221 222 223 224 225
        if verbosity is None:
            verbosity = self.command_verbosity

        worker = multiprocessing.Process(
            target=start_worker,
            args=(
                name,
                self.broker_address,
                settings.PREFIX,
                settings.CACHE_ROOT,
                verbosity,
226
                docker,
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
            ),
        )

        worker.start()

        self.workers[name] = worker

    def stop_worker(self, name):
        worker = self.workers.pop(name, None)

        if worker is not None:
            worker.terminate()
            worker.join()
            del worker

    def check_worker_status(self, name, active, timeout=10):
        start = time.time()
        worker = Worker.objects.get(name=name)

        while (
            worker.active != active
        ):  # Worker.objects.filter(name=name, active=active).count() == 0:
            if time.time() - start > timeout:
                self.shutdown_everything()
                self.assertTrue(False)
            worker.refresh_from_db()


# ----------------------------------------------------------


258
@skipIf(settings.RUNNING_ON_CI, "Not runnable on the CI")
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
class TestConnection(TestSchedulerBase):
    def test_worker_connection(self):
        self.start_scheduling()

        self.assertEqual(Worker.objects.filter(active=True).count(), 0)

        self.start_worker("node1")

        self.check_worker_status("node1", True)

        self.assertEqual(Worker.objects.filter(active=True).count(), 1)

    def test_worker_disconnection(self):
        self.start_scheduling()
        self.start_worker("node1")

        self.check_worker_status("node1", True)

        self.workers["node1"].terminate()
        self.workers["node1"].join()
        del self.workers["node1"]

        self.check_worker_status("node1", False)

    def test_two_workers_connection(self):
        self.start_scheduling()

        self.assertEqual(Worker.objects.filter(active=True).count(), 0)

        self.start_worker("node1")
        self.start_worker("node2")

        self.check_worker_status("node1", True)
        self.check_worker_status("node2", True)

        self.assertEqual(Worker.objects.filter(active=True).count(), 2)

    def test_scheduler_last(self):
        self.start_worker("node1")

        time.sleep(1)

        self.start_scheduling()

        self.check_worker_status("node1", True)

    def test_broker_shutdown(self):
        self.start_broker()

        self.assertEqual(Worker.objects.filter(active=True).count(), 0)

        self.start_worker("node1")

        self.check_worker_status("node1", True)

        self.assertEqual(Worker.objects.filter(active=True).count(), 1)

        self.stop_broker()

        self.assertEqual(Worker.objects.filter(active=False).count(), 1)

        time.sleep(1)

        self.start_broker()

        self.check_worker_status("node1", True)

        self.assertEqual(Worker.objects.filter(active=True).count(), 1)


# ----------------------------------------------------------


332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
@skipIf(settings.RUNNING_ON_CI, "Not runnable on the CI")
class TestDockerExecution(TestSchedulerBase):
    def setUp(self):
        super().setUp()

        import subprocess

        subprocess.run(
            [
                "./bin/beat",
                "--cache",
                settings.CACHE_ROOT,
                "--prefix",
                settings.PREFIX,
                "databases",
                "index",
                "simple_rawdata_access/1",
                "--docker",
            ],
            check=True,
        )
        self.start_scheduling()
        self.start_worker("node1", docker=True)
        self.check_worker_status("node1", True)

    def test_database_rawdata_access_experiment(self):
        fullname = "user/user/single/1/single_rawdata_access"

        xp = Experiment.objects.get(name=fullname.split("/")[-1])

        schedule_experiment(xp)
        xp.refresh_from_db()

        while xp.status != Experiment.DONE:
            xp.refresh_from_db()

        echo = xp.blocks.get(name="echo")
        analysis = xp.blocks.get(name="analysis")

        self.assertEqual(echo.status, Block.DONE)
        self.assertEqual(analysis.status, Block.DONE)


375
@skipIf(settings.RUNNING_ON_CI, "Not runnable on the CI")
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
class TestExecution(TestSchedulerBase):
    def setUp(self):
        super(TestExecution, self).setUp()

        self.start_scheduling()
        self.start_worker("node1")
        self.start_worker("node2")
        self.check_worker_status("node1", True)
        self.check_worker_status("node2", True)

    def test_successful_experiment(self):
        fullname = "user/user/double/1/double"

        xp = Experiment.objects.get(name=fullname.split("/")[-1])

        self.prepare_databases(xp.declaration)

        schedule_experiment(xp)
        xp.refresh_from_db()

        start = time.time()
        while xp.status != Experiment.DONE:
            self.assertTrue(time.time() - start < self.timeout)
            xp.refresh_from_db()

        b1 = xp.blocks.get(name="echo1")
        b2 = xp.blocks.get(name="echo2")
        b3 = xp.blocks.get(name="analysis")

        self.assertEqual(b1.status, Block.DONE)
        self.assertEqual(b2.status, Block.DONE)
        self.assertEqual(b3.status, Block.DONE)

    def test_successful_splitted_experiment(self):
        fullname = "user/user/double/1/double_split_2"

        xp = Experiment.objects.get(name=fullname.split("/")[-1])

        self.prepare_databases(xp.declaration)

        schedule_experiment(xp)
        xp.refresh_from_db()

        start = time.time()
        while xp.status != Experiment.DONE:
            self.assertTrue(time.time() - start < self.timeout)
            xp.refresh_from_db()

        b1 = xp.blocks.get(name="echo1")
        b2 = xp.blocks.get(name="echo2")
        b3 = xp.blocks.get(name="analysis")

        self.assertEqual(b1.status, Block.DONE)
        self.assertEqual(b2.status, Block.DONE)
        self.assertEqual(b3.status, Block.DONE)

    def test_two_similar_experiments(self):
        fullname1 = "user/user/double/1/double"
        fullname2 = "user/user/double/1/double_split_2"

        xp1 = Experiment.objects.get(name=fullname1.split("/")[-1])
        xp2 = Experiment.objects.get(name=fullname2.split("/")[-1])

        self.prepare_databases(xp1.declaration)
        self.prepare_databases(xp2.declaration)

        schedule_experiment(xp1)
        schedule_experiment(xp2)

        xp1.refresh_from_db()
        xp2.refresh_from_db()

        start = time.time()
        while (xp1.status != Experiment.DONE) or (xp2.status != Experiment.DONE):
            self.assertTrue(time.time() - start < self.timeout)
            xp1.refresh_from_db()
            xp2.refresh_from_db()

        self.assertEqual(xp1.start_date, xp2.start_date)
        self.assertEqual(xp1.end_date, xp2.end_date)

        b1 = xp1.blocks.get(name="echo1")
        b2 = xp2.blocks.get(name="echo1")

        self.assertEqual(b1.status, Block.DONE)
        self.assertEqual(b2.status, Block.DONE)
        self.assertEqual(b1.start_date, b2.start_date)
        self.assertEqual(b1.end_date, b2.end_date)

        b1 = xp1.blocks.get(name="echo2")
        b2 = xp2.blocks.get(name="echo2")

        self.assertEqual(b1.status, Block.DONE)
        self.assertEqual(b2.status, Block.DONE)
        self.assertEqual(b1.start_date, b2.start_date)
        self.assertEqual(b1.end_date, b2.end_date)

        b1 = xp1.blocks.get(name="analysis")
        b2 = xp2.blocks.get(name="analysis")

        self.assertEqual(b1.status, Block.DONE)
        self.assertEqual(b2.status, Block.DONE)
        self.assertEqual(b1.start_date, b2.start_date)
        self.assertEqual(b1.end_date, b2.end_date)

    def test_two_different_experiments(self):
        fullname1 = "user/user/single/1/single"
        fullname2 = "user/user/single/1/single_add"

        xp1 = Experiment.objects.get(name=fullname1.split("/")[-1])
        xp2 = Experiment.objects.get(name=fullname2.split("/")[-1])

        self.prepare_databases(xp1.declaration)
        self.prepare_databases(xp2.declaration)

        schedule_experiment(xp1)
        schedule_experiment(xp2)

        xp1.refresh_from_db()
        xp2.refresh_from_db()

        start = time.time()
        while (xp1.status != Experiment.DONE) or (xp2.status != Experiment.DONE):
            self.assertTrue(time.time() - start < self.timeout)
            xp1.refresh_from_db()
            xp2.refresh_from_db()

        self.assertNotEqual(xp1.start_date, xp2.start_date)
        self.assertNotEqual(xp1.end_date, xp2.end_date)

    def test_two_delayed_similar_experiments(self):
        fullname1 = "user/user/double/1/double"
        fullname2 = "user/user/double/1/double_split_2"

        xp1 = Experiment.objects.get(name=fullname1.split("/")[-1])
        xp2 = Experiment.objects.get(name=fullname2.split("/")[-1])

        self.prepare_databases(xp1.declaration)
        self.prepare_databases(xp2.declaration)

        schedule_experiment(xp1)

        start = time.time()
        while xp1.status != Experiment.RUNNING:
            self.assertTrue(time.time() - start < self.timeout)
            xp1.refresh_from_db()

        schedule_experiment(xp2)
        # xp2.refresh_from_db()

        start = time.time()
        while xp1.status != Experiment.DONE or xp2.status != Experiment.DONE:
            self.assertTrue(time.time() - start < self.timeout)
            xp1.refresh_from_db()
            xp2.refresh_from_db()

    def test_failed_experiment(self):
        fullname = "user/user/single/1/single_error"

        xp = Experiment.objects.get(name=fullname.split("/")[-1])

        self.prepare_databases(xp.declaration)

        schedule_experiment(xp)
        xp.refresh_from_db()

        start = time.time()
        while xp.status != Experiment.FAILED:
            self.assertTrue(time.time() - start < self.timeout)
            xp.refresh_from_db()

        b1 = xp.blocks.get(name="echo")
        b2 = xp.blocks.get(name="analysis")

        self.assertEqual(b1.status, Block.FAILED)
        self.assertEqual(b2.status, Block.CANCELLED)

    def test_failed_splitted_experiment(self):
        fullname = "user/user/double/1/double_error_split_2"

        xp = Experiment.objects.get(name=fullname.split("/")[-1])

        self.prepare_databases(xp.declaration)

        schedule_experiment(xp)
        xp.refresh_from_db()

        start = time.time()
        while xp.status != Experiment.FAILED:
            self.assertTrue(time.time() - start < self.timeout)
            xp.refresh_from_db()

        b1 = xp.blocks.get(name="echo1")
        b2 = xp.blocks.get(name="echo2")
        b3 = xp.blocks.get(name="analysis")

        self.assertEqual(b1.status, Block.DONE)
        self.assertEqual(b2.status, Block.FAILED)
        self.assertEqual(b3.status, Block.CANCELLED)

    def test_failed_mirror_experiments(self):
        fullname1 = "user/user/double/1/double_error"
        fullname2 = "user/user/double/1/double_error_split_2"

        xp1 = Experiment.objects.get(name=fullname1.split("/")[-1])
        xp2 = Experiment.objects.get(name=fullname2.split("/")[-1])

        self.prepare_databases(xp1.declaration)
        self.prepare_databases(xp2.declaration)

        schedule_experiment(xp1)
        schedule_experiment(xp2)

        xp1.refresh_from_db()
        xp2.refresh_from_db()

        start = time.time()
        while xp1.status != Experiment.FAILED or xp2.status != Experiment.FAILED:
            self.assertTrue(time.time() - start < self.timeout)
            xp1.refresh_from_db()
            xp2.refresh_from_db()

        b1 = xp1.blocks.get(name="echo1")
        b2 = xp1.blocks.get(name="echo2")
        b3 = xp1.blocks.get(name="analysis")

        self.assertEqual(b1.status, Block.DONE)
        self.assertEqual(b2.status, Block.FAILED)
        self.assertEqual(b3.status, Block.CANCELLED)

        b1 = xp2.blocks.get(name="echo1")
        b2 = xp2.blocks.get(name="echo2")
        b3 = xp2.blocks.get(name="analysis")

        self.assertEqual(b1.status, Block.DONE)
        self.assertEqual(b2.status, Block.FAILED)
        self.assertEqual(b3.status, Block.CANCELLED)


# ----------------------------------------------------------


618
@skipIf(settings.RUNNING_ON_CI, "Not runnable on the CI")
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
class TestCancellation(TestSchedulerBase):
    def setUp(self):
        super(TestCancellation, self).setUp()

        self.start_scheduling(0)
        self.start_worker("node1", 0)
        self.check_worker_status("node1", True)
        self.start_worker("node2", 0)
        self.check_worker_status("node2", True)

    def process(self, experiment_name, block_name=None):
        xp_name = experiment_name.split("/")[-1]

        xp = Experiment.objects.get(name=xp_name)

        self.prepare_databases(xp.declaration)

        schedule_experiment(xp)
        xp.refresh_from_db()

        start = time.time()
        while xp.status != Experiment.RUNNING:
            self.assertTrue(time.time() - start < self.timeout)
            xp.refresh_from_db()

        if block_name is not None:
            block = xp.blocks.get(name=block_name)

            start = time.time()
            while block.status != Block.PROCESSING:
                self.assertTrue(time.time() - start < self.timeout)
                block.refresh_from_db()

        cancel_experiment(xp)
        xp.refresh_from_db()

        start = time.time()
        while xp.status != Experiment.PENDING:
            self.assertTrue(time.time() - start < self.timeout)
            xp.refresh_from_db()

        self.assertTrue(xp.blocks.filter(status=Block.CANCELLED).count() > 0)
        self.assertEqual(xp.blocks.filter(status=Block.PENDING).count(), 0)
        self.assertEqual(xp.blocks.filter(status=Block.PROCESSING).count(), 0)

    def process2(self, experiment_name1, experiment_name2, cancel_index=0):
        xp1 = Experiment.objects.get(name=experiment_name1.split("/")[-1])
        xp2 = Experiment.objects.get(name=experiment_name2.split("/")[-1])

        self.prepare_databases(xp1.declaration)
        self.prepare_databases(xp2.declaration)

        schedule_experiment(xp1)
        schedule_experiment(xp2)
        xp1.refresh_from_db()
        xp2.refresh_from_db()

        start = time.time()
        while xp1.status != Experiment.RUNNING or xp2.status != Experiment.RUNNING:
            self.assertTrue(time.time() - start < self.timeout)
            xp1.refresh_from_db()
            xp2.refresh_from_db()

        if cancel_index == 0:
            xp_to_cancel = xp1
            xp_to_finish = xp2
        else:
            xp_to_cancel = xp2
            xp_to_finish = xp1

        cancel_experiment(xp_to_cancel)

        xp_to_cancel.refresh_from_db()
        xp_to_finish.refresh_from_db()

        start = time.time()
        while xp_to_cancel.status != Experiment.PENDING:
            self.assertTrue(time.time() - start < self.timeout)
            xp_to_cancel.refresh_from_db()

        xp_to_finish.refresh_from_db()

        start = time.time()
        while xp_to_finish.status != Experiment.DONE:
            self.assertTrue(time.time() - start < self.timeout)
            xp_to_finish.refresh_from_db()

        self.assertTrue(xp_to_cancel.blocks.filter(status=Block.CANCELLED).count() > 0)
        self.assertEqual(xp_to_cancel.blocks.filter(status=Block.PENDING).count(), 0)
        self.assertEqual(xp_to_cancel.blocks.filter(status=Block.PROCESSING).count(), 0)

        self.assertEqual(xp_to_finish.blocks.filter(status=Block.CANCELLED).count(), 0)
        self.assertEqual(xp_to_finish.blocks.filter(status=Block.PENDING).count(), 0)
        self.assertEqual(xp_to_finish.blocks.filter(status=Block.PROCESSING).count(), 0)

    def test_one_split_running(self):
        self.process("user/user/single/1/single_sleep_4")

    def test_one_split_of_two_in_a_block_running(self):
        self.stop_worker("node2")
        self.check_worker_status("node2", False)
        self.process("user/user/double/1/double_sleep_split_2", block_name="echo2")

    def test_two_splits_of_same_block_running(self):
        self.process("user/user/double/1/double_sleep_split_2", block_name="echo2")

    def test_two_splits_of_different_blocks_running(self):
        self.process("user/user/triangle/1/triangle_sleep_4")

    def test_mirror_block(self):
        self.stop_worker("node2")
        self.check_worker_status("node2", False)
        self.process2(
            "user/user/single/1/single_sleep_4",
            "user/user/single/1/single_sleep_5",
            cancel_index=1,
        )

    def test_mirrored_block(self):
        self.stop_worker("node2")
        self.check_worker_status("node2", False)
        self.process2(
            "user/user/single/1/single_sleep_4",
            "user/user/single/1/single_sleep_5",
            cancel_index=0,
        )