go.temporal.io/server@v1.23.0/common/archiver/gcloud/util_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 gcloud 26 27 import ( 28 "testing" 29 "time" 30 31 "github.com/stretchr/testify/require" 32 "github.com/stretchr/testify/suite" 33 historypb "go.temporal.io/api/history/v1" 34 "google.golang.org/protobuf/types/known/timestamppb" 35 36 "go.temporal.io/server/common" 37 "go.temporal.io/server/common/codec" 38 ) 39 40 func (s *utilSuite) SetupTest() { 41 s.Assertions = require.New(s.T()) 42 } 43 func TestUtilSuite(t *testing.T) { 44 suite.Run(t, new(utilSuite)) 45 } 46 47 type utilSuite struct { 48 *require.Assertions 49 suite.Suite 50 } 51 52 func (s *utilSuite) TestEncodeDecodeHistoryBatches() { 53 now := time.Date(2020, 8, 22, 1, 2, 3, 4, time.UTC) 54 historyBatches := []*historypb.History{ 55 { 56 Events: []*historypb.HistoryEvent{ 57 { 58 EventId: common.FirstEventID, 59 Version: 1, 60 }, 61 }, 62 }, 63 { 64 Events: []*historypb.HistoryEvent{ 65 { 66 EventId: common.FirstEventID + 1, 67 EventTime: timestamppb.New(now), 68 Version: 1, 69 }, 70 { 71 EventId: common.FirstEventID + 2, 72 Version: 2, 73 Attributes: &historypb.HistoryEvent_WorkflowTaskStartedEventAttributes{WorkflowTaskStartedEventAttributes: &historypb.WorkflowTaskStartedEventAttributes{ 74 Identity: "some random identity", 75 }}, 76 }, 77 }, 78 }, 79 } 80 81 encoder := codec.NewJSONPBEncoder() 82 encodedHistoryBatches, err := encoder.EncodeHistories(historyBatches) 83 s.NoError(err) 84 85 decodedHistoryBatches, err := encoder.DecodeHistories(encodedHistoryBatches) 86 s.NoError(err) 87 s.Equal(historyBatches, decodedHistoryBatches) 88 } 89 90 func (s *utilSuite) TestconstructHistoryFilename() { 91 testCases := []struct { 92 namespaceID string 93 workflowID string 94 runID string 95 closeFailoverVersion int64 96 expectBuiltName string 97 }{ 98 { 99 namespaceID: "testNamespaceID", 100 workflowID: "testWorkflowID", 101 runID: "testRunID", 102 closeFailoverVersion: 5, 103 expectBuiltName: "11936904199538907273367046253745284795510285995943906173973_5_0.history", 104 }, 105 } 106 107 for _, tc := range testCases { 108 filename := constructHistoryFilenameMultipart(tc.namespaceID, tc.workflowID, tc.runID, tc.closeFailoverVersion, 0) 109 s.Equal(tc.expectBuiltName, filename) 110 } 111 } 112 113 func (s *utilSuite) TestSerializeDeserializeGetHistoryToken() { 114 token := &getHistoryToken{ 115 CloseFailoverVersion: 101, 116 BatchIdxOffset: 20, 117 } 118 119 serializedToken, err := serializeToken(token) 120 s.Nil(err) 121 122 deserializedToken, err := deserializeGetHistoryToken(serializedToken) 123 s.Nil(err) 124 s.Equal(token, deserializedToken) 125 } 126 127 func (s *utilSuite) TestConstructHistoryFilenamePrefix() { 128 s.Equal("67753999582745295208344541402884576509131521284625246243", constructHistoryFilenamePrefix("namespaceID", "workflowID", "runID")) 129 } 130 131 func (s *utilSuite) TestConstructHistoryFilenameMultipart() { 132 s.Equal("67753999582745295208344541402884576509131521284625246243_-24_0.history", constructHistoryFilenameMultipart("namespaceID", "workflowID", "runID", -24, 0)) 133 } 134 135 func (s *utilSuite) TestConstructVisibilityFilenamePrefix() { 136 s.Equal("namespaceID/startTimeout", constructVisibilityFilenamePrefix("namespaceID", indexKeyStartTimeout)) 137 } 138 139 func (s *utilSuite) TestConstructTimeBasedSearchKey() { 140 t, _ := time.Parse(time.RFC3339, "2019-10-04T11:00:00+00:00") 141 s.Equal("namespaceID/startTimeout_2019-10-04T", constructTimeBasedSearchKey("namespaceID", indexKeyStartTimeout, t, "Day")) 142 } 143 144 func (s *utilSuite) TestConstructVisibilityFilename() { 145 s.Equal("namespaceID/startTimeout_1970-01-01T00:24:32Z_4346151385925082125_8344541402884576509_131521284625246243.visibility", constructVisibilityFilename("namespaceID", "workflowTypeName", "workflowID", "runID", indexKeyStartTimeout, time.Date(1970, 01, 01, 0, 24, 32, 0, time.UTC))) 146 } 147 148 func (s *utilSuite) TestWorkflowIdPrecondition() { 149 testCases := []struct { 150 workflowID string 151 fileName string 152 expectedResult bool 153 }{ 154 { 155 workflowID: "4418294404690464320", 156 fileName: "closeTimeout_2020-02-27T09:42:28Z_12851121011173788097_4418294404690464320_15619178330501475177.visibility", 157 expectedResult: true, 158 }, 159 { 160 workflowID: "testWorkflowID", 161 fileName: "closeTimeout_2020-02-27T09:42:28Z_12851121011173788097_4418294404690464320_15619178330501475177.visibility", 162 expectedResult: false, 163 }, 164 { 165 workflowID: "", 166 fileName: "closeTimeout_2020-02-27T09:42:28Z_12851121011173788097_4418294404690464320_15619178330501475177.visibility", 167 expectedResult: true, 168 }, 169 } 170 171 for _, testCase := range testCases { 172 s.Equal(newWorkflowIDPrecondition(testCase.workflowID)(testCase.fileName), testCase.expectedResult) 173 } 174 175 } 176 177 func (s *utilSuite) TestRunIdPrecondition() { 178 testCases := []struct { 179 workflowID string 180 runID string 181 fileName string 182 expectedResult bool 183 }{ 184 { 185 workflowID: "4418294404690464320", 186 runID: "15619178330501475177", 187 fileName: "closeTimeout_2020-02-27T09:42:28Z_12851121011173788097_4418294404690464320_15619178330501475177.visibility", 188 expectedResult: true, 189 }, 190 { 191 workflowID: "4418294404690464320", 192 runID: "15619178330501475177", 193 fileName: "closeTimeout_2020-02-27T09:42:28Z_12851121011173788097_4418294404690464320_unkonwnRunID.visibility", 194 expectedResult: false, 195 }, 196 { 197 workflowID: "4418294404690464320", 198 runID: "", 199 fileName: "closeTimeout_2020-02-27T09:42:28Z_12851121011173788097_4418294404690464320_unkonwnRunID.visibility", 200 expectedResult: true, 201 }, 202 } 203 204 for _, testCase := range testCases { 205 s.Equal(newRunIDPrecondition(testCase.runID)(testCase.fileName), testCase.expectedResult) 206 } 207 208 } 209 210 func (s *utilSuite) TestWorkflowTypeNamePrecondition() { 211 testCases := []struct { 212 workflowID string 213 runID string 214 workflowTypeName string 215 fileName string 216 expectedResult bool 217 }{ 218 { 219 workflowID: "4418294404690464320", 220 runID: "15619178330501475177", 221 workflowTypeName: "12851121011173788097", 222 fileName: "closeTimeout_2020-02-27T09:42:28Z_12851121011173788097_4418294404690464320_15619178330501475177.visibility", 223 expectedResult: true, 224 }, 225 { 226 workflowID: "4418294404690464320", 227 runID: "15619178330501475177", 228 workflowTypeName: "12851121011173788097", 229 fileName: "closeTimeout_2020-02-27T09:42:28Z_12851121011173788098_4418294404690464320_15619178330501475177.visibility", 230 expectedResult: false, 231 }, 232 { 233 workflowID: "4418294404690464320", 234 runID: "15619178330501475177", 235 workflowTypeName: "", 236 fileName: "closeTimeout_2020-02-27T09:42:28Z_unkownWorkflowTypeName_4418294404690464320_15619178330501475177.visibility", 237 expectedResult: true, 238 }, 239 } 240 241 for _, testCase := range testCases { 242 s.Equal(newWorkflowTypeNamePrecondition(testCase.workflowTypeName)(testCase.fileName), testCase.expectedResult) 243 } 244 245 }