Skip to content

test_parallely is still flaky #74

@aldbr

Description

@aldbr

We noticed that test_parallely could still be flaky under certain conditions.
Instead of basing it on time, we should probably focus on the number of cores used during the execution. Here is a potential solution (from Claude):

import subprocess
import time
import psutil
import pytest

def test_run_job_parallely():
    """Verify that jobs actually execute in parallel using multiple cores."""
    
    # Test 1: Sequential (taskset) should use ~100% CPU (one core)
    command_seq = [
        "taskset", "-c", "0",
        "dirac-cwl", "job", "submit",
        "test/workflows/parallel/description.cwl"
    ]
    
    process = subprocess.Popen(command_seq)
    seq_cpu_samples = []
    
    while process.poll() is None:
        try:
            # Get CPU usage of process and all children
            parent = psutil.Process(process.pid)
            cpu_percent = parent.cpu_percent(interval=0.1)
            for child in parent.children(recursive=True):
                cpu_percent += child.cpu_percent(interval=0.1)
            seq_cpu_samples.append(cpu_percent)
        except psutil.NoSuchProcess:
            break
        time.sleep(0.1)
    
    max_seq_cpu = max(seq_cpu_samples) if seq_cpu_samples else 0
    
    # Test 2: Parallel should use >150% CPU (multiple cores)
    command_par = [
        "dirac-cwl", "job", "submit",
        "test/workflows/parallel/description.cwl"
    ]
    
    process = subprocess.Popen(command_par)
    par_cpu_samples = []
    
    while process.poll() is None:
        try:
            parent = psutil.Process(process.pid)
            cpu_percent = parent.cpu_percent(interval=0.1)
            for child in parent.children(recursive=True):
                cpu_percent += child.cpu_percent(interval=0.1)
            par_cpu_samples.append(cpu_percent)
        except psutil.NoSuchProcess:
            break
        time.sleep(0.1)
    
    max_par_cpu = max(par_cpu_samples) if par_cpu_samples else 0
    
    # Assertions
    assert max_seq_cpu < 150, \
        f"Sequential execution should use <150% CPU, got {max_seq_cpu:.1f}%"
    
    assert max_par_cpu > 150, \
        f"Parallel execution should use >150% CPU (multiple cores), got {max_par_cpu:.1f}%"
    
    print(f"Sequential: {max_seq_cpu:.1f}% CPU, Parallel: {max_par_cpu:.1f}% CPU")

Metadata

Metadata

Labels

points:1Trivial, 1 day, very clear

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions