go.temporal.io/server@v1.23.0/common/persistence/serializer_test.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package persistence 26 27 import ( 28 "sync" 29 "testing" 30 "time" 31 32 "github.com/stretchr/testify/require" 33 "github.com/stretchr/testify/suite" 34 enumspb "go.temporal.io/api/enums/v1" 35 historypb "go.temporal.io/api/history/v1" 36 "google.golang.org/protobuf/types/known/timestamppb" 37 38 "go.temporal.io/server/common" 39 "go.temporal.io/server/common/log" 40 "go.temporal.io/server/common/payloads" 41 "go.temporal.io/server/common/persistence/serialization" 42 "go.temporal.io/server/common/testing/protorequire" 43 ) 44 45 type ( 46 temporalSerializerSuite struct { 47 suite.Suite 48 // override suite.Suite.Assertions with require.Assertions; this means that s.NotNil(nil) will stop the test, 49 // not merely log an error 50 *require.Assertions 51 protorequire.ProtoAssertions 52 logger log.Logger 53 } 54 ) 55 56 func TestTemporalSerializerSuite(t *testing.T) { 57 s := new(temporalSerializerSuite) 58 suite.Run(t, s) 59 } 60 61 func (s *temporalSerializerSuite) SetupTest() { 62 s.logger = log.NewTestLogger() 63 // Have to define our overridden assertions in the test setup. If we did it earlier, s.T() will return nil 64 s.Assertions = require.New(s.T()) 65 s.ProtoAssertions = protorequire.New(s.T()) 66 } 67 68 func (s *temporalSerializerSuite) TestSerializer() { 69 70 concurrency := 1 71 startWG := sync.WaitGroup{} 72 doneWG := sync.WaitGroup{} 73 74 startWG.Add(1) 75 doneWG.Add(concurrency) 76 77 serializer := serialization.NewSerializer() 78 79 eventType := enumspb.EVENT_TYPE_ACTIVITY_TASK_COMPLETED 80 event0 := &historypb.HistoryEvent{ 81 EventId: 999, 82 EventTime: timestamppb.New(time.Date(2020, 8, 22, 0, 0, 0, 0, time.UTC)), 83 EventType: eventType, 84 Attributes: &historypb.HistoryEvent_ActivityTaskCompletedEventAttributes{ 85 ActivityTaskCompletedEventAttributes: &historypb.ActivityTaskCompletedEventAttributes{ 86 Result: payloads.EncodeString("result-1-event-1"), 87 ScheduledEventId: 4, 88 StartedEventId: 5, 89 Identity: "event-1", 90 }, 91 }, 92 } 93 94 history0 := &historypb.History{Events: []*historypb.HistoryEvent{event0, event0}} 95 96 for i := 0; i < concurrency; i++ { 97 98 go func() { 99 100 startWG.Wait() 101 defer doneWG.Done() 102 103 // serialize event 104 105 nilEvent, err := serializer.SerializeEvent(nil, enumspb.ENCODING_TYPE_PROTO3) 106 s.Nil(err) 107 s.Nil(nilEvent) 108 109 _, err = serializer.SerializeEvent(event0, enumspb.ENCODING_TYPE_UNSPECIFIED) 110 s.NotNil(err) 111 _, ok := err.(*serialization.UnknownEncodingTypeError) 112 s.True(ok) 113 114 dProto, err := serializer.SerializeEvent(event0, enumspb.ENCODING_TYPE_PROTO3) 115 s.Nil(err) 116 s.NotNil(dProto) 117 118 // serialize batch events 119 120 nilEvents, err := serializer.SerializeEvents(nil, enumspb.ENCODING_TYPE_PROTO3) 121 s.Nil(err) 122 s.NotNil(nilEvents) 123 124 _, err = serializer.SerializeEvents(history0.Events, enumspb.ENCODING_TYPE_UNSPECIFIED) 125 s.NotNil(err) 126 _, ok = err.(*serialization.UnknownEncodingTypeError) 127 s.True(ok) 128 129 dsProto, err := serializer.SerializeEvents(history0.Events, enumspb.ENCODING_TYPE_PROTO3) 130 s.Nil(err) 131 s.NotNil(dsProto) 132 133 // deserialize event 134 135 dNilEvent, err := serializer.DeserializeEvent(nilEvent) 136 s.Nil(err) 137 s.Nil(dNilEvent) 138 139 event2, err := serializer.DeserializeEvent(dProto) 140 s.Nil(err) 141 s.ProtoEqual(event0, event2) 142 143 // deserialize events 144 145 dNilEvents, err := serializer.DeserializeEvents(nilEvents) 146 s.Nil(err) 147 s.Nil(dNilEvents) 148 149 events, err := serializer.DeserializeEvents(dsProto) 150 history2 := &historypb.History{Events: events} 151 s.Nil(err) 152 s.ProtoEqual(history0, history2) 153 }() 154 } 155 156 startWG.Done() 157 succ := common.AwaitWaitGroup(&doneWG, 10*time.Second) 158 s.True(succ, "test timed out") 159 }