executor_test.go

Overview

The executor_test.go file contains unit tests for the Executor component of the system, which is responsible for executing AI agent tasks and managing their lifecycle in the context of the Agent-To-Agent (A2A) protocol. The tests verify the behavior of the executor when processing tasks through the agent runtime, handling sessions, managing event queues, and dealing with cancellation requests.

The file primarily focuses on the following:

This testing file interacts with components such as the agent.Agent interface, the session.Service for session management, and the eventqueue.Queue for event handling. It uses mock implementations and replay agents to simulate various scenarios and validate the executor's response.


Components and Key Elements

Types

testQueue

A test double implementing the eventqueue.Queue interface.

testSessionService

A test double wrapping the session.Service.

eventIndex

A simple struct representing an index position in the event queue to trigger failures.


Functions

newEventReplayAgent(events []*session.Event, failWith error) (agent.Agent, error)

Creates a mock agent.Agent which replays a predefined sequence of session events.


Test Functions

TestExecutor_Execute(t *testing.T)

Tests the Executor.Execute method's behavior across multiple scenarios defined as test cases.


TestExecutor_Cancel(t *testing.T)

Tests the Executor.Cancel method.


TestExecutor_SessionReuse(t *testing.T)

Tests session reuse behavior across executions.


Important Implementation Details and Algorithms


Interaction with Other System Components


Diagram: Executor Test Structure and Workflow

flowchart TD
subgraph Test Setup
TC[Test Cases]
Agent[newEventReplayAgent]
Session[testSessionService]
Queue[testQueue]
end
subgraph Executor
Exec[Executor]
Runner[runner.Config]
end
TC -->|defines inputs| Agent
TC -->|sets failure flags| Session
TC -->|sets failure flags| Queue
Agent --> Exec
Session --> Exec
Queue --> Exec
Runner --> Exec
Exec -->|calls Execute/Cancel| Agent
Exec -->|uses| Session
Exec -->|writes events| Queue
Exec -->|generates events| Events[a2a.Events]
Events -->|compare| TC
click Agent href "80562" "LLM Integration and Agents"
click Session href "80559" "Session Management"
click Queue href "80591" "Event Conversion and Processing"
click Exec href "80589" "Executor and Server"

Usage Examples Extracted from Tests

1. Executing a task:

agent, _ := newEventReplayAgent(events, nil)
sessionService := &testSessionService{Service: session.InMemoryService()}
runnerConfig := runner.Config{AppName: agent.Name(), Agent: agent, SessionService: sessionService}
executor := NewExecutor(ExecutorConfig{RunnerConfig: runnerConfig})
queue := &testQueue{Queue: eventqueue.NewInMemoryQueue(10)}
reqCtx := &a2asrv.RequestContext{TaskID: task.ID, ContextID: task.ContextID, Message: request.Message}

err := executor.Execute(ctx, reqCtx, queue)

2. Cancelling a task:

executor := NewExecutor(ExecutorConfig{})
queue := &testQueue{Queue: eventqueue.NewInMemoryQueue(10)}
reqCtx := &a2asrv.RequestContext{TaskID: task.ID, ContextID: task.ContextID, StoredTask: task}

err := executor.Cancel(ctx, reqCtx, queue)

References to Related Topics


This documentation provides a detailed view of how the executor_test.go file exercises the Executor component, focusing on task execution, session handling, event generation, and error scenarios through comprehensive unit tests.