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.

1 package chasm
2
3 import (
4 "context"
5 "errors"
6 "fmt"
7 "slices"
8 "sync"
9 "time"
10
11 commonpb "go.temporal.io/api/common/v1"
12 sdkpb "go.temporal.io/api/sdk/v1"
13 persistencespb "go.temporal.io/server/api/persistence/v1"
14 "go.temporal.io/server/common/log"
15 "go.temporal.io/server/common/log/tag"
16 "go.temporal.io/server/common/metrics"
17 "go.temporal.io/server/common/namespace"
18 "google.golang.org/grpc/metadata"
19 )
20
21 var _ Context = (*MockContext)(nil)
22 var _ MutableContext = (*MockMutableContext)(nil)
23
24 // MockContext is a mock implementation of [Context].
25 type MockContext struct {
26 HandleExecutionKey func() ExecutionKey
27 HandleNow func(component Component) time.Time
28 HandleRef func(component Component) ([]byte, error)
29 HandleExecutionCloseTime func() time.Time
30 HandleStateTransitionCount func() int64
31 HandleExecutionInfo func() ExecutionInfo
32 HandleLibrary func(name string) (Library, bool)
33 HandleNamespaceEntry func() *namespace.Namespace
34 HandleEndpointByName func(string) (*persistencespb.NexusEndpointEntry, error)
35 HandleMetricsHandler func() metrics.Handler
36 HandleLinks func(component Component) []*commonpb.Link
37 HandleRequestLinks func(component Component, requestID string) ([]*commonpb.Link, error)
38 HandleUserMetadata func(component Component) *sdkpb.UserMetadata
39
40 // GoCtx is the underlying context.Context used for context value lookups.
41 // Any values set on it will be available via the CHASM mock context's Value method,
42 // and take precedence over any registered context values.
43 // Defaults to context.Background() if nil.
44 GoCtx context.Context
45
46 registeredContextValues map[any]any
47 }
48
49 func (c *MockContext) RegisterComponentContextValues(
50 keyValues map[any]any,
51 ) {
52 if c.registeredContextValues == nil {
53 c.registeredContextValues = make(map[any]any)
54 }
55 for k, v := range keyValues {
56 if _, exists := c.registeredContextValues[k]; exists {
57 // nolint:forbidigo
58 panic(fmt.Sprintf("context value key already registered: %v", k))
59 }
60 c.registeredContextValues[k] = v
61 }
62 }
63
64 > func (c *MockContext) goContext() context.Context { context_mock.go ×2
65 > if c.GoCtx == nil {
66 > c.GoCtx = context.Background() context_mock.go ×1
67 > }
68 > return c.GoCtx context_mock.go ×2
69 }
70
71 > func (c *MockContext) RequestHeader(key string) string { statemachine.go ×3
72 > if values := metadata.ValueFromIncomingContext(c.goContext(), key); len(values) > 0 {
73 > return values[0] context_mock.go ×1
74 > }
75 > return "" context_mock.go ×1
76 }
77
78 > func (c *MockContext) EndpointByName(name string) (*persistencespb.NexusEndpointEntry, error) { context_mock.go ×1
79 > if c.HandleEndpointByName != nil {
80 > return c.HandleEndpointByName(name)
81 > }
82 return nil, errors.New("endpoint registry not available")
83 }
84
85 > func (c *MockContext) Now(cmp Component) time.Time { context_mock.go ×1
86 > if c.HandleNow != nil {
87 > return c.HandleNow(cmp) context_mock.go ×1
88 > }
89 > return time.Now() context_mock.go ×1
90 }
91
92 > func (c *MockContext) Ref(cmp Component) ([]byte, error) { operation.go ×4
93 > if c.HandleRef != nil {
94 > return c.HandleRef(cmp)
95 > }
96 return nil, nil
97 }
98
99 func (c *MockContext) structuredRef(cmp Component) (ComponentRef, error) {
100 return ComponentRef{}, nil
101 }
102
103 > func (c *MockContext) ExecutionKey() ExecutionKey { context_mock.go ×1
104 > if c.HandleExecutionKey != nil {
105 > return c.HandleExecutionKey() context_mock.go ×1
106 > }
107 > return ExecutionKey{} context_mock.go ×1
108 }
109
110 func (c *MockContext) ExecutionInfo() ExecutionInfo {
111 if c.HandleExecutionInfo != nil {
112 return c.HandleExecutionInfo()
113 }
114 return ExecutionInfo{}
115 }
116
117 > func (c *MockContext) NamespaceEntry() *namespace.Namespace { context_mock.go ×1
118 > if c.HandleNamespaceEntry != nil {
119 > return c.HandleNamespaceEntry()
120 > }
121 return nil
122 }
123
124 > func (c *MockContext) Logger() log.Logger { context_mock.go ×1
125 > executionKey := c.ExecutionKey()
126 > return log.NewTestLogger().With(
127 > tag.WorkflowNamespaceID(executionKey.NamespaceID),
128 > tag.WorkflowID(executionKey.BusinessID),
129 > tag.WorkflowRunID(executionKey.RunID),
130 > )
131 > }
132
133 > func (c *MockContext) MetricsHandler() metrics.Handler { context_mock.go ×1
134 > if c.HandleMetricsHandler != nil {
135 > return c.HandleMetricsHandler() context_mock.go ×1
136 > }
137 > return metrics.NoopMetricsHandler context_mock.go ×1
138 }
139
140 > func (c *MockContext) Value(key any) any { context_mock.go ×1
141 > return c.goContext().Value(key)
142 > }
143
144 > func (c *MockContext) Links(component Component) []*commonpb.Link { context_mock.go ×1
145 > if c.HandleLinks != nil {
146 > return c.HandleLinks(component) activity.go ×3
147 > }
148 > return nil activity_state.pb.go ×4
149 }
150
151 > func (c *MockContext) RequestLinks(component Component, requestID string) ([]*commonpb.Link, error) { activity.go ×3
152 > if c.HandleRequestLinks != nil {
153 > return c.HandleRequestLinks(component, requestID)
154 > }
155 return nil, nil
156 }
157
158 > func (c *MockContext) UserMetadata(component Component) *sdkpb.UserMetadata { context_mock.go ×1
159 > if c.HandleUserMetadata != nil {
160 > return c.HandleUserMetadata(component) context_mock.go ×1
161 > }
162 > return nil context_mock.go ×1
163 }
164
165 > func (c *MockContext) withValue(key any, value any) Context { context_mock.go ×2
166 > return &MockContext{
167 > HandleExecutionKey: c.HandleExecutionKey,
168 > HandleNow: c.HandleNow,
169 > HandleRef: c.HandleRef,
170 > HandleExecutionInfo: c.HandleExecutionInfo,
171 > HandleMetricsHandler: c.HandleMetricsHandler,
172 > GoCtx: context.WithValue(c.goContext(), key, value),
173 > HandleNamespaceEntry: c.HandleNamespaceEntry,
174 > HandleEndpointByName: c.HandleEndpointByName,
175 > HandleLinks: c.HandleLinks,
176 > HandleRequestLinks: c.HandleRequestLinks,
177 > HandleUserMetadata: c.HandleUserMetadata,
178 > }
179 > }
180
181 // MockMutableContext is a mock implementation of [MutableContext] that records added tasks for inspection in
182 // tests.
183 type MockMutableContext struct {
184 MockContext
185
186 mu sync.Mutex
187 Tasks []MockTask
188 LinksByRequest map[Component]map[string][]*commonpb.Link
189 UserMetadataByComponent map[Component]*sdkpb.UserMetadata
190 }
191
192 > func (c *MockMutableContext) AddTask(component Component, attributes TaskAttributes, payload any) { context_mock.go ×1
193 > c.mu.Lock()
194 > defer c.mu.Unlock()
195 > c.Tasks = append(c.Tasks, MockTask{component, attributes, payload})
196 > }
197
198 > func (c *MockMutableContext) SetRequestLinks(component Component, requestID string, links []*commonpb.Link) error { context_mock.go ×2
199 > c.mu.Lock()
200 > defer c.mu.Unlock()
201 > if c.LinksByRequest == nil {
202 > c.LinksByRequest = make(map[Component]map[string][]*commonpb.Link)
203 > }
204 > perRequest, ok := c.LinksByRequest[component]
205 > if !ok {
206 > perRequest = make(map[string][]*commonpb.Link)
207 > c.LinksByRequest[component] = perRequest
208 > }
209 > if len(links) == 0 {
210 delete(perRequest, requestID)
211 > } else { context_mock.go ×2
212 > perRequest[requestID] = links
213 > }
214 > return nil
215 }
216
217 > func (c *MockMutableContext) SetUserMetadata(component Component, md *sdkpb.UserMetadata) error { context_mock.go ×1
218 > c.mu.Lock()
219 > defer c.mu.Unlock()
220 > if c.UserMetadataByComponent == nil {
221 > c.UserMetadataByComponent = make(map[Component]*sdkpb.UserMetadata)
222 > }
223 > c.UserMetadataByComponent[component] = md
224 > return nil
225 }
226
227 > func (c *MockMutableContext) withValue(key any, value any) Context { context_mock.go ×2
228 > return &MockMutableContext{
229 > MockContext: *ContextWithValue(&c.MockContext, key, value),
230 > Tasks: slices.Clone(c.Tasks),
231 > }
232 > }
233
234 type MockTask struct {
235 Component Component
236 Attributes TaskAttributes
237 Payload any
238 }