Skip to content

Commit

Permalink
e2e about workflow call sub workflow
Browse files Browse the repository at this point in the history
Signed-off-by: MregXN <mregxn@gmail.com>
  • Loading branch information
MregXN committed Nov 27, 2023
1 parent 4d626ca commit 4dbd4c2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/apps/workflowsapp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ public void ConfigureServices(IServiceCollection services)
// inventory and charging the customer credit card etc. But let's keep it simple ;)
await context.CallActivityAsync<string>("ShipProduct", itemToPurchase);

try
{
await context.WaitForExternalEventAsync<string>("CallSubWorkflow", TimeSpan.FromSeconds(5));
ChildWorkflowTaskOptions options = new ChildWorkflowTaskOptions("subworkflow-" + context.InstanceId);
context.CallChildWorkflowAsync<string>("CloseOrder", itemToPurchase, options);
}
catch (TaskCanceledException e)
{
// sub workflow is not triggered, pass.
}

return itemToPurchase;
});

Expand All @@ -71,6 +82,11 @@ public void ConfigureServices(IServiceCollection services)
return Task.FromResult($"We are shipping {input} to the customer using our hoard of drones!");
});

options.RegisterWorkflow<string, string>("CloseOrder",implementation: async (context, input) => {
await context.CreateTimer(TimeSpan.FromSeconds(5));
return $"{input} order closed. Starting a new place order workflow...";
});

});
services.AddAuthentication().AddDapr();
services.AddAuthorization(o => o.AddDapr());
Expand Down
41 changes: 41 additions & 0 deletions tests/e2e/workflows/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func raiseEventTest(url string, instanceID string) func(t *testing.T) {
resp, err = utils.HTTPPost(fmt.Sprintf("%s/RaiseWorkflowEvent/dapr/%s/ChangePurchaseItem/1", url, instanceID), nil)
require.NoError(t, err, "failure raising event on workflow")

time.Sleep(10 * time.Second)
require.EventuallyWithT(t, func(t *assert.CollectT) {
resp, err = utils.HTTPGet(getString)
assert.NoError(t, err, "failure getting info on workflow")
Expand All @@ -155,6 +156,45 @@ func raiseEventTest(url string, instanceID string) func(t *testing.T) {
}
}

func callSubworkflowTest(url string, instanceID string) func(t *testing.T) {
return func(t *testing.T) {
getString := fmt.Sprintf("%s/dapr/%s", url, instanceID)

// Start the workflow and check that it is running
resp, err := utils.HTTPPost(fmt.Sprintf("%s/StartWorkflow/dapr/placeOrder/%s", url, instanceID), nil)
require.NoError(t, err, "failure starting workflow")

require.EventuallyWithT(t, func(t *assert.CollectT) {
resp, err = utils.HTTPGet(getString)
assert.NoError(t, err, "failure getting info on workflow")
assert.Equalf(t, "Running", string(resp), "expected workflow to be Running, actual workflow state is: %s", string(resp))
}, 5*time.Second, 100*time.Millisecond)

// Raise an event on the workflow
resp, err = utils.HTTPPost(fmt.Sprintf("%s/RaiseWorkflowEvent/dapr/%s/ChangePurchaseItem/1", url, instanceID), nil)
require.NoError(t, err, "failure raising event on workflow")

// Raise an event to trigger sub workflow
resp, err = utils.HTTPPost(fmt.Sprintf("%s/RaiseWorkflowEvent/dapr/%s/CallSubWorkflow/1", url, instanceID), nil)
require.NoError(t, err, "failure raising event on workflow")

getSubWorkflowString := fmt.Sprintf("%s/dapr/%s", url, "subworkflow-"+instanceID)

require.EventuallyWithT(t, func(t *assert.CollectT) {
resp, err := utils.HTTPGet(getSubWorkflowString)
assert.NoError(t, err, "failure getting info on workflow")
assert.Equalf(t, "Running", string(resp), "expected sub workflow to be Running, actual workflow state is: %s", string(resp))
}, 5*time.Second, 100*time.Millisecond)

time.Sleep(5 * time.Second)
require.EventuallyWithT(t, func(t *assert.CollectT) {
resp, err := utils.HTTPGet(getSubWorkflowString)
assert.NoError(t, err, "failure getting info on workflow")
assert.Equalf(t, "Completed", string(resp), "expected sub workflow to be Completed, actual workflow state is: %s", string(resp))
}, 5*time.Second, 100*time.Millisecond)
}
}

// Functions for each test case
func purgeTest(url string, instanceID string) func(t *testing.T) {
return func(t *testing.T) {
Expand Down Expand Up @@ -217,4 +257,5 @@ func TestWorkflow(t *testing.T) {
t.Run("Pause and Resume", pauseResumeTest(externalURL, "pause-"+suffix))
t.Run("Purge", purgeTest(externalURL, "purge-"+suffix))
t.Run("Raise event", raiseEventTest(externalURL, "raiseEvent-"+suffix))
t.Run("Call subWorkflow", callSubworkflowTest(externalURL, "callSubWorkflow-"+suffix))
}

0 comments on commit 4dbd4c2

Please sign in to comment.