github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/cmd/migrate/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func Test_calcSyncRanges(t *testing.T) {
    11  	type args struct {
    12  		from    int64
    13  		to      int64
    14  		shardBy int64
    15  	}
    16  	tests := []struct {
    17  		name string
    18  		args args
    19  		want []*syncRange
    20  	}{
    21  		{
    22  			name: "one range",
    23  			args: args{
    24  				from:    0,
    25  				to:      10,
    26  				shardBy: 10,
    27  			},
    28  			want: []*syncRange{
    29  				{
    30  					from:   0,
    31  					to:     10,
    32  					number: 0,
    33  				},
    34  			},
    35  		},
    36  		{
    37  			name: "two ranges",
    38  			args: args{
    39  				from:    0,
    40  				to:      20,
    41  				shardBy: 10,
    42  			},
    43  			want: []*syncRange{
    44  				{
    45  					from:   0,
    46  					to:     10,
    47  					number: 0,
    48  				},
    49  				{
    50  					from:   11,
    51  					to:     20,
    52  					number: 1,
    53  				},
    54  			},
    55  		},
    56  		{
    57  			name: "three ranges",
    58  			args: args{
    59  				from:    0,
    60  				to:      20,
    61  				shardBy: 6,
    62  			},
    63  			want: []*syncRange{
    64  				{
    65  					from:   0,
    66  					to:     6,
    67  					number: 0,
    68  				},
    69  				{
    70  					from:   7,
    71  					to:     12,
    72  					number: 1,
    73  				},
    74  				{
    75  					from:   13,
    76  					to:     18,
    77  					number: 2,
    78  				},
    79  				{
    80  					from:   19,
    81  					to:     20,
    82  					number: 3,
    83  				},
    84  			},
    85  		},
    86  		{
    87  			name: "four ranges actual data",
    88  			args: args{
    89  				from:    1583798400000000000,
    90  				to:      1583884800000000000,
    91  				shardBy: 21600000000000,
    92  			},
    93  			want: []*syncRange{
    94  				{
    95  					from:   1583798400000000000,
    96  					to:     1583820000000000000,
    97  					number: 0,
    98  				},
    99  				{
   100  					from:   1583820000000000001,
   101  					to:     1583841600000000000,
   102  					number: 1,
   103  				},
   104  				{
   105  					from:   1583841600000000001,
   106  					to:     1583863200000000000,
   107  					number: 2,
   108  				},
   109  				{
   110  					from:   1583863200000000001,
   111  					to:     1583884800000000000,
   112  					number: 3,
   113  				},
   114  			},
   115  		},
   116  	}
   117  	for _, tt := range tests {
   118  		t.Run(tt.name, func(t *testing.T) {
   119  			if got := calcSyncRanges(tt.args.from, tt.args.to, tt.args.shardBy); !reflect.DeepEqual(got, tt.want) {
   120  				assert.Equal(t, tt.want, got)
   121  			}
   122  		})
   123  	}
   124  }