Skip to content

Commit

Permalink
add timeout test
Browse files Browse the repository at this point in the history
Signed-off-by: MregXN <mregxn@gmail.com>
  • Loading branch information
MregXN committed Dec 4, 2023
1 parent 2fd6f5b commit 4fc546d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
14 changes: 11 additions & 3 deletions tests/apps/workflowsapp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,25 @@ public void ConfigureServices(IServiceCollection services)
var itemToPurchase = input;

itemToPurchase = await context.WaitForExternalEventAsync<string>("ChangePurchaseItem");

// Parallel Execution - Waiting for all tasks to finish
Task<string> t1 = context.WaitForExternalEventAsync<string>("ConfirmSize", TimeSpan.FromSeconds(10));
Task<string> t2 = context.WaitForExternalEventAsync<string>("ConfirmColor", TimeSpan.FromSeconds(10));
Task<string> t3 = context.WaitForExternalEventAsync<string>("ConfirmAddress", TimeSpan.FromSeconds(10));
await Task.WhenAll(t1, t2, t3);


// Parallel Execution - Waiting for any task to finish
Task<string> e1 = context.WaitForExternalEventAsync<string>("PayInCash", TimeSpan.FromSeconds(10));
Task<string> e2 = context.WaitForExternalEventAsync<string>("PayByCard", TimeSpan.FromSeconds(10));
Task<string> e3 = context.WaitForExternalEventAsync<string>("PayOnline", TimeSpan.FromSeconds(10));
await Task.WhenAny(e1, e2, e3);

Task<string> e3 = context.WaitForExternalEventAsync<string>("PayOnline", TimeSpan.FromSeconds(10));
Task timeout = context.CreateTimer(TimeSpan.FromSeconds(1));
Task winner = await Task.WhenAny(e1, e2, e3, timeout);

if (winner == timeout) {
throw new TimeoutException("Payment timed out");
}

// In real life there are other steps related to placing an order, like reserving
// inventory and charging the customer credit card etc. But let's keep it simple ;)
await context.CallActivityAsync<string>("ShipProduct", itemToPurchase);
Expand Down
74 changes: 73 additions & 1 deletion tests/e2e/workflows/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,76 @@ func raiseEventTest(url string, instanceID string) func(t *testing.T) {
}
}

func raiseEventTimeoutTest2(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 parallel events on the workflow
resp, err = utils.HTTPPost(fmt.Sprintf("%s/RaiseWorkflowEvent/dapr/%s/ConfirmSize/1", url, instanceID), nil)
require.NoError(t, err, "failure raising event on workflow")

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

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

time.Sleep(15 * time.Second)

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

func raiseEventTimeoutTest(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 only one parallel events on the workflow
resp, err = utils.HTTPPost(fmt.Sprintf("%s/RaiseWorkflowEvent/dapr/%s/ConfirmSize/1", url, instanceID), nil)
require.NoError(t, err, "failure raising event on workflow")

time.Sleep(15 * time.Second)

require.EventuallyWithT(t, func(t *assert.CollectT) {
resp, err = utils.HTTPGet(getString)
assert.NoError(t, err, "failure getting info on workflow")
assert.Equalf(t, "Failed", string(resp), "expected 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 @@ -232,5 +302,7 @@ func TestWorkflow(t *testing.T) {
t.Run("Start", startTest(externalURL, "start-"+suffix))
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("Raise Event", raiseEventTest(externalURL, "raiseEvent-"+suffix))
t.Run("Raise Event Timeout", raiseEventTimeoutTest(externalURL, "raiseEventTimeout-"+suffix))
t.Run("Raise Event Timeout 2", raiseEventTimeoutTest2(externalURL, "raiseEventTimeout2-"+suffix))
}

0 comments on commit 4fc546d

Please sign in to comment.