github.com/grafana/pyroscope@v1.18.0/pkg/metastore/index/store/partition_test.go (about)

     1  package store
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/grafana/pyroscope/pkg/test"
    10  )
    11  
    12  func TestPartition_Overlaps(t *testing.T) {
    13  	type args struct {
    14  		start time.Time
    15  		end   time.Time
    16  	}
    17  	tests := []struct {
    18  		name string
    19  		key  Partition
    20  		args args
    21  		want bool
    22  	}{
    23  		{
    24  			name: "simple overlap",
    25  			key:  NewPartition(test.Time("2024-09-11T06:00:00.000Z"), 6*time.Hour),
    26  			args: args{
    27  				start: test.Time("2024-09-11T07:15:24.123Z"),
    28  				end:   test.Time("2024-09-11T13:15:24.123Z"),
    29  			},
    30  			want: true,
    31  		},
    32  		{
    33  			name: "overlap at partition start",
    34  			key:  NewPartition(test.Time("2024-09-11T06:00:00.000Z"), 6*time.Hour),
    35  			args: args{
    36  				start: test.Time("2024-09-11T04:00:00.000Z"),
    37  				end:   test.Time("2024-09-11T06:00:00.000Z"),
    38  			},
    39  			want: true,
    40  		},
    41  		{
    42  			name: "no overlap close to partition start",
    43  			key:  NewPartition(test.Time("2024-09-11T06:00:00.000Z"), 6*time.Hour),
    44  			args: args{
    45  				start: test.Time("2024-09-11T04:00:00.000Z"),
    46  				end:   test.Time("2024-09-11T05:59:59.999Z"),
    47  			},
    48  			want: false,
    49  		},
    50  		{
    51  			name: "overlap at partition end",
    52  			key:  NewPartition(test.Time("2024-09-11T06:00:00.000Z"), 6*time.Hour),
    53  			args: args{
    54  				start: test.Time("2024-09-11T11:59:59.999Z"),
    55  				end:   test.Time("2024-09-11T13:00:00.000Z"),
    56  			},
    57  			want: true,
    58  		},
    59  		{
    60  			name: "no overlap close to partition end",
    61  			key:  NewPartition(test.Time("2024-09-11T06:00:00.000Z"), 6*time.Hour),
    62  			args: args{
    63  				start: test.Time("2024-09-11T12:00:00.000Z"),
    64  				end:   test.Time("2024-09-11T13:59:59.999Z"),
    65  			},
    66  			want: true,
    67  		},
    68  		{
    69  			name: "overlap around midnight",
    70  			key:  NewPartition(test.Time("2024-09-11T00:00:00.000Z"), 6*time.Hour),
    71  			args: args{
    72  				start: test.Time("2024-09-10T19:00:00.000Z"),
    73  				end:   test.Time("2024-09-11T00:01:01.999Z"),
    74  			},
    75  			want: true,
    76  		},
    77  		{
    78  			name: "partition fully contains interval",
    79  			key:  NewPartition(test.Time("2024-09-11T06:00:00.000Z"), 6*time.Hour),
    80  			args: args{
    81  				start: test.Time("2024-09-11T07:00:00.000Z"),
    82  				end:   test.Time("2024-09-11T08:01:01.999Z"),
    83  			},
    84  			want: true,
    85  		},
    86  		{
    87  			name: "interval fully contains partition",
    88  			key:  NewPartition(test.Time("2024-09-11T06:00:00.000Z"), 6*time.Hour),
    89  			args: args{
    90  				start: test.Time("2024-09-11T02:00:00.000Z"),
    91  				end:   test.Time("2024-09-11T13:01:01.999Z"),
    92  			},
    93  			want: true,
    94  		},
    95  	}
    96  	for _, tt := range tests {
    97  		t.Run(tt.name, func(t *testing.T) {
    98  			assert.Equalf(t, tt.want, tt.key.Overlaps(tt.args.start, tt.args.end), "overlaps(%v, %v)", tt.args.start, tt.args.end)
    99  		})
   100  	}
   101  }
   102  
   103  func TestPartitionKey_Equal(t *testing.T) {
   104  	k1 := NewPartition(test.Time("2024-09-11T02:00:00.000Z"), 2*time.Hour)
   105  	k2 := NewPartition(test.Time("2024-09-11T03:01:01.999Z"), 2*time.Hour)
   106  	assert.Equal(t, k1, k2)
   107  
   108  	k1 = NewPartition(test.Time("2024-09-11T02:00:00.000Z"), time.Hour)
   109  	k2 = NewPartition(test.Time("2024-09-11T03:01:01.999Z"), time.Hour)
   110  	assert.NotEqual(t, k1, k2)
   111  }
   112  
   113  func TestPartitionKey_Encoding(t *testing.T) {
   114  	k := NewPartition(time.Now(), time.Hour*6)
   115  	b, err := k.MarshalBinary()
   116  	assert.NoError(t, err)
   117  	var d Partition
   118  	err = d.UnmarshalBinary(b)
   119  	assert.NoError(t, err)
   120  	assert.Equal(t, k, d)
   121  }