perf(fulfillment): Webhook 新订单即时选号
This commit is contained in:
@@ -104,6 +104,70 @@ def test_auto_allocator_only_scans_ready_unassigned_tasks(db):
|
||||
assert not auto_allocator._running_task_ids
|
||||
|
||||
|
||||
def test_auto_allocator_skips_task_already_claimed_by_another_caller(db):
|
||||
actor = User(username="auto-running-actor", password_hash="h", role="super_admin")
|
||||
db.add(
|
||||
PlatformFulfillmentSetting(
|
||||
id=1,
|
||||
lease_duration_minutes=None,
|
||||
order_match_started_at=datetime.now(UTC) - timedelta(days=1),
|
||||
)
|
||||
)
|
||||
task = PlatformFulfillmentTask(
|
||||
source_system="order_site",
|
||||
source_order_id="running-1",
|
||||
source_order_item_id="running-1",
|
||||
platform_order_id="AUTO-RUNNING",
|
||||
internal_sku_key="skin",
|
||||
commodity_id="commodity-1",
|
||||
execution_status="ready",
|
||||
binding_status="not_started",
|
||||
exchange_status="not_started",
|
||||
paid_at=datetime.now(UTC),
|
||||
next_allocate_at=datetime.now(UTC),
|
||||
updated_at=datetime.now(UTC),
|
||||
)
|
||||
db.add_all([actor, task])
|
||||
db.commit()
|
||||
auto_allocator._running_task_ids.add(task.id)
|
||||
try:
|
||||
with patch.object(
|
||||
auto_allocator.service,
|
||||
"allocate_task",
|
||||
side_effect=AssertionError("running task must not be allocated twice"),
|
||||
):
|
||||
result = auto_allocator.run_auto_allocation_once(db, limit=1)
|
||||
finally:
|
||||
auto_allocator._running_task_ids.discard(task.id)
|
||||
|
||||
assert result == {"checked": 0, "allocated": 0, "failed": 0}
|
||||
|
||||
|
||||
def test_immediate_allocation_runs_and_wakes_background_retry(db):
|
||||
expected = {"checked": 1, "allocated": 1, "failed": 0}
|
||||
session = object()
|
||||
with (
|
||||
patch.object(
|
||||
auto_allocator,
|
||||
"run_auto_allocation_once",
|
||||
return_value=expected,
|
||||
) as run,
|
||||
patch.object(auto_allocator, "wake_auto_allocator") as wake,
|
||||
):
|
||||
result = auto_allocator.run_immediate_allocation(
|
||||
session,
|
||||
task_ids=[9, 9, 10],
|
||||
)
|
||||
|
||||
assert result == expected
|
||||
run.assert_called_once_with(
|
||||
session,
|
||||
limit=2,
|
||||
task_ids=[9, 10],
|
||||
)
|
||||
wake.assert_called_once_with([9, 10])
|
||||
|
||||
|
||||
def test_auto_allocator_schedules_retry_only_after_failed_allocation(db):
|
||||
actor = User(username="auto-retry-actor", password_hash="h", role="super_admin")
|
||||
db.add(
|
||||
|
||||
Reference in New Issue
Block a user