github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/topic/topicreaderinternal/commit_range_test.go (about) 1 package topicreaderinternal 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 8 "github.com/ydb-platform/ydb-go-sdk/v3/internal/grpcwrapper/rawtopic/rawtopicreader" 9 ) 10 11 var _ PublicCommitRangeGetter = &PublicMessage{} 12 13 var _ PublicCommitRangeGetter = &PublicBatch{} 14 15 func TestCompressCommitsInplace(t *testing.T) { 16 session1 := &partitionSession{partitionSessionID: 1} 17 session2 := &partitionSession{partitionSessionID: 2} 18 table := []struct { 19 name string 20 source []commitRange 21 expected []commitRange 22 }{ 23 { 24 name: "Empty", 25 source: nil, 26 expected: nil, 27 }, 28 { 29 name: "OneCommit", 30 source: []commitRange{ 31 { 32 commitOffsetStart: 1, 33 commitOffsetEnd: 2, 34 partitionSession: session1, 35 }, 36 }, 37 expected: []commitRange{ 38 { 39 commitOffsetStart: 1, 40 commitOffsetEnd: 2, 41 partitionSession: session1, 42 }, 43 }, 44 }, 45 { 46 name: "CompressedToOne", 47 source: []commitRange{ 48 { 49 commitOffsetStart: 1, 50 commitOffsetEnd: 2, 51 partitionSession: session1, 52 }, 53 { 54 commitOffsetStart: 2, 55 commitOffsetEnd: 5, 56 partitionSession: session1, 57 }, 58 { 59 commitOffsetStart: 5, 60 commitOffsetEnd: 10, 61 partitionSession: session1, 62 }, 63 }, 64 expected: []commitRange{ 65 { 66 commitOffsetStart: 1, 67 commitOffsetEnd: 10, 68 partitionSession: session1, 69 }, 70 }, 71 }, 72 { 73 name: "CompressedUnordered", 74 source: []commitRange{ 75 { 76 commitOffsetStart: 5, 77 commitOffsetEnd: 10, 78 partitionSession: session1, 79 }, 80 { 81 commitOffsetStart: 2, 82 commitOffsetEnd: 5, 83 partitionSession: session1, 84 }, 85 { 86 commitOffsetStart: 1, 87 commitOffsetEnd: 2, 88 partitionSession: session1, 89 }, 90 }, 91 expected: []commitRange{ 92 { 93 commitOffsetStart: 1, 94 commitOffsetEnd: 10, 95 partitionSession: session1, 96 }, 97 }, 98 }, 99 { 100 name: "CompressDifferentSessionsSeparated", 101 source: []commitRange{ 102 { 103 commitOffsetStart: 1, 104 commitOffsetEnd: 2, 105 partitionSession: session1, 106 }, 107 { 108 commitOffsetStart: 2, 109 commitOffsetEnd: 3, 110 partitionSession: session2, 111 }, 112 }, 113 expected: []commitRange{ 114 { 115 commitOffsetStart: 1, 116 commitOffsetEnd: 2, 117 partitionSession: session1, 118 }, 119 { 120 commitOffsetStart: 2, 121 commitOffsetEnd: 3, 122 partitionSession: session2, 123 }, 124 }, 125 }, 126 { 127 name: "CompressTwoSessions", 128 source: []commitRange{ 129 { 130 commitOffsetStart: 1, 131 commitOffsetEnd: 1, 132 partitionSession: session1, 133 }, 134 { 135 commitOffsetStart: 2, 136 commitOffsetEnd: 3, 137 partitionSession: session2, 138 }, 139 { 140 commitOffsetStart: 1, 141 commitOffsetEnd: 3, 142 partitionSession: session1, 143 }, 144 { 145 commitOffsetStart: 3, 146 commitOffsetEnd: 5, 147 partitionSession: session2, 148 }, 149 }, 150 expected: []commitRange{ 151 { 152 commitOffsetStart: 1, 153 commitOffsetEnd: 3, 154 partitionSession: session1, 155 }, 156 { 157 commitOffsetStart: 2, 158 commitOffsetEnd: 5, 159 partitionSession: session2, 160 }, 161 }, 162 }, 163 } 164 for _, test := range table { 165 t.Run(test.name, func(t *testing.T) { 166 var v CommitRanges 167 v.ranges = test.source 168 v.optimize() 169 require.Equal(t, test.expected, v.ranges) 170 }) 171 } 172 } 173 174 func TestCommitsToRawPartitionCommitOffset(t *testing.T) { 175 session1 := &partitionSession{partitionSessionID: 1} 176 session2 := &partitionSession{partitionSessionID: 2} 177 178 table := []struct { 179 name string 180 source []commitRange 181 expected []rawtopicreader.PartitionCommitOffset 182 }{ 183 { 184 name: "Empty", 185 source: nil, 186 expected: nil, 187 }, 188 { 189 name: "OneCommit", 190 source: []commitRange{ 191 {commitOffsetStart: 1, commitOffsetEnd: 2, partitionSession: session1}, 192 }, 193 expected: []rawtopicreader.PartitionCommitOffset{ 194 { 195 PartitionSessionID: 1, 196 Offsets: []rawtopicreader.OffsetRange{ 197 {Start: 1, End: 2}, 198 }, 199 }, 200 }, 201 }, 202 { 203 name: "NeighboursWithOneSession", 204 source: []commitRange{ 205 {commitOffsetStart: 1, commitOffsetEnd: 2, partitionSession: session1}, 206 {commitOffsetStart: 10, commitOffsetEnd: 20, partitionSession: session1}, 207 {commitOffsetStart: 30, commitOffsetEnd: 40, partitionSession: session1}, 208 }, 209 expected: []rawtopicreader.PartitionCommitOffset{ 210 { 211 PartitionSessionID: 1, 212 Offsets: []rawtopicreader.OffsetRange{ 213 {Start: 1, End: 2}, 214 {Start: 10, End: 20}, 215 {Start: 30, End: 40}, 216 }, 217 }, 218 }, 219 }, 220 { 221 name: "TwoSessionsSameOffsets", 222 source: []commitRange{ 223 {commitOffsetStart: 1, commitOffsetEnd: 2, partitionSession: session1}, 224 {commitOffsetStart: 10, commitOffsetEnd: 20, partitionSession: session1}, 225 {commitOffsetStart: 1, commitOffsetEnd: 2, partitionSession: session2}, 226 {commitOffsetStart: 10, commitOffsetEnd: 20, partitionSession: session2}, 227 }, 228 expected: []rawtopicreader.PartitionCommitOffset{ 229 { 230 PartitionSessionID: 1, 231 Offsets: []rawtopicreader.OffsetRange{ 232 {Start: 1, End: 2}, 233 {Start: 10, End: 20}, 234 }, 235 }, 236 { 237 PartitionSessionID: 2, 238 Offsets: []rawtopicreader.OffsetRange{ 239 {Start: 1, End: 2}, 240 {Start: 10, End: 20}, 241 }, 242 }, 243 }, 244 }, 245 { 246 name: "TwoSessionsWithDifferenceOffsets", 247 source: []commitRange{ 248 {commitOffsetStart: 1, commitOffsetEnd: 2, partitionSession: session1}, 249 {commitOffsetStart: 10, commitOffsetEnd: 20, partitionSession: session1}, 250 {commitOffsetStart: 1, commitOffsetEnd: 2, partitionSession: session2}, 251 {commitOffsetStart: 3, commitOffsetEnd: 4, partitionSession: session2}, 252 {commitOffsetStart: 5, commitOffsetEnd: 6, partitionSession: session2}, 253 }, 254 expected: []rawtopicreader.PartitionCommitOffset{ 255 { 256 PartitionSessionID: 1, 257 Offsets: []rawtopicreader.OffsetRange{ 258 {Start: 1, End: 2}, 259 {Start: 10, End: 20}, 260 }, 261 }, 262 { 263 PartitionSessionID: 2, 264 Offsets: []rawtopicreader.OffsetRange{ 265 {Start: 1, End: 2}, 266 {Start: 3, End: 4}, 267 {Start: 5, End: 6}, 268 }, 269 }, 270 }, 271 }, 272 } 273 274 for _, test := range table { 275 t.Run(test.name, func(t *testing.T) { 276 var v CommitRanges 277 v.ranges = test.source 278 res := v.toRawPartitionCommitOffset() 279 require.Equal(t, test.expected, res) 280 }) 281 } 282 } 283 284 func testNewCommitRanges(commitable ...PublicCommitRangeGetter) *CommitRanges { 285 var res CommitRanges 286 res.Append(commitable...) 287 288 return &res 289 }