Add KopfTask and KopfThread programmatic testing runners#1292
Draft
nolar wants to merge 8 commits into
Draft
Conversation
e7e82ba to
096e620
Compare
2ffdc6c to
471117f
Compare
6bfea98 to
8e982b3
Compare
107bf11 to
842acd7
Compare
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]>
…nner 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]>
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]>
Signed-off-by: Sergey Vasilyev <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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).