go.temporal.io/server@v1.23.0/common/persistence/sql/sqlplugin/tests/history_tree.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 tests 26 27 import ( 28 "math/rand" 29 "testing" 30 31 "github.com/stretchr/testify/require" 32 "github.com/stretchr/testify/suite" 33 34 "go.temporal.io/server/common/persistence/sql/sqlplugin" 35 "go.temporal.io/server/common/primitives" 36 "go.temporal.io/server/common/shuffle" 37 ) 38 39 type ( 40 historyTreeSuite struct { 41 suite.Suite 42 *require.Assertions 43 44 store sqlplugin.HistoryTree 45 } 46 ) 47 48 const ( 49 testHistoryTreeEncoding = "random encoding" 50 ) 51 52 var ( 53 testHistoryTreeData = []byte("random history tree data") 54 ) 55 56 func NewHistoryTreeSuite( 57 t *testing.T, 58 store sqlplugin.HistoryTree, 59 ) *historyTreeSuite { 60 return &historyTreeSuite{ 61 Assertions: require.New(t), 62 store: store, 63 } 64 } 65 66 func (s *historyTreeSuite) SetupSuite() { 67 68 } 69 70 func (s *historyTreeSuite) TearDownSuite() { 71 72 } 73 74 func (s *historyTreeSuite) SetupTest() { 75 s.Assertions = require.New(s.T()) 76 } 77 78 func (s *historyTreeSuite) TearDownTest() { 79 80 } 81 82 func (s *historyTreeSuite) TestInsert_Success() { 83 shardID := rand.Int31() 84 treeID := primitives.NewUUID() 85 branchID := primitives.NewUUID() 86 87 node := s.newRandomTreeRow(shardID, treeID, branchID) 88 result, err := s.store.InsertIntoHistoryTree(newExecutionContext(), &node) 89 s.NoError(err) 90 rowsAffected, err := result.RowsAffected() 91 s.NoError(err) 92 s.Equal(1, int(rowsAffected)) 93 } 94 95 func (s *historyTreeSuite) TestInsert_Duplicate_Success() { 96 shardID := rand.Int31() 97 treeID := primitives.NewUUID() 98 branchID := primitives.NewUUID() 99 100 node := s.newRandomTreeRow(shardID, treeID, branchID) 101 result, err := s.store.InsertIntoHistoryTree(newExecutionContext(), &node) 102 s.NoError(err) 103 rowsAffected, err := result.RowsAffected() 104 s.NoError(err) 105 s.Equal(1, int(rowsAffected)) 106 107 node = s.newRandomTreeRow(shardID, treeID, branchID) 108 result, err = s.store.InsertIntoHistoryTree(newExecutionContext(), &node) 109 s.NoError(err) 110 _, err = result.RowsAffected() 111 s.NoError(err) 112 // TODO cannot assert on the number of rows affect 113 // since MySQL and PostgreSQL have different behavior 114 } 115 116 func (s *historyTreeSuite) TestInsertSelect() { 117 shardID := rand.Int31() 118 treeID := primitives.NewUUID() 119 branchID := primitives.NewUUID() 120 121 tree := s.newRandomTreeRow(shardID, treeID, branchID) 122 result, err := s.store.InsertIntoHistoryTree(newExecutionContext(), &tree) 123 s.NoError(err) 124 rowsAffected, err := result.RowsAffected() 125 s.NoError(err) 126 s.Equal(1, int(rowsAffected)) 127 128 selectFilter := sqlplugin.HistoryTreeSelectFilter{ 129 ShardID: shardID, 130 TreeID: treeID, 131 } 132 rows, err := s.store.SelectFromHistoryTree(newExecutionContext(), selectFilter) 133 s.NoError(err) 134 for index := range rows { 135 rows[index].ShardID = shardID 136 rows[index].TreeID = treeID 137 } 138 s.Equal([]sqlplugin.HistoryTreeRow{tree}, rows) 139 } 140 141 func (s *historyTreeSuite) TestDeleteSelect() { 142 shardID := rand.Int31() 143 treeID := primitives.NewUUID() 144 branchID := primitives.NewUUID() 145 146 deleteFilter := sqlplugin.HistoryTreeDeleteFilter{ 147 ShardID: shardID, 148 TreeID: treeID, 149 BranchID: branchID, 150 } 151 result, err := s.store.DeleteFromHistoryTree(newExecutionContext(), deleteFilter) 152 s.NoError(err) 153 rowsAffected, err := result.RowsAffected() 154 s.NoError(err) 155 s.Equal(0, int(rowsAffected)) 156 157 selectFilter := sqlplugin.HistoryTreeSelectFilter{ 158 ShardID: shardID, 159 TreeID: treeID, 160 } 161 rows, err := s.store.SelectFromHistoryTree(newExecutionContext(), selectFilter) 162 s.NoError(err) 163 for index := range rows { 164 rows[index].ShardID = shardID 165 rows[index].TreeID = treeID 166 } 167 s.Equal([]sqlplugin.HistoryTreeRow(nil), rows) 168 } 169 170 func (s *historyTreeSuite) TestInsertDeleteSelect() { 171 shardID := rand.Int31() 172 treeID := primitives.NewUUID() 173 branchID := primitives.NewUUID() 174 175 tree := s.newRandomTreeRow(shardID, treeID, branchID) 176 result, err := s.store.InsertIntoHistoryTree(newExecutionContext(), &tree) 177 s.NoError(err) 178 rowsAffected, err := result.RowsAffected() 179 s.NoError(err) 180 s.Equal(1, int(rowsAffected)) 181 182 deleteFilter := sqlplugin.HistoryTreeDeleteFilter{ 183 ShardID: shardID, 184 TreeID: treeID, 185 BranchID: branchID, 186 } 187 result, err = s.store.DeleteFromHistoryTree(newExecutionContext(), deleteFilter) 188 s.NoError(err) 189 rowsAffected, err = result.RowsAffected() 190 s.NoError(err) 191 s.Equal(1, int(rowsAffected)) 192 193 selectFilter := sqlplugin.HistoryTreeSelectFilter{ 194 ShardID: shardID, 195 TreeID: treeID, 196 } 197 rows, err := s.store.SelectFromHistoryTree(newExecutionContext(), selectFilter) 198 s.NoError(err) 199 for index := range rows { 200 rows[index].ShardID = shardID 201 rows[index].TreeID = treeID 202 } 203 s.Equal([]sqlplugin.HistoryTreeRow(nil), rows) 204 } 205 206 func (s *historyTreeSuite) newRandomTreeRow( 207 shardID int32, 208 treeID primitives.UUID, 209 branchID primitives.UUID, 210 ) sqlplugin.HistoryTreeRow { 211 return sqlplugin.HistoryTreeRow{ 212 ShardID: shardID, 213 TreeID: treeID, 214 BranchID: branchID, 215 Data: shuffle.Bytes(testHistoryTreeData), 216 DataEncoding: testHistoryTreeEncoding, 217 } 218 }