|
| 1 | +""" |
| 2 | +Tests for public ComfyAPI and ComfyAPISync functions. |
| 3 | +
|
| 4 | +These tests verify that the public API methods work correctly in both sync and async contexts, |
| 5 | +ensuring that the sync wrapper generation (via get_type_hints() in async_to_sync.py) correctly |
| 6 | +handles string annotations from 'from __future__ import annotations'. |
| 7 | +""" |
| 8 | + |
| 9 | +import pytest |
| 10 | +import time |
| 11 | +import subprocess |
| 12 | +import torch |
| 13 | +from pytest import fixture |
| 14 | +from comfy_execution.graph_utils import GraphBuilder |
| 15 | +from tests.execution.test_execution import ComfyClient |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.execution |
| 19 | +class TestPublicAPI: |
| 20 | + """Test suite for public ComfyAPI and ComfyAPISync methods.""" |
| 21 | + |
| 22 | + @fixture(scope="class", autouse=True) |
| 23 | + def _server(self, args_pytest): |
| 24 | + """Start ComfyUI server for testing.""" |
| 25 | + pargs = [ |
| 26 | + 'python', 'main.py', |
| 27 | + '--output-directory', args_pytest["output_dir"], |
| 28 | + '--listen', args_pytest["listen"], |
| 29 | + '--port', str(args_pytest["port"]), |
| 30 | + '--extra-model-paths-config', 'tests/execution/extra_model_paths.yaml', |
| 31 | + '--cpu', |
| 32 | + ] |
| 33 | + p = subprocess.Popen(pargs) |
| 34 | + yield |
| 35 | + p.kill() |
| 36 | + torch.cuda.empty_cache() |
| 37 | + |
| 38 | + @fixture(scope="class", autouse=True) |
| 39 | + def shared_client(self, args_pytest, _server): |
| 40 | + """Create shared client with connection retry.""" |
| 41 | + client = ComfyClient() |
| 42 | + n_tries = 5 |
| 43 | + for i in range(n_tries): |
| 44 | + time.sleep(4) |
| 45 | + try: |
| 46 | + client.connect(listen=args_pytest["listen"], port=args_pytest["port"]) |
| 47 | + break |
| 48 | + except ConnectionRefusedError: |
| 49 | + if i == n_tries - 1: |
| 50 | + raise |
| 51 | + yield client |
| 52 | + del client |
| 53 | + torch.cuda.empty_cache() |
| 54 | + |
| 55 | + @fixture |
| 56 | + def client(self, shared_client, request): |
| 57 | + """Set test name for each test.""" |
| 58 | + shared_client.set_test_name(f"public_api[{request.node.name}]") |
| 59 | + yield shared_client |
| 60 | + |
| 61 | + @fixture |
| 62 | + def builder(self, request): |
| 63 | + """Create GraphBuilder for each test.""" |
| 64 | + yield GraphBuilder(prefix=request.node.name) |
| 65 | + |
| 66 | + def test_sync_progress_update_executes(self, client: ComfyClient, builder: GraphBuilder): |
| 67 | + """Test that TestSyncProgressUpdate executes without errors. |
| 68 | +
|
| 69 | + This test validates that api_sync.execution.set_progress() works correctly, |
| 70 | + which is the primary code path fixed by adding get_type_hints() to async_to_sync.py. |
| 71 | + """ |
| 72 | + g = builder |
| 73 | + image = g.node("StubImage", content="BLACK", height=256, width=256, batch_size=1) |
| 74 | + |
| 75 | + # Use TestSyncProgressUpdate with short sleep |
| 76 | + progress_node = g.node("TestSyncProgressUpdate", |
| 77 | + value=image.out(0), |
| 78 | + sleep_seconds=0.5) |
| 79 | + output = g.node("SaveImage", images=progress_node.out(0)) |
| 80 | + |
| 81 | + # Execute workflow |
| 82 | + result = client.run(g) |
| 83 | + |
| 84 | + # Verify execution |
| 85 | + assert result.did_run(progress_node), "Progress node should have executed" |
| 86 | + assert result.did_run(output), "Output node should have executed" |
| 87 | + |
| 88 | + # Verify output |
| 89 | + images = result.get_images(output) |
| 90 | + assert len(images) == 1, "Should have produced 1 image" |
| 91 | + |
| 92 | + def test_async_progress_update_executes(self, client: ComfyClient, builder: GraphBuilder): |
| 93 | + """Test that TestAsyncProgressUpdate executes without errors. |
| 94 | +
|
| 95 | + This test validates that await api.execution.set_progress() works correctly |
| 96 | + in async contexts. |
| 97 | + """ |
| 98 | + g = builder |
| 99 | + image = g.node("StubImage", content="WHITE", height=256, width=256, batch_size=1) |
| 100 | + |
| 101 | + # Use TestAsyncProgressUpdate with short sleep |
| 102 | + progress_node = g.node("TestAsyncProgressUpdate", |
| 103 | + value=image.out(0), |
| 104 | + sleep_seconds=0.5) |
| 105 | + output = g.node("SaveImage", images=progress_node.out(0)) |
| 106 | + |
| 107 | + # Execute workflow |
| 108 | + result = client.run(g) |
| 109 | + |
| 110 | + # Verify execution |
| 111 | + assert result.did_run(progress_node), "Async progress node should have executed" |
| 112 | + assert result.did_run(output), "Output node should have executed" |
| 113 | + |
| 114 | + # Verify output |
| 115 | + images = result.get_images(output) |
| 116 | + assert len(images) == 1, "Should have produced 1 image" |
| 117 | + |
| 118 | + def test_sync_and_async_progress_together(self, client: ComfyClient, builder: GraphBuilder): |
| 119 | + """Test both sync and async progress updates in same workflow. |
| 120 | +
|
| 121 | + This test ensures that both ComfyAPISync and ComfyAPI can coexist and work |
| 122 | + correctly in the same workflow execution. |
| 123 | + """ |
| 124 | + g = builder |
| 125 | + image1 = g.node("StubImage", content="BLACK", height=256, width=256, batch_size=1) |
| 126 | + image2 = g.node("StubImage", content="WHITE", height=256, width=256, batch_size=1) |
| 127 | + |
| 128 | + # Use both types of progress nodes |
| 129 | + sync_progress = g.node("TestSyncProgressUpdate", |
| 130 | + value=image1.out(0), |
| 131 | + sleep_seconds=0.3) |
| 132 | + async_progress = g.node("TestAsyncProgressUpdate", |
| 133 | + value=image2.out(0), |
| 134 | + sleep_seconds=0.3) |
| 135 | + |
| 136 | + # Create outputs |
| 137 | + output1 = g.node("SaveImage", images=sync_progress.out(0)) |
| 138 | + output2 = g.node("SaveImage", images=async_progress.out(0)) |
| 139 | + |
| 140 | + # Execute workflow |
| 141 | + result = client.run(g) |
| 142 | + |
| 143 | + # Both should execute successfully |
| 144 | + assert result.did_run(sync_progress), "Sync progress node should have executed" |
| 145 | + assert result.did_run(async_progress), "Async progress node should have executed" |
| 146 | + assert result.did_run(output1), "First output node should have executed" |
| 147 | + assert result.did_run(output2), "Second output node should have executed" |
| 148 | + |
| 149 | + # Verify outputs |
| 150 | + images1 = result.get_images(output1) |
| 151 | + images2 = result.get_images(output2) |
| 152 | + assert len(images1) == 1, "Should have produced 1 image from sync node" |
| 153 | + assert len(images2) == 1, "Should have produced 1 image from async node" |
0 commit comments