go.uber.org/cadence@v1.2.9/internal/internal_utils_test.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 internal
    22  
    23  import (
    24  	"fmt"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/stretchr/testify/require"
    29  
    30  	s "go.uber.org/cadence/.gen/go/shared"
    31  )
    32  
    33  func TestChannelBuilderOptions(t *testing.T) {
    34  	t.Parallel()
    35  	builder := &contextBuilder{Timeout: defaultRPCTimeout}
    36  
    37  	opt1 := chanTimeout(time.Minute)
    38  	opt1(builder)
    39  
    40  	require.Equal(t, time.Minute, builder.Timeout)
    41  }
    42  
    43  func TestNewValues(t *testing.T) {
    44  	t.Parallel()
    45  	var details []interface{}
    46  	heartbeatDetail := "status-report-to-workflow"
    47  	heartbeatDetail2 := 1
    48  	heartbeatDetail3 := testStruct{
    49  		Name: heartbeatDetail,
    50  		Age:  heartbeatDetail2,
    51  	}
    52  	details = append(details, heartbeatDetail, heartbeatDetail2, heartbeatDetail3)
    53  	data, err := encodeArgs(nil, details)
    54  	if err != nil {
    55  		panic(err)
    56  	}
    57  	var res string
    58  	var res2 int
    59  	var res3 testStruct
    60  	NewValues(data).Get(&res, &res2, &res3)
    61  	require.Equal(t, heartbeatDetail, res)
    62  	require.Equal(t, heartbeatDetail2, res2)
    63  	require.Equal(t, heartbeatDetail3, res3)
    64  }
    65  
    66  func TestNewValue(t *testing.T) {
    67  	t.Parallel()
    68  	heartbeatDetail := "status-report-to-workflow"
    69  	data, err := encodeArg(nil, heartbeatDetail)
    70  	if err != nil {
    71  		panic(err)
    72  	}
    73  	var res string
    74  	require.NoError(t, NewValue(data).Get(&res))
    75  	require.Equal(t, res, heartbeatDetail)
    76  }
    77  
    78  func TestGetErrorDetails_CustomError(t *testing.T) {
    79  	t.Parallel()
    80  	dc := getDefaultDataConverter()
    81  	details, err := dc.ToData("error details")
    82  	require.NoError(t, err)
    83  
    84  	val := newEncodedValues(details, dc).(*EncodedValues)
    85  	customErr1 := NewCustomError(customErrReasonA, val)
    86  	reason, data := getErrorDetails(customErr1, dc)
    87  	require.Equal(t, customErrReasonA, reason)
    88  	require.Equal(t, val.values, data)
    89  
    90  	customErr2 := NewCustomError(customErrReasonA, testErrorDetails1)
    91  	val2, err := encodeArgs(dc, []interface{}{testErrorDetails1})
    92  	require.NoError(t, err)
    93  	reason, data = getErrorDetails(customErr2, dc)
    94  	require.Equal(t, customErrReasonA, reason)
    95  	require.Equal(t, val2, data)
    96  }
    97  
    98  func TestGetErrorDetails_CancelError(t *testing.T) {
    99  	t.Parallel()
   100  	dc := getDefaultDataConverter()
   101  	details, err := dc.ToData("error details")
   102  	require.NoError(t, err)
   103  
   104  	val := newEncodedValues(details, dc).(*EncodedValues)
   105  	canceledErr1 := NewCanceledError(val)
   106  	reason, data := getErrorDetails(canceledErr1, dc)
   107  	require.Equal(t, errReasonCanceled, reason)
   108  	require.Equal(t, val.values, data)
   109  
   110  	canceledErr2 := NewCanceledError(testErrorDetails1)
   111  	val2, err := encodeArgs(dc, []interface{}{testErrorDetails1})
   112  	require.NoError(t, err)
   113  	reason, data = getErrorDetails(canceledErr2, dc)
   114  	require.Equal(t, errReasonCanceled, reason)
   115  	require.Equal(t, val2, data)
   116  }
   117  
   118  func TestGetErrorDetails_TimeoutError(t *testing.T) {
   119  	t.Parallel()
   120  	dc := getDefaultDataConverter()
   121  	details, err := dc.ToData("error details")
   122  	require.NoError(t, err)
   123  
   124  	val := newEncodedValues(details, dc).(*EncodedValues)
   125  	timeoutErr1 := NewTimeoutError(s.TimeoutTypeScheduleToStart, val)
   126  	reason, data := getErrorDetails(timeoutErr1, dc)
   127  	require.Equal(t, fmt.Sprintf("%v %v", errReasonTimeout, s.TimeoutTypeScheduleToStart), reason)
   128  	require.Equal(t, val.values, data)
   129  
   130  	timeoutErr2 := NewTimeoutError(s.TimeoutTypeHeartbeat, testErrorDetails4)
   131  	val2, err := encodeArgs(dc, []interface{}{testErrorDetails4})
   132  	require.NoError(t, err)
   133  	reason, data = getErrorDetails(timeoutErr2, dc)
   134  	require.Equal(t, fmt.Sprintf("%v %v", errReasonTimeout, s.TimeoutTypeHeartbeat), reason)
   135  	require.Equal(t, val2, data)
   136  }
   137  
   138  func TestConstructError_TimeoutError(t *testing.T) {
   139  	t.Parallel()
   140  	dc := getDefaultDataConverter()
   141  	details, err := dc.ToData(testErrorDetails1)
   142  	require.NoError(t, err)
   143  
   144  	reason := fmt.Sprintf("%v %v", errReasonTimeout, s.TimeoutTypeHeartbeat)
   145  	constructedErr := constructError(reason, details, dc)
   146  	timeoutErr, ok := constructedErr.(*TimeoutError)
   147  	require.True(t, ok)
   148  	require.True(t, timeoutErr.HasDetails())
   149  	var detailValue string
   150  	err = timeoutErr.Details(&detailValue)
   151  	require.NoError(t, err)
   152  	require.Equal(t, testErrorDetails1, detailValue)
   153  
   154  	// Backward compatibility test
   155  	reason = errReasonTimeout
   156  	details, err = dc.ToData(s.TimeoutTypeHeartbeat)
   157  	require.NoError(t, err)
   158  	constructedErr = constructError(reason, details, dc)
   159  	timeoutErr, ok = constructedErr.(*TimeoutError)
   160  	require.True(t, ok)
   161  	require.Equal(t, s.TimeoutTypeHeartbeat, timeoutErr.TimeoutType())
   162  	require.False(t, timeoutErr.HasDetails())
   163  }