-
Notifications
You must be signed in to change notification settings - Fork 54.3k
fix: 修复单股推送模式下通知链路的共享实例并发复用问题 (#876) #899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e7bb6b5
fix(issue-876): [bug]-修复单股推送模式下通知链路的共享实例并发复用问题
ZhuLinsen 94dbfdd
fix(issue-876): [bug]-修复单股推送模式下通知链路的共享实例并发复用问题
ZhuLinsen aacc4dc
fix(review-feedback-899): address latest review comments
ZhuLinsen 32a2659
fix(review-feedback-899): address latest review comments
ZhuLinsen ef9ca17
fix: resolve changelog conflict on pr-899
d906a29
fix(review-feedback-899): address latest review comments
ZhuLinsen 654ba7c
fix(review-feedback-899): address latest review comments
ZhuLinsen af1633c
fix(review-feedback-899): address latest review comments
ZhuLinsen fed766e
fix(review-feedback-899): address latest review comments
ZhuLinsen 63b15fb
fix(review-feedback-899): address latest review comments
ZhuLinsen 02c2ee4
Merge branch 'main' into autocode/issue-876-bug
ZhuLinsen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """ | ||
| Regression tests for single-stock notification thread safety. | ||
| """ | ||
|
|
||
| import os | ||
| import sys | ||
| import threading | ||
| import time | ||
| import unittest | ||
| from unittest.mock import MagicMock | ||
|
|
||
| sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) | ||
|
|
||
| from tests.litellm_stub import ensure_litellm_stub | ||
|
|
||
| ensure_litellm_stub() | ||
|
|
||
| from src.analyzer import AnalysisResult | ||
| from src.core.pipeline import StockAnalysisPipeline | ||
|
|
||
|
|
||
| def _make_result(code: str) -> AnalysisResult: | ||
| return AnalysisResult( | ||
| code=code, | ||
| name=f"股票{code}", | ||
| sentiment_score=80, | ||
| trend_prediction="看多", | ||
| operation_advice="持有", | ||
| analysis_summary="测试结果", | ||
| ) | ||
|
|
||
|
|
||
| class _CriticalSectionTrackingNotifier: | ||
| def __init__(self): | ||
| self._state_lock = threading.Lock() | ||
| self._inflight = 0 | ||
| self.max_inflight = 0 | ||
| self.calls = [] | ||
| self.is_available = MagicMock(return_value=True) | ||
| self.generate_single_stock_report = MagicMock( | ||
| side_effect=self._generate_single_stock_report | ||
| ) | ||
| self.send = MagicMock(side_effect=self._send) | ||
|
|
||
| def _enter(self, stage: str, code: str) -> None: | ||
| with self._state_lock: | ||
| self._inflight += 1 | ||
| self.max_inflight = max(self.max_inflight, self._inflight) | ||
|
|
||
| self.calls.append((stage, code, threading.current_thread().name)) | ||
| time.sleep(0.02) | ||
|
|
||
| with self._state_lock: | ||
| self._inflight -= 1 | ||
|
|
||
| def _generate_single_stock_report(self, result: AnalysisResult) -> str: | ||
| self._enter("generate", result.code) | ||
| return f"single:{result.code}" | ||
|
|
||
| def _send(self, content: str, email_stock_codes=None) -> bool: | ||
| stock_code = (email_stock_codes or ["unknown"])[0] | ||
| self._enter("send", stock_code) | ||
| return True | ||
|
|
||
|
|
||
| class TestPipelineSingleNotifyThreadSafety(unittest.TestCase): | ||
| def test_process_single_stock_serializes_direct_notification_path(self): | ||
| pipeline = StockAnalysisPipeline.__new__(StockAnalysisPipeline) | ||
| pipeline.fetch_and_save_stock_data = MagicMock(return_value=(True, None)) | ||
| pipeline.notifier = _CriticalSectionTrackingNotifier() | ||
|
|
||
| notify_barrier = threading.Barrier(2) | ||
|
|
||
| def _analyze(code, report_type, query_id): | ||
| notify_barrier.wait(timeout=10) | ||
| return _make_result(code) | ||
|
|
||
| pipeline.analyze_stock = MagicMock(side_effect=_analyze) | ||
|
|
||
| results = [] | ||
| result_lock = threading.Lock() | ||
|
|
||
| def _worker(code: str) -> None: | ||
| result = pipeline.process_single_stock( | ||
| code=code, | ||
| single_stock_notify=True, | ||
| analysis_query_id=f"query-{code}", | ||
| ) | ||
| with result_lock: | ||
| results.append(result) | ||
|
|
||
| threads = [ | ||
| threading.Thread(target=_worker, args=(code,), name=f"notify-{code}") | ||
| for code in ("000001", "600519") | ||
| ] | ||
|
|
||
| for thread in threads: | ||
| thread.start() | ||
| for thread in threads: | ||
| thread.join() | ||
|
|
||
| self.assertEqual(len(results), 2) | ||
| self.assertTrue(all(result is not None for result in results)) | ||
| self.assertEqual(pipeline.notifier.generate_single_stock_report.call_count, 2) | ||
| self.assertEqual(pipeline.notifier.send.call_count, 2) | ||
| self.assertEqual(pipeline.notifier.max_inflight, 1) | ||
| self.assertCountEqual( | ||
| [(stage, code) for stage, code, _ in pipeline.notifier.calls], | ||
| [ | ||
| ("generate", "000001"), | ||
| ("send", "000001"), | ||
| ("generate", "600519"), | ||
| ("send", "600519"), | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This notification call now runs inside the same
as_completedloop that appliesanalysis_delay, so whenANALYSIS_DELAY > 0the second and later completed stocks can be held back by one delay interval each even if their analysis already finished. Insingle_stock_notifymode this regresses the prior “notify as soon as a stock finishes” behavior and can introduce substantial user-visible lag on larger stock lists.Useful? React with 👍 / 👎.