File size: 1,609 Bytes
b701455
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import pytest
import server
from src.FileManaging import ImageSaver


@pytest.mark.asyncio
async def test_large_coalesced_batch_1024(monkeypatch):
    # Set small chunk limit to force many chunks
    server.LD_MAX_IMAGES_PER_GROUP = 32

    async def immediate_to_thread(func, /, *args, **kwargs):
        return func(*args, **kwargs)

    monkeypatch.setattr(server.asyncio, "to_thread", immediate_to_thread)

    def fake_pipeline(**kwargs):
        per_sample_info = kwargs.get("per_sample_info", [])
        results = {}
        for info in per_sample_info:
            rid = info["request_id"]
            filename = f"{rid}_{len(results.get(rid, []))}_img.png"
            ImageSaver.store_image_bytes(f"LD-REQ-{rid}", filename, "Classic", b"PNGDATA")
            results.setdefault(rid, []).append({"filename": filename, "subfolder": "Classic"})
        return {"batched_results": results}

    monkeypatch.setattr(server, "pipeline", fake_pipeline)

    items = []
    # 64 requests, each asking for 16 images = 1024 total
    for i in range(64):
        req = server.GenerateRequest(prompt=f"p{i}", num_images=16)
        pr = server.PendingRequest(req, request_id=f"r{i:04d}")
        items.append(pr)

    buf = server.GenerationBuffer()

    await buf._process_group(items)

    # All completed
    for p in items:
        assert p.future.done()
        res = p.future.result()
        assert isinstance(res, dict)
        assert "images" in res and len(res["images"]) == 16

    # Buffer emptied for all
    for i in range(64):
        assert ImageSaver.pop_image_bytes(f"LD-REQ-r{i:04d}") == []