go.uber.org/cadence@v1.2.9/test/replaytests/branch_workflow.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package replaytests
    22  
    23  import (
    24  	"fmt"
    25  	"time"
    26  
    27  	"go.uber.org/cadence/workflow"
    28  )
    29  
    30  /**
    31   * This sample workflow executes multiple branches in parallel. The number of branches is controlled by passed in parameter.
    32   */
    33  
    34  // sampleBranchWorkflow workflow decider
    35  func sampleBranchWorkflow(ctx workflow.Context) error {
    36  	var futures []workflow.Future
    37  	// starts activities in parallel
    38  	ao := workflow.ActivityOptions{
    39  		ScheduleToStartTimeout: time.Minute,
    40  		StartToCloseTimeout:    time.Minute,
    41  		HeartbeatTimeout:       time.Second * 20,
    42  	}
    43  	ctx = workflow.WithActivityOptions(ctx, ao)
    44  
    45  	for i := 1; i <= 3; i++ {
    46  		activityInput := fmt.Sprintf("branch %d of 3", i)
    47  		future := workflow.ExecuteActivity(ctx, sampleActivity, activityInput)
    48  		futures = append(futures, future)
    49  	}
    50  
    51  	// wait until all futures are done
    52  	for _, future := range futures {
    53  		if err := future.Get(ctx, nil); err != nil {
    54  			return err
    55  		}
    56  	}
    57  
    58  	workflow.GetLogger(ctx).Info("Workflow completed.")
    59  
    60  	return nil
    61  }
    62  
    63  // SampleBranchWorkflow2 run a workflow with different number of branch.
    64  // If the number of expected branches is changed the replayer should catch it as a non deterministic error.
    65  func sampleBranchWorkflow2(ctx workflow.Context) error {
    66  	var futures []workflow.Future
    67  	// starts activities in parallel
    68  	ao := workflow.ActivityOptions{
    69  		ScheduleToStartTimeout: time.Minute,
    70  		StartToCloseTimeout:    time.Minute,
    71  		HeartbeatTimeout:       time.Second * 20,
    72  	}
    73  	ctx = workflow.WithActivityOptions(ctx, ao)
    74  
    75  	for i := 1; i <= 2; i++ {
    76  		activityInput := fmt.Sprintf("branch %d of 4", i)
    77  		future := workflow.ExecuteActivity(ctx, sampleActivity, activityInput)
    78  		futures = append(futures, future)
    79  	}
    80  
    81  	// wait until all futures are done
    82  	for _, future := range futures {
    83  		if err := future.Get(ctx, nil); err != nil {
    84  			return err
    85  		}
    86  	}
    87  
    88  	workflow.GetLogger(ctx).Info("Workflow completed.")
    89  
    90  	return nil
    91  }