-
Notifications
You must be signed in to change notification settings - Fork 41
test dbos mocks #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test dbos mocks #115
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| all: false | ||
| dir: './mocks' | ||
| filename: '{{.InterfaceName}}_mock.go' | ||
| force-file-write: true | ||
| formatter: goimports | ||
| include-auto-generated: false | ||
| log-level: info | ||
| structname: 'Mock{{.InterfaceName}}' | ||
| pkgname: 'mocks' | ||
| recursive: false | ||
| require-template-schema-exists: true | ||
| template: testify | ||
| template-schema: '{{.Template}}.schema.json' | ||
| packages: | ||
| github.com/dbos-inc/dbos-transact-golang/dbos: | ||
| interfaces: | ||
| DBOSContext: | ||
| WorkflowHandle: |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| package dbos_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/dbos-inc/dbos-transact-golang/dbos" | ||
| "github.com/dbos-inc/dbos-transact-golang/dbos/mocks" | ||
| "github.com/stretchr/testify/mock" | ||
| ) | ||
|
|
||
| func step(ctx context.Context) (int, error) { | ||
| return 1, nil | ||
| } | ||
|
|
||
| func childWorkflow(ctx dbos.DBOSContext, i int) (int, error) { | ||
| return i + 1, nil | ||
| } | ||
|
|
||
| func workflow(ctx dbos.DBOSContext, i int) (int, error) { | ||
| // Test RunAsStep | ||
| a, err := dbos.RunAsStep(ctx, step) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| // Child wf | ||
| ch, err := dbos.RunWorkflow(ctx, childWorkflow, i) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| b, err := ch.GetResult() | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| // Test messaging operations | ||
| c, err := dbos.Recv[int](ctx, "chan1", 1*time.Second) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| d, err := dbos.GetEvent[int](ctx, "tgw", "event1", 1*time.Second) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| err = dbos.Send(ctx, "dst", 1, "topic") | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| // Test SetEvent | ||
| err = dbos.SetEvent(ctx, "test_key", "test_value") | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| // Test Sleep | ||
| _, err = dbos.Sleep(ctx, 100*time.Millisecond) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| // Test ID retrieval methods | ||
| workflowID, err := ctx.GetWorkflowID() | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| stepID, err := ctx.GetStepID() | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| // Test workflow management | ||
| _, err = dbos.RetrieveWorkflow[int](ctx, workflowID) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| _, err = dbos.Enqueue[int, int](ctx, "test_queue", "test_workflow", 42) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| err = dbos.CancelWorkflow(ctx, workflowID) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| _, err = dbos.ResumeWorkflow[int](ctx, workflowID) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| forkInput := dbos.ForkWorkflowInput{ | ||
| OriginalWorkflowID: workflowID, | ||
| StartStep: uint(stepID), | ||
| } | ||
| _, err = dbos.ForkWorkflow[int](ctx, forkInput) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| _, err = dbos.ListWorkflows(ctx) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| _, err = dbos.GetWorkflowSteps(ctx, workflowID) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| // Test accessor methods | ||
| appVersion := ctx.GetApplicationVersion() | ||
| executorID := ctx.GetExecutorID() | ||
| appID := ctx.GetApplicationID() | ||
|
|
||
| // Use some values to avoid compiler warnings | ||
| _ = appVersion | ||
| _ = executorID | ||
| _ = appID | ||
|
|
||
| return a + b + c + d, nil | ||
| } | ||
|
|
||
| func aRealProgramFunction(dbosCtx dbos.DBOSContext) error { | ||
|
|
||
| dbos.RegisterWorkflow(dbosCtx, workflow) | ||
|
|
||
| err := dbosCtx.Launch() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer dbosCtx.Shutdown(1 * time.Second) | ||
|
|
||
| res, err := workflow(dbosCtx, 2) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if res != 4 { | ||
| return fmt.Errorf("unexpected result: %v", res) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func TestMocks(t *testing.T) { | ||
| mockCtx := mocks.NewMockDBOSContext(t) | ||
|
|
||
| // Context lifecycle | ||
| mockCtx.On("Launch").Return(nil) | ||
| mockCtx.On("Shutdown", mock.Anything).Return() | ||
|
|
||
| // Basic workflow operations (existing) | ||
| mockCtx.On("RunAsStep", mockCtx, mock.Anything, mock.Anything).Return(1, nil) | ||
|
|
||
| // Child workflow | ||
| mockChildHandle := mocks.NewMockWorkflowHandle[any](t) | ||
| mockCtx.On("RunWorkflow", mockCtx, mock.Anything, 2, mock.Anything).Return(mockChildHandle, nil).Once() | ||
| mockChildHandle.On("GetResult").Return(1, nil) | ||
|
|
||
| // Messaging | ||
| mockCtx.On("Recv", mockCtx, "chan1", 1*time.Second).Return(1, nil) | ||
| mockCtx.On("GetEvent", mockCtx, "tgw", "event1", 1*time.Second).Return(1, nil) | ||
| mockCtx.On("Send", mockCtx, "dst", 1, "topic").Return(nil) | ||
| mockCtx.On("SetEvent", mockCtx, "test_key", "test_value").Return(nil) | ||
|
|
||
| mockCtx.On("Sleep", mockCtx, 100*time.Millisecond).Return(100*time.Millisecond, nil) | ||
|
|
||
| // ID retrieval methods | ||
| mockCtx.On("GetWorkflowID").Return("test-workflow-id", nil) | ||
| mockCtx.On("GetStepID").Return(1, nil) | ||
|
|
||
| // Workflow management | ||
| mockGenericHandle := mocks.NewMockWorkflowHandle[any](t) | ||
| mockGenericHandle.On("GetWorkflowID").Return("generic-workflow-id").Maybe() | ||
| mockGenericHandle.On("GetResult").Return(42, nil).Maybe() | ||
| mockGenericHandle.On("GetStatus").Return(dbos.WorkflowStatus{}, nil).Maybe() | ||
|
|
||
| mockCtx.On("RetrieveWorkflow", mockCtx, "test-workflow-id").Return(mockGenericHandle, nil) | ||
| mockCtx.On("Enqueue", mockCtx, "test_queue", "test_workflow", 42).Return(mockGenericHandle, nil) | ||
| mockCtx.On("CancelWorkflow", mockCtx, "test-workflow-id").Return(nil) | ||
| mockCtx.On("ResumeWorkflow", mockCtx, "test-workflow-id").Return(mockGenericHandle, nil) | ||
| mockCtx.On("ForkWorkflow", mockCtx, mock.Anything).Return(mockGenericHandle, nil) | ||
| mockCtx.On("ListWorkflows", mockCtx).Return([]dbos.WorkflowStatus{}, nil) | ||
| mockCtx.On("GetWorkflowSteps", mockCtx, "test-workflow-id").Return([]dbos.StepInfo{}, nil) | ||
|
|
||
| // Accessor methods | ||
| mockCtx.On("GetApplicationVersion").Return("test-version") | ||
| mockCtx.On("GetExecutorID").Return("test-executor") | ||
| mockCtx.On("GetApplicationID").Return("test-app-id") | ||
|
|
||
| err := aRealProgramFunction(mockCtx) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| mockCtx.AssertExpectations(t) | ||
| mockChildHandle.AssertExpectations(t) | ||
| // mockGenericHandle.AssertExpectations(t) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -216,6 +216,35 @@ func (h *workflowPollingHandle[R]) GetResult() (R, error) { | |
| return *new(R), err | ||
| } | ||
|
|
||
| // Wrapper handle -- useful for handling mocks in RunWorkflow | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this do? Why do we need it? Do users need to worry about it?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The situation we have with RunWorkflow, is that only the interface methods are mockable. If you want to mock RunWorkflow and have it return a mock handle, only the "inner" call with get a mock. Then, in the package level method, we use information from that handle to return a new typed handle. This new handle, not being the mock, makes testing impossible. What this does -- users don't need to worry -- is to fall back on a "proxy" handler if the interface RunWorkflow does not return the "normal" handlers (direct or polling). The proxy just calls in whatever was returned by RunWorkflow. This allow a mock handle to be passed through.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we just return a polling handle (passing in the mocked context) instead of this new thing?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning a new polling handle would be returning a new object that is not the mock returned by
|
||
| type workflowHandleProxy[R any] struct { | ||
| wrappedHandle WorkflowHandle[any] | ||
| } | ||
|
|
||
| func (h *workflowHandleProxy[R]) GetResult() (R, error) { | ||
| result, err := h.wrappedHandle.GetResult() | ||
| if err != nil { | ||
| var zero R | ||
| return zero, err | ||
| } | ||
|
|
||
| // Convert from any to R | ||
| if typed, ok := result.(R); ok { | ||
| return typed, nil | ||
| } | ||
|
|
||
| var zero R | ||
| return zero, fmt.Errorf("cannot convert result of type %T to %T", result, zero) | ||
| } | ||
|
|
||
| func (h *workflowHandleProxy[R]) GetStatus() (WorkflowStatus, error) { | ||
| return h.wrappedHandle.GetStatus() | ||
| } | ||
|
|
||
| func (h *workflowHandleProxy[R]) GetWorkflowID() string { | ||
| return h.wrappedHandle.GetWorkflowID() | ||
| } | ||
|
|
||
| /**********************************/ | ||
| /******* WORKFLOW REGISTRY *******/ | ||
| /**********************************/ | ||
|
|
@@ -583,8 +612,8 @@ func RunWorkflow[P any, R any](ctx DBOSContext, fn Workflow[P, R], input P, opts | |
| return typedHandle, nil | ||
| } | ||
|
|
||
| // Should never happen | ||
| return nil, fmt.Errorf("unexpected workflow handle type: %T", handle) | ||
| // Usually on a mocked path | ||
| return &workflowHandleProxy[R]{wrappedHandle: handle}, nil | ||
| } | ||
|
|
||
| func (c *dbosContext) RunWorkflow(_ DBOSContext, fn WorkflowFunc, input any, opts ...WorkflowOption) (WorkflowHandle[any], error) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.