Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add KopfTask and KopfThread programmatic testing runners#1292

Draft
nolar wants to merge 8 commits into
mainfrom
testing-runners
Draft

Add KopfTask and KopfThread programmatic testing runners#1292
nolar wants to merge 8 commits into
mainfrom
testing-runners

Conversation

@nolar
Copy link
Copy Markdown
Owner

@nolar nolar commented Mar 24, 2026

Based on the experience drafting unit-tests for operators in this PR, some tasks create a lot of bolierplate and hassle. Kopf's goal is to remove hassle. So, this seems like a good addition — to run an operator in a programmatic way instead of calling via the simulated CLI KopfRunner (that one remains).

@nolar nolar added the enhancement New feature or request label Mar 24, 2026
@nolar nolar changed the title Testing runners Add KopfTask and KopfThread programmatic testing runners Mar 24, 2026
@nolar nolar force-pushed the testing-runners branch 15 times, most recently from e7e82ba to 096e620 Compare March 28, 2026 11:39
@nolar nolar force-pushed the testing-runners branch 5 times, most recently from 2ffdc6c to 471117f Compare April 6, 2026 10:50
@nolar nolar force-pushed the testing-runners branch from 471117f to e725c01 Compare April 14, 2026 10:22
@nolar nolar force-pushed the testing-runners branch 4 times, most recently from 6bfea98 to 8e982b3 Compare April 15, 2026 14:27
@nolar nolar force-pushed the testing-runners branch 4 times, most recently from 107bf11 to 842acd7 Compare April 17, 2026 12:51
nolar and others added 5 commits April 17, 2026 14:56
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Signed-off-by: Sergey Vasilyev <[email protected]>
KopfRunner enters the operator through the CLI, requiring module paths and Click invocation. The new runners enter via operator() directly, making them suitable for programmatic tests where the handlers are already registered in the process: KopfThread as a sync context manager running in a background thread, and KopfTask as an async context manager running as a background asyncio task. Both inject a stop flag if not provided, set it on context exit for graceful shutdown, and support timeout and reraise semantics matching KopfRunner.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Signed-off-by: Sergey Vasilyev <[email protected]>
The Flag type union and the wait_flag/raise_flag/check_flag match blocks only handled in-process primitives (asyncio, threading, concurrent.futures). The upcoming KopfCLI subprocess runner needs to pass a `multiprocessing.Event` across process boundaries as a stop flag. This extends all three functions with a dedicated `multiprocessing.synchronize.Event` arm — kept separate from `threading.Event` despite the identical API, since they are semantically distinct (in-process vs cross-process).

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Signed-off-by: Sergey Vasilyev <[email protected]>
…lation

KopfRunner uses Click's CliRunner in a thread, which shares sys.modules with the parent process. This breaks module-based loading (`kopf run -m mymodule`) because importlib.import_module() is a no-op for already-imported modules — decorators don't re-execute, the isolated registry stays empty, and the operator runs with no handlers.

KopfCLI spawns the operator in a child process via multiprocessing with the 'spawn' start method, giving it a fresh Python interpreter where imports execute from scratch. It supports two shutdown modes: a cross-process stop flag (default, clean exit) and signal-based shutdown (SIGTERM/SIGINT, testing the production shutdown path). Output is captured via an os-level pipe with a background reader thread, readable both during the run and after exit.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: Sergey Vasilyev <[email protected]>
@nolar nolar force-pushed the testing-runners branch from 842acd7 to 766e60c Compare April 17, 2026 13:36
nolar and others added 3 commits April 17, 2026 15:51
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: Sergey Vasilyev <[email protected]>
KopfRunner runs the CLI in a thread sharing sys.modules with the parent, which breaks module-based loading due to lack of import isolation. Emit a DeprecationWarning pointing users to the appropriate replacements and the documentation.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Signed-off-by: Sergey Vasilyev <[email protected]>
@nolar nolar force-pushed the testing-runners branch from 766e60c to 27fa628 Compare April 17, 2026 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant