testsessionservice.go

Overview

testsessionservice.go implements a fake in-memory session service used primarily for testing purposes within the ADK framework. It provides a mock version of the session management functionality defined in the broader system, allowing tests to simulate session creation, retrieval, listing, deletion, and event appending without requiring a persistent backend. This helps isolate session-related logic in tests and supports deterministic behavior.

The file defines several types such as TestState, TestEvents, TestSession, and FakeSessionService, which collectively emulate real session behavior while storing data in simple Go maps and slices. It implements the session.Service interface, ensuring compatibility with the rest of the application that relies on session services.


Types and Their Responsibilities

TestState

TestEvents

TestSession

SessionKey

FakeSessionService


Methods of FakeSessionService

Each method receives a context.Context and request objects from the session package, returning appropriate responses or errors.

Create

func (s *FakeSessionService) Create(ctx context.Context, req *session.CreateRequest) (*session.CreateResponse, error)

Get

func (s *FakeSessionService) Get(ctx context.Context, req *session.GetRequest) (*session.GetResponse, error)

List

func (s *FakeSessionService) List(ctx context.Context, req *session.ListRequest) (*session.ListResponse, error)

Delete

func (s *FakeSessionService) Delete(ctx context.Context, req *session.DeleteRequest) error

AppendEvent

func (s *FakeSessionService) AppendEvent(ctx context.Context, curSession session.Session, event *session.Event) error

Important Implementation Details


Interaction with Other System Components


Usage Example

// Initialize fake service
fakeService := &FakeSessionService{
    Sessions: make(map[SessionKey]TestSession),
}

// Create a session
createResp, err := fakeService.Create(context.Background(), &session.CreateRequest{
    AppName: "TestApp",
    UserID: "User1",
    SessionID: "",
    State: make(TestState),
})

// Retrieve the session
getResp, err := fakeService.Get(context.Background(), &session.GetRequest{
    AppName: "TestApp",
    UserID: "User1",
    SessionID: "testID",
})

// Append an event
event := &session.Event{
    Timestamp: time.Now(),
    Name: "TestEvent",
    Data: nil,
}
err = fakeService.AppendEvent(context.Background(), getResp.Session, event)

Diagram: Class and Type Structure

classDiagram
class TestState {
+Get()
+Set()
+All()
}
class TestEvents {
+All()
+Len()
+At()
}
class TestSession {
-Id: SessionKey
-SessionState: TestState
-SessionEvents: TestEvents
-UpdatedAt: time.Time
+ID()
+AppName()
+UserID()
+State()
+Events()
+LastUpdateTime()
}
class SessionKey {
-AppName: string
-UserID: string
-SessionID: string
}
class FakeSessionService {
-Sessions: map[SessionKey]TestSession
+Create()
+Get()
+List()
+Delete()
+AppendEvent()
}
FakeSessionService --> "1..*" TestSession : manages
TestSession --> TestState : contains
TestSession --> TestEvents : contains
TestSession --> SessionKey : identified by

References to Related Topics