Skip to content

Commit

Permalink
Add test for cog stream draining file logging
Browse files Browse the repository at this point in the history
* This tests exposes the issue that cog stream
drainer can’t drain due to file logging in the
logging module.
  • Loading branch information
8W9aG committed Nov 25, 2024
1 parent cf0f8b2 commit 7126313
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build:
python_version: "3.11"
python_packages:
- "pillow==8.3.2"
predict: "predict.py:Predictor"
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from cog import BasePredictor, File
import logging
import os
from PIL import Image


class Predictor(BasePredictor):
def setup(self):
logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s")
rootLogger = logging.getLogger()

fileHandler = logging.FileHandler("{0}/{1}.log".format(os.path.dirname(__file__), "mylog.log"))
fileHandler.setFormatter(logFormatter)
rootLogger.addHandler(fileHandler)

consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
rootLogger.addHandler(consoleHandler)

def predict(self) -> list[Image]:
logging.info("Do some logging.")
return []
16 changes: 16 additions & 0 deletions test-integration/test_integration/test_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,19 @@ def test_predict_with_subprocess_in_setup(fixture_name):
assert response.status_code == 200, str(response)

assert busy_count < 10


def test_file_logging_stream_drain(tmpdir_factory):
project_dir = Path(__file__).parent / "fixtures/file-logging-stream-drain-project"
out_dir = pathlib.Path(tmpdir_factory.mktemp("project"))
shutil.copytree(project_dir, out_dir, dirs_exist_ok=True)
cmd = ["cog", "--debug", "predict"]

subprocess.run(
cmd,
cwd=out_dir,
check=True,
capture_output=True,
text=True,
timeout=DEFAULT_TIMEOUT,
)

0 comments on commit 7126313

Please sign in to comment.