Repository: ClickHouse/ClickHouse
File: tests/integration/test_storage_delta/test.py (Python)

Code before:
    print(f"First shutdown check hits: {first_check_hits}")
    print(f"Queue shutdown check hits: {queue_check_hits}")

    assert first_check_hits or queue_check_hits

    assert 1 == int(instance.query(
        f"SELECT count() FROM system.text_log WHERE query_id = '{query_id}' AND message LIKE '%List batch size is 1/1, shutdown: true%'"
    ))


    # Early return should scan significantly fewer files
    # With s3_list_object_keys_size=1, queue pauses frequently forcing shutdown checks
    # Should stop very early after consuming just a few files
    assert scanned_files < full_scan_files, \
        f"Early return should scan fewer files: {scanned_files} >= {full_scan_files}"
    # 3 because:
    # we have async reader creation with 2 existing readers at a moment of time,
    # each calls next() and consumes 2 files from the scan.
    # It takes 1 file for the query to stop because of LIMIT 1.
    # But because scan is also asynchronous and continues once batch limit is not reached,
    # we get +1 scanned file.
    assert scanned_files == 3, \
        f"Early return should scan 3 files with LIMIT 1, but scanned {scanned_files}"




Review comment (bug):
These assertions look timing/scheduling dependent and may be flaky across environments: (1) log count being exactly `1` can fail if the message is emitted multiple times, and (2) `scanned_files == 3` is a very strict bound for an async pipeline. Consider loosening the checks to “at least once” for the log, and using an upper bound/range for `scanned_files` (while keeping the key guarantee that `scanned_files < full_scan_files`).
```suggestion
    assert int(
        instance.query(
            f"SELECT count() FROM system.text_log WHERE query_id = '{query_id}' AND message LIKE '%List batch size is 1/1, shutdown: true%'"
        )
    ) >= 1

    # Early return should scan significantly fewer files
    # With s3_list_object_keys_size=1, queue pauses frequently forcing shutdown checks
    # Should stop very early after consuming just a few files
    assert scanned_files < full_scan_files, \
        f"Early return should scan fewer files: {scanned_files} >= {full_scan_files}"
    # Expectation is around 3 because:
    # we have async reader creation with 2 existing readers at a moment of time,
    # each calls next() and consumes 2 files from the scan.
    # It takes 1 file for the query to stop because of LIMIT 1.
    # But because scan is also asynchronous and continues once batch limit is not reached,
    # we can get a small variation in the exact number of scanned files.
    # Assert that we scanned a small number of files, within a reasonable range.
    assert 1 <= scanned_files <= 6, \
        f"Early return should scan only a few files with LIMIT 1, but scanned {scanned_files}"
```

Code after:
    first_check_hits = int(first_check_hits)
    queue_check_hits = int(queue_check_hits)

    print(f"First shutdown check hits: {first_check_hits}")
    print(f"Queue shutdown check hits: {queue_check_hits}")

    assert first_check_hits > 0 or queue_check_hits > 0

    assert 1 == int(instance.query(
        f"SELECT count() FROM system.text_log WHERE query_id = '{query_id}' AND message LIKE '%List batch size is 1/1, shutdown: true%'"
    ))


    # Early return should scan significantly fewer files
    # With s3_list_object_keys_size=1, queue pauses frequently forcing shutdown checks
    # Should stop very early after consuming just a few files
    assert scanned_files < full_scan_files, \
        f"Early return should scan fewer files: {scanned_files} >= {full_scan_files}"
    # 3 because:
    # we have async reader creation with 2 existing readers at a moment of time,
    # each calls next() and consumes 2 files from the scan.
    # It takes 1 file for the query to stop because of LIMIT 1.
    # But because scan is also asynchronous and continues once batch limit is not reached,
    # we get +1 scanned file.
    assert scanned_files == 3, \
        f"Early return should scan 3 files with LIMIT 1, but scanned {scanned_files}"