Files
tasks/chasm/node_pure_task_mock.go
Roey BermanandGitHub 5d6d6dd385 Add chasm.Context.ExecutionKey() method (#8533)
## What changed?

- Add chasm.Context.ExecutionKey() method
- Refactor `context.go` to use better struct names and unexport concepts
that should not be exported
- Add some docstrings
2025-10-24 12:29:38 -07:00

87 lines
2.2 KiB
Go

package chasm
import (
"context"
"sync"
)
// MockNodePureTask is a lightweight manual mock for the NodePureTask interface.
// Methods may be stubbed by assigning the corresponding Handle fields. Call history
// is recorded in the struct fields (thread-safe).
type MockNodePureTask struct {
HandleExecutePureTask func(baseCtx context.Context, taskAttributes TaskAttributes, taskInstance any) (bool, error)
HandleValidatePureTask func(baseCtx context.Context, taskAttributes TaskAttributes, taskInstance any) (bool, error)
mu sync.Mutex
ExecuteCalls []struct {
BaseCtx context.Context
Attributes TaskAttributes
Task any
}
ValidateCalls []struct {
BaseCtx context.Context
Attributes TaskAttributes
Task any
}
}
func (m *MockNodePureTask) ExecutePureTask(
baseCtx context.Context,
taskAttributes TaskAttributes,
taskInstance any,
) (bool, error) {
if m.HandleExecutePureTask != nil {
ok, err := m.HandleExecutePureTask(baseCtx, taskAttributes, taskInstance)
m.mu.Lock()
m.ExecuteCalls = append(m.ExecuteCalls, struct {
BaseCtx context.Context
Attributes TaskAttributes
Task any
}{BaseCtx: baseCtx, Attributes: taskAttributes, Task: taskInstance})
m.mu.Unlock()
return ok, err
}
m.mu.Lock()
m.ExecuteCalls = append(m.ExecuteCalls, struct {
BaseCtx context.Context
Attributes TaskAttributes
Task any
}{BaseCtx: baseCtx, Attributes: taskAttributes, Task: taskInstance})
m.mu.Unlock()
return false, nil
}
func (m *MockNodePureTask) ValidatePureTask(
baseCtx context.Context,
taskAttributes TaskAttributes,
taskInstance any,
) (bool, error) {
if m.HandleValidatePureTask != nil {
ok, err := m.HandleValidatePureTask(baseCtx, taskAttributes, taskInstance)
m.mu.Lock()
m.ValidateCalls = append(m.ValidateCalls, struct {
BaseCtx context.Context
Attributes TaskAttributes
Task any
}{BaseCtx: baseCtx, Attributes: taskAttributes, Task: taskInstance})
m.mu.Unlock()
return ok, err
}
m.mu.Lock()
m.ValidateCalls = append(m.ValidateCalls, struct {
BaseCtx context.Context
Attributes TaskAttributes
Task any
}{BaseCtx: baseCtx, Attributes: taskAttributes, Task: taskInstance})
m.mu.Unlock()
return false, nil
}