github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/internal/topic/topicreadercommon/commit_range_test.go (about)

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