go.uber.org/cadence@v1.2.9/internal/encoded_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 "bytes" 25 "encoding/gob" 26 "fmt" 27 "reflect" 28 "testing" 29 30 "github.com/stretchr/testify/require" 31 ) 32 33 func testDataConverterFunction(t *testing.T, dc DataConverter, f interface{}, args ...interface{}) string { 34 input, err := dc.ToData(args...) 35 require.NoError(t, err, err) 36 37 var result []interface{} 38 for _, v := range args { 39 arg := reflect.New(reflect.TypeOf(v)).Interface() 40 result = append(result, arg) 41 } 42 err = dc.FromData(input, result...) 43 require.NoError(t, err, err) 44 45 targetArgs := []reflect.Value{} 46 for _, arg := range result { 47 targetArgs = append(targetArgs, reflect.ValueOf(arg).Elem()) 48 } 49 fnValue := reflect.ValueOf(f) 50 retValues := fnValue.Call(targetArgs) 51 return retValues[0].Interface().(string) 52 } 53 54 func TestDefaultDataConverter(t *testing.T) { 55 t.Parallel() 56 dc := getDefaultDataConverter() 57 t.Run("result", func(t *testing.T) { 58 t.Parallel() 59 f1 := func(ctx Context, r []byte) string { 60 return "result" 61 } 62 r1 := testDataConverterFunction(t, dc, f1, new(emptyCtx), []byte("test")) 63 require.Equal(t, r1, "result") 64 }) 65 t.Run("empty", func(t *testing.T) { 66 t.Parallel() 67 // No parameters. 68 f2 := func() string { 69 return "empty-result" 70 } 71 r2 := testDataConverterFunction(t, dc, f2) 72 require.Equal(t, r2, "empty-result") 73 }) 74 t.Run("nil", func(t *testing.T) { 75 t.Parallel() 76 // Nil parameter. 77 f3 := func(r []byte) string { 78 return "nil-result" 79 } 80 r3 := testDataConverterFunction(t, dc, f3, []byte("")) 81 require.Equal(t, r3, "nil-result") 82 }) 83 } 84 85 // testDataConverter implements encoded.DataConverter using gob 86 type testDataConverter struct{} 87 88 func newTestDataConverter() DataConverter { 89 return &testDataConverter{} 90 } 91 92 func (tdc *testDataConverter) ToData(value ...interface{}) ([]byte, error) { 93 var buf bytes.Buffer 94 enc := gob.NewEncoder(&buf) 95 for i, obj := range value { 96 if err := enc.Encode(obj); err != nil { 97 return nil, fmt.Errorf( 98 "unable to encode argument: %d, %v, with gob error: %v", i, reflect.TypeOf(obj), err) 99 } 100 } 101 return buf.Bytes(), nil 102 } 103 104 func (tdc *testDataConverter) FromData(input []byte, valuePtr ...interface{}) error { 105 dec := gob.NewDecoder(bytes.NewBuffer(input)) 106 for i, obj := range valuePtr { 107 if err := dec.Decode(obj); err != nil { 108 return fmt.Errorf( 109 "unable to decode argument: %d, %v, with gob error: %v", i, reflect.TypeOf(obj), err) 110 } 111 } 112 return nil 113 } 114 115 func TestDecodeArg(t *testing.T) { 116 t.Parallel() 117 dc := getDefaultDataConverter() 118 119 b, err := encodeArg(dc, testErrorDetails3) 120 require.NoError(t, err) 121 var r testStruct 122 err = decodeArg(dc, b, &r) 123 require.NoError(t, err) 124 require.Equal(t, testErrorDetails3, r) 125 126 // test mismatch of multi arguments 127 b, err = encodeArgs(dc, []interface{}{testErrorDetails1, testErrorDetails2}) 128 require.NoError(t, err) 129 require.Error(t, decodeArg(dc, b, &r)) 130 }