go.uber.org/cadence@v1.2.9/internal/workflow_testsuite_test.go (about)

     1  // Copyright (c) 2017-2020 Uber Technologies Inc.
     2  // Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc.
     3  //
     4  // Permission is hereby granted, free of charge, to any person obtaining a copy
     5  // of this software and associated documentation files (the "Software"), to deal
     6  // in the Software without restriction, including without limitation the rights
     7  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8  // copies of the Software, and to permit persons to whom the Software is
     9  // furnished to do so, subject to the following conditions:
    10  //
    11  // The above copyright notice and this permission notice shall be included in
    12  // all copies or substantial portions of the Software.
    13  //
    14  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    20  // THE SOFTWARE.
    21  
    22  package internal
    23  
    24  import (
    25  	"context"
    26  	"strings"
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  func TestSetMemoOnStart(t *testing.T) {
    34  	t.Parallel()
    35  	env := newTestWorkflowEnv(t)
    36  
    37  	memo := map[string]interface{}{
    38  		"key": make(chan int),
    39  	}
    40  	err := env.SetMemoOnStart(memo)
    41  	require.Error(t, err)
    42  
    43  	memo = map[string]interface{}{
    44  		"memoKey": "memo",
    45  	}
    46  	require.Nil(t, env.impl.workflowInfo.Memo)
    47  	err = env.SetMemoOnStart(memo)
    48  	require.NoError(t, err)
    49  	require.NotNil(t, env.impl.workflowInfo.Memo)
    50  }
    51  
    52  func TestSetSearchAttributesOnStart(t *testing.T) {
    53  	t.Parallel()
    54  	env := newTestWorkflowEnv(t)
    55  
    56  	invalidSearchAttr := map[string]interface{}{
    57  		"key": make(chan int),
    58  	}
    59  	err := env.SetSearchAttributesOnStart(invalidSearchAttr)
    60  	require.Error(t, err)
    61  
    62  	searchAttr := map[string]interface{}{
    63  		"CustomIntField": 1,
    64  	}
    65  	err = env.SetSearchAttributesOnStart(searchAttr)
    66  	require.NoError(t, err)
    67  	require.NotNil(t, env.impl.workflowInfo.SearchAttributes)
    68  }
    69  
    70  func TestUnregisteredActivity(t *testing.T) {
    71  	t.Parallel()
    72  	env := newTestWorkflowEnv(t)
    73  	workflow := func(ctx Context) error {
    74  		ctx = WithActivityOptions(ctx, ActivityOptions{
    75  			ScheduleToStartTimeout: time.Minute,
    76  			StartToCloseTimeout:    time.Minute,
    77  		})
    78  		return ExecuteActivity(ctx, "unregistered").Get(ctx, nil)
    79  	}
    80  	env.RegisterWorkflow(workflow)
    81  	env.ExecuteWorkflow(workflow)
    82  	require.Error(t, env.GetWorkflowError())
    83  	ee := env.GetWorkflowError()
    84  	require.NotNil(t, ee)
    85  	require.True(t, strings.HasPrefix(ee.Error(), "unable to find activityType=unregistered"), ee.Error())
    86  }
    87  
    88  func TestNoExplicitRegistrationRequired(t *testing.T) {
    89  	env := newTestWorkflowEnv(t)
    90  	activity := func(ctx context.Context, arg string) (string, error) { return arg + " World!", nil }
    91  	env.RegisterActivity(activity)
    92  	env.ExecuteWorkflow(func(ctx Context, arg1 string) (string, error) {
    93  		ctx = WithActivityOptions(ctx, ActivityOptions{
    94  			ScheduleToCloseTimeout: time.Hour,
    95  			StartToCloseTimeout:    time.Hour,
    96  			ScheduleToStartTimeout: time.Hour,
    97  		})
    98  		var result string
    99  		err := ExecuteActivity(ctx, activity, arg1).Get(ctx, &result)
   100  		if err != nil {
   101  			return "", err
   102  		}
   103  		return result, nil
   104  	}, "Hello")
   105  	require.NoError(t, env.GetWorkflowError())
   106  	var result string
   107  	err := env.GetWorkflowResult(&result)
   108  	require.NoError(t, err)
   109  	require.Equal(t, "Hello World!", result)
   110  }
   111  
   112  func TestWorkflowReturnNil(t *testing.T) {
   113  	env := newTestWorkflowEnv(t)
   114  
   115  	var isExecuted bool
   116  	testWF := func(ctx Context) error {
   117  		isExecuted = true
   118  		return nil
   119  	}
   120  	env.ExecuteWorkflow(testWF)
   121  
   122  	require.True(t, env.IsWorkflowCompleted())
   123  	require.NoError(t, env.GetWorkflowError())
   124  	require.True(t, isExecuted)
   125  
   126  	var r struct{}
   127  	err := env.GetWorkflowResult(&r)
   128  	require.NoError(t, err)
   129  }