Skip to content

Commit

Permalink
tests(session): migrate span processor tests to exporter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
teocns committed Nov 26, 2024
1 parent 1cdb18f commit 5a75eda
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
47 changes: 42 additions & 5 deletions tests/session/test_exporter.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import pytest
from unittest.mock import Mock, patch
from uuid import uuid4
from opentelemetry.sdk.trace.export import SpanExportResult
from opentelemetry.trace import SpanKind

import pytest
from opentelemetry.sdk.trace import ReadableSpan
from opentelemetry.sdk.trace.export import BatchSpanProcessor, SpanExportResult
from opentelemetry.trace import SpanKind

from agentops.session.exporter import SessionExporter
from agentops.event import Event, ErrorEvent
from agentops.config import Configuration
from agentops.event import ErrorEvent, Event
from agentops.session import Session
from agentops.session.exporter import SessionExporter


@pytest.fixture
Expand Down Expand Up @@ -121,3 +122,39 @@ def test_export_error_handling(exporter, mock_session):

result = exporter.export([span])
assert result == SpanExportResult.FAILURE


def test_span_processor_config(mock_session):
"""Test that BatchSpanProcessor is configured correctly for testing"""
# Create a mock BatchSpanProcessor instead of using real Session
mock_processor = Mock(spec=BatchSpanProcessor)
mock_processor._max_export_batch_size = 1
mock_processor._schedule_delay_millis = 0

with patch("agentops.session.Session._span_processor", mock_processor):
session = Session(mock_session.config)
# Verify BatchSpanProcessor configuration
assert session._span_processor._max_export_batch_size == 1
assert session._span_processor._schedule_delay_millis == 0


def test_event_batching(mock_session):
"""Test that events are properly batched for export"""
mock_processor = Mock(spec=BatchSpanProcessor)
mock_processor.on_end = Mock()

with patch("agentops.session.Session._span_processor", mock_processor), patch(
"agentops.session.Session._tracer_provider"
) as mock_provider:
session = Session(mock_session.config)
session._span_processor = mock_processor # Explicitly set the processor

# Create mock events
mock_events = [Mock(spec=Event) for _ in range(3)]

# Record events
for event in mock_events:
session.record(event)

# Verify batching
assert mock_processor.on_end.call_count == 3
21 changes: 0 additions & 21 deletions tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,27 +276,6 @@ def test_get_analytics(self, mock_req):
session.end_session(end_state="Success")
agentops.end_all_sessions()

def test_span_processor_config(self):
session = agentops.start_session()

# Verify BatchSpanProcessor is configured for immediate export in tests
processor = session._span_processor
assert processor._max_export_batch_size == 1
assert processor._schedule_delay_millis == 0

def test_event_batching(self):
with patch("agentops.session.exporter.BatchSpanProcessor") as mock_processor:
session = agentops.start_session()

# Record multiple events
events = [ActionEvent(f"event_{i}") for i in range(3)]
for event in events:
session.record(event)

# Verify events were batched correctly
mock_processor.return_value.on_end.assert_called()
assert len(mock_processor.return_value.on_end.call_args[0][0]) == 3


class TestMultiSessions:
def setup_method(self):
Expand Down

0 comments on commit 5a75eda

Please sign in to comment.