|
| 1 | +package serializer |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/stretchr/testify/assert" |
| 5 | + "github.com/stretchr/testify/require" |
| 6 | + "go.uber.org/cadence/.gen/go/shared" |
| 7 | + "go.uber.org/cadence/internal/common" |
| 8 | + "reflect" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestSerializationRoundup(t *testing.T) { |
| 13 | + for _, encoding := range []shared.EncodingType{shared.EncodingTypeJSON, shared.EncodingTypeThriftRW} { |
| 14 | + t.Run(encoding.String(), func(t *testing.T) { |
| 15 | + |
| 16 | + events := []*shared.HistoryEvent{ |
| 17 | + { |
| 18 | + EventId: common.Int64Ptr(1), |
| 19 | + Timestamp: common.Int64Ptr(1), |
| 20 | + EventType: common.EventTypePtr(shared.EventTypeActivityTaskCompleted), |
| 21 | + Version: common.Int64Ptr(1), |
| 22 | + ActivityTaskCompletedEventAttributes: &shared.ActivityTaskCompletedEventAttributes{ |
| 23 | + Result: []byte("result"), |
| 24 | + }, |
| 25 | + }, |
| 26 | + } |
| 27 | + |
| 28 | + serialized, err := SerializeBatchEvents(events, encoding) |
| 29 | + require.NoError(t, err) |
| 30 | + |
| 31 | + deserialized, err := DeserializeBatchEvents(serialized) |
| 32 | + require.NoError(t, err) |
| 33 | + |
| 34 | + assert.Equal(t, events, deserialized) |
| 35 | + }) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestDeserializeBlobDataToHistoryEvents(t *testing.T) { |
| 40 | + events := []*shared.HistoryEvent{ |
| 41 | + { |
| 42 | + EventId: common.Int64Ptr(1), |
| 43 | + Timestamp: common.Int64Ptr(1), |
| 44 | + EventType: common.EventTypePtr(shared.EventTypeDecisionTaskStarted), |
| 45 | + Version: common.Int64Ptr(1), |
| 46 | + DecisionTaskStartedEventAttributes: &shared.DecisionTaskStartedEventAttributes{ |
| 47 | + ScheduledEventId: common.Int64Ptr(1), |
| 48 | + }, |
| 49 | + }, |
| 50 | + { |
| 51 | + EventId: common.Int64Ptr(1), |
| 52 | + Timestamp: common.Int64Ptr(1), |
| 53 | + EventType: common.EventTypePtr(shared.EventTypeActivityTaskCompleted), |
| 54 | + Version: common.Int64Ptr(1), |
| 55 | + ActivityTaskCompletedEventAttributes: &shared.ActivityTaskCompletedEventAttributes{ |
| 56 | + Result: []byte("result"), |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + serialized, err := SerializeBatchEvents(events, shared.EncodingTypeThriftRW) |
| 62 | + require.NoError(t, err) |
| 63 | + |
| 64 | + deserialized, err := DeserializeBlobDataToHistoryEvents([]*shared.DataBlob{serialized}, shared.HistoryEventFilterTypeCloseEvent) |
| 65 | + require.NoError(t, err) |
| 66 | + |
| 67 | + assert.Equal(t, events[1], deserialized.Events[0]) |
| 68 | +} |
| 69 | + |
| 70 | +func TestDeserializeBlobDataToHistoryEvents_failure(t *testing.T) { |
| 71 | + for _, tc := range []struct { |
| 72 | + name string |
| 73 | + serialized *shared.DataBlob |
| 74 | + expectedErrString string |
| 75 | + }{ |
| 76 | + { |
| 77 | + name: "empty blob", |
| 78 | + serialized: &shared.DataBlob{}, |
| 79 | + expectedErrString: "corrupted history event batch, empty events", |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "corrupted blob", |
| 83 | + serialized: &shared.DataBlob{Data: []byte("corrupted"), EncodingType: shared.EncodingTypeThriftRW.Ptr()}, |
| 84 | + expectedErrString: "BadRequestError{Message: Invalid binary encoding version.}", |
| 85 | + }, |
| 86 | + } { |
| 87 | + t.Run(tc.name, func(t *testing.T) { |
| 88 | + _, err := DeserializeBlobDataToHistoryEvents([]*shared.DataBlob{tc.serialized}, shared.HistoryEventFilterTypeCloseEvent) |
| 89 | + assert.ErrorContains(t, err, tc.expectedErrString) |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestThriftEncodingRoundtrip(t *testing.T) { |
| 95 | + for _, tc := range []struct { |
| 96 | + input interface{} |
| 97 | + }{ |
| 98 | + { |
| 99 | + input: &shared.HistoryEvent{ |
| 100 | + EventId: common.Int64Ptr(1), |
| 101 | + EventType: shared.EventTypeDecisionTaskStarted.Ptr(), |
| 102 | + }, |
| 103 | + }, |
| 104 | + { |
| 105 | + input: &shared.Memo{ |
| 106 | + Fields: map[string][]byte{"key": []byte("value")}, |
| 107 | + }, |
| 108 | + }, |
| 109 | + { |
| 110 | + input: &shared.ResetPoints{ |
| 111 | + Points: []*shared.ResetPointInfo{ |
| 112 | + { |
| 113 | + BinaryChecksum: common.StringPtr("checksum"), |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + { |
| 119 | + input: &shared.BadBinaries{ |
| 120 | + Binaries: map[string]*shared.BadBinaryInfo{ |
| 121 | + "key": { |
| 122 | + Reason: common.StringPtr("reason"), |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + { |
| 128 | + input: &shared.VersionHistories{ |
| 129 | + CurrentVersionHistoryIndex: common.Int32Ptr(1), |
| 130 | + }, |
| 131 | + }, |
| 132 | + { |
| 133 | + input: nil, |
| 134 | + }, |
| 135 | + } { |
| 136 | + name := "nil" |
| 137 | + if tc.input != nil { |
| 138 | + name = reflect.TypeOf(tc.input).String() |
| 139 | + } |
| 140 | + t.Run(name, func(t *testing.T) { |
| 141 | + serialized, err := thriftrwEncode(tc.input) |
| 142 | + require.NoError(t, err) |
| 143 | + |
| 144 | + var deserialized interface{} |
| 145 | + if tc.input != nil { |
| 146 | + deserialized = createEmptyPointer(tc.input) |
| 147 | + err = thriftrwDecode(serialized, deserialized) |
| 148 | + require.NoError(t, err) |
| 149 | + } |
| 150 | + |
| 151 | + assert.Equal(t, tc.input, deserialized) |
| 152 | + }) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func TestSerialization_corner_cases(t *testing.T) { |
| 157 | + t.Run("nil", func(t *testing.T) { |
| 158 | + res, err := serialize(nil, shared.EncodingTypeThriftRW) |
| 159 | + assert.Nil(t, res) |
| 160 | + assert.NoError(t, err) |
| 161 | + }) |
| 162 | + t.Run("unsupported encoding", func(t *testing.T) { |
| 163 | + _, err := SerializeBatchEvents(nil, -1) |
| 164 | + assert.ErrorContains(t, err, "unknown or unsupported encoding type") |
| 165 | + }) |
| 166 | + t.Run("serialization error", func(t *testing.T) { |
| 167 | + res, err := Encode(nil) |
| 168 | + assert.Nil(t, res) |
| 169 | + assert.ErrorIs(t, err, MsgPayloadNotThriftEncoded) |
| 170 | + }) |
| 171 | +} |
| 172 | + |
| 173 | +func createEmptyPointer(input interface{}) interface{} { |
| 174 | + inputType := reflect.TypeOf(input) |
| 175 | + if inputType.Kind() != reflect.Ptr { |
| 176 | + panic("input must be a pointer to a struct") |
| 177 | + } |
| 178 | + elemType := inputType.Elem() |
| 179 | + newInstance := reflect.New(elemType) |
| 180 | + return newInstance.Interface() |
| 181 | +} |
0 commit comments