go.temporal.io/server/chasm/context_mock.go
238 LOC · 112 covered · 126 uncovered · 33 ranges · 397 concepts · 30 introducers · 190 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
package chasm
import (
"context"
"errors"
"fmt"
"slices"
"sync"
"time"
commonpb "go.temporal.io/api/common/v1"
sdkpb "go.temporal.io/api/sdk/v1"
persistencespb "go.temporal.io/server/api/persistence/v1"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/common/namespace"
"google.golang.org/grpc/metadata"
)
var _ Context = (*MockContext)(nil)
var _ MutableContext = (*MockMutableContext)(nil)
// MockContext is a mock implementation of [Context].
type MockContext struct {
HandleExecutionKey func() ExecutionKey
HandleNow func(component Component) time.Time
HandleRef func(component Component) ([]byte, error)
HandleExecutionCloseTime func() time.Time
HandleStateTransitionCount func() int64
HandleExecutionInfo func() ExecutionInfo
HandleLibrary func(name string) (Library, bool)
HandleNamespaceEntry func() *namespace.Namespace
HandleEndpointByName func(string) (*persistencespb.NexusEndpointEntry, error)
HandleMetricsHandler func() metrics.Handler
HandleLinks func(component Component) []*commonpb.Link
HandleRequestLinks func(component Component, requestID string) ([]*commonpb.Link, error)
HandleUserMetadata func(component Component) *sdkpb.UserMetadata
// GoCtx is the underlying context.Context used for context value lookups.
// Any values set on it will be available via the CHASM mock context's Value method,
// and take precedence over any registered context values.
// Defaults to context.Background() if nil.
GoCtx context.Context
registeredContextValues map[any]any
}
func (c *MockContext) RegisterComponentContextValues(
keyValues map[any]any,
) {
if c.registeredContextValues == nil {
c.registeredContextValues = make(map[any]any)
}
for k, v := range keyValues {
if _, exists := c.registeredContextValues[k]; exists {
// nolint:forbidigo
panic(fmt.Sprintf("context value key already registered: %v", k))
}
c.registeredContextValues[k] = v
}
}
if c.GoCtx == nil {
}
}
if values := metadata.ValueFromIncomingContext(c.goContext(), key); len(values) > 0 {
}
}
func (c *MockContext) EndpointByName(name string) (*persistencespb.NexusEndpointEntry, error) {
context_mock.go ×1
if c.HandleEndpointByName != nil {
return c.HandleEndpointByName(name)
}
return nil, errors.New("endpoint registry not available")
}
if c.HandleNow != nil {
}
}
if c.HandleRef != nil {
return c.HandleRef(cmp)
}
return nil, nil
}
func (c *MockContext) structuredRef(cmp Component) (ComponentRef, error) {
return ComponentRef{}, nil
}
if c.HandleExecutionKey != nil {
}
}
func (c *MockContext) ExecutionInfo() ExecutionInfo {
if c.HandleExecutionInfo != nil {
return c.HandleExecutionInfo()
}
return ExecutionInfo{}
}
if c.HandleNamespaceEntry != nil {
return c.HandleNamespaceEntry()
}
return nil
}
executionKey := c.ExecutionKey()
return log.NewTestLogger().With(
tag.WorkflowNamespaceID(executionKey.NamespaceID),
tag.WorkflowID(executionKey.BusinessID),
tag.WorkflowRunID(executionKey.RunID),
)
}
if c.HandleMetricsHandler != nil {
}
}
return c.goContext().Value(key)
}
if c.HandleLinks != nil {
}
}
func (c *MockContext) RequestLinks(component Component, requestID string) ([]*commonpb.Link, error) {
activity.go ×3
if c.HandleRequestLinks != nil {
return c.HandleRequestLinks(component, requestID)
}
return nil, nil
}
func (c *MockContext) UserMetadata(component Component) *sdkpb.UserMetadata {
context_mock.go ×1
if c.HandleUserMetadata != nil {
}
}
return &MockContext{
HandleExecutionKey: c.HandleExecutionKey,
HandleNow: c.HandleNow,
HandleRef: c.HandleRef,
HandleExecutionInfo: c.HandleExecutionInfo,
HandleMetricsHandler: c.HandleMetricsHandler,
GoCtx: context.WithValue(c.goContext(), key, value),
HandleNamespaceEntry: c.HandleNamespaceEntry,
HandleEndpointByName: c.HandleEndpointByName,
HandleLinks: c.HandleLinks,
HandleRequestLinks: c.HandleRequestLinks,
HandleUserMetadata: c.HandleUserMetadata,
}
}
// MockMutableContext is a mock implementation of [MutableContext] that records added tasks for inspection in
// tests.
type MockMutableContext struct {
MockContext
mu sync.Mutex
Tasks []MockTask
LinksByRequest map[Component]map[string][]*commonpb.Link
UserMetadataByComponent map[Component]*sdkpb.UserMetadata
}
func (c *MockMutableContext) AddTask(component Component, attributes TaskAttributes, payload any) {
context_mock.go ×1
c.mu.Lock()
defer c.mu.Unlock()
c.Tasks = append(c.Tasks, MockTask{component, attributes, payload})
}
func (c *MockMutableContext) SetRequestLinks(component Component, requestID string, links []*commonpb.Link) error {
context_mock.go ×2
c.mu.Lock()
defer c.mu.Unlock()
if c.LinksByRequest == nil {
c.LinksByRequest = make(map[Component]map[string][]*commonpb.Link)
}
perRequest, ok := c.LinksByRequest[component]
if !ok {
perRequest = make(map[string][]*commonpb.Link)
c.LinksByRequest[component] = perRequest
}
if len(links) == 0 {
delete(perRequest, requestID)
perRequest[requestID] = links
}
return nil
}
func (c *MockMutableContext) SetUserMetadata(component Component, md *sdkpb.UserMetadata) error {
context_mock.go ×1
c.mu.Lock()
defer c.mu.Unlock()
if c.UserMetadataByComponent == nil {
c.UserMetadataByComponent = make(map[Component]*sdkpb.UserMetadata)
}
c.UserMetadataByComponent[component] = md
return nil
}
return &MockMutableContext{
MockContext: *ContextWithValue(&c.MockContext, key, value),
Tasks: slices.Clone(c.Tasks),
}
}
type MockTask struct {
Component Component
Attributes TaskAttributes
Payload any
}