mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-10-24 17:35:19 +08:00
fix(telemetry): skip unrouted events and preserve matched deliveries
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""Independent bot subscriptions must not change primary route delivery."""
|
||||
|
||||
import asyncio
|
||||
import contextvars
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
@@ -8,6 +9,7 @@ import pytest
|
||||
from langbot_plugin.api.entities.builtin.platform.events import MemberJoinedEvent
|
||||
|
||||
from langbot.pkg.platform.botmgr import RuntimeBot
|
||||
from langbot.pkg.persistence.tenant_uow import PersistenceScope, PersistenceScopeBoundary
|
||||
|
||||
|
||||
def make_bot(subscriptions):
|
||||
@@ -105,3 +107,46 @@ async def test_missing_processor_is_logged_without_unscoped_lookup():
|
||||
await bot._dispatch_plugin_subscription(MemberJoinedEvent(), None, 'missing')
|
||||
bot.ap.agent_service.get_agent.assert_awaited_once_with('workspace', 'missing')
|
||||
assert bot._record_event_route_trace.await_args.kwargs['status'] == 'failed'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('primary_fails', [False, True])
|
||||
async def test_cloud_deliveries_own_their_scope_and_preserve_parent(primary_fails):
|
||||
bot = make_bot([{'processor_uuid': 'plugin'}])
|
||||
bot.workspace_uuid = 'workspace'
|
||||
active_scope = contextvars.ContextVar('test_event_scope', default=None)
|
||||
active_transaction = contextvars.ContextVar('test_event_transaction', default=None)
|
||||
|
||||
def tenant_scope(workspace):
|
||||
return PersistenceScopeBoundary(
|
||||
PersistenceScope.workspace(workspace),
|
||||
active_scope=active_scope,
|
||||
active_transaction=active_transaction,
|
||||
)
|
||||
|
||||
bot.ap = SimpleNamespace(
|
||||
persistence_mgr=SimpleNamespace(mode=SimpleNamespace(value='cloud_runtime'), tenant_scope=tenant_scope)
|
||||
)
|
||||
seen = []
|
||||
|
||||
async def delivery(event, adapter, *args, **kwargs):
|
||||
state = active_scope.get()
|
||||
assert state.owner_task is asyncio.current_task()
|
||||
assert state.scope == PersistenceScope.workspace('workspace')
|
||||
seen.append(state)
|
||||
await asyncio.sleep(0)
|
||||
assert active_scope.get() is state
|
||||
if primary_fails and not args:
|
||||
raise ValueError('primary failed')
|
||||
|
||||
bot._dispatch_eba_event_to_processor = delivery
|
||||
bot._dispatch_plugin_subscription = delivery
|
||||
async with tenant_scope('workspace'):
|
||||
parent = active_scope.get()
|
||||
await bot._handle_platform_event(MemberJoinedEvent(), None)
|
||||
assert active_scope.get() is parent
|
||||
assert parent.owner_task is asyncio.current_task()
|
||||
assert active_scope.get() is None
|
||||
assert len(seen) == 2
|
||||
assert seen[0] is not seen[1]
|
||||
assert all(state is not parent and state.depth == 0 for state in seen)
|
||||
assert bot.logger.error.await_count == int(primary_fails)
|
||||
|
||||
Reference in New Issue
Block a user