github.com/grafana/pyroscope@v1.18.0/pkg/phlaredb/tsdb/index/shard_test.go (about)

     1  package index
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"testing"
     7  
     8  	"github.com/prometheus/common/model"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestShardMatch(t *testing.T) {
    13  	for _, tc := range []struct {
    14  		shard ShardAnnotation
    15  		fp    uint64
    16  		exp   bool
    17  	}{
    18  		{
    19  			shard: NewShard(0, 2),
    20  			fp:    0,
    21  			exp:   true,
    22  		},
    23  		{
    24  			shard: NewShard(0, 2),
    25  			fp:    1 << 63,
    26  			exp:   false,
    27  		},
    28  		{
    29  			shard: NewShard(1, 2),
    30  			fp:    0,
    31  			exp:   false,
    32  		},
    33  		{
    34  			shard: NewShard(1, 2),
    35  			fp:    1 << 63,
    36  			exp:   true,
    37  		},
    38  		{
    39  			shard: NewShard(2, 4),
    40  			fp:    0,
    41  			exp:   false,
    42  		},
    43  		{
    44  			shard: NewShard(2, 4),
    45  			fp:    1 << 63,
    46  			exp:   true,
    47  		},
    48  		{
    49  			shard: NewShard(2, 4),
    50  			fp:    3 << 62,
    51  			exp:   false,
    52  		},
    53  	} {
    54  		t.Run(fmt.Sprint(tc.shard, tc.fp), func(t *testing.T) {
    55  			require.Equal(t, tc.exp, tc.shard.Match(model.Fingerprint(tc.fp)))
    56  		})
    57  	}
    58  }
    59  
    60  func TestShardBounds(t *testing.T) {
    61  	for _, tc := range []struct {
    62  		shard         ShardAnnotation
    63  		from, through uint64
    64  	}{
    65  		{
    66  			shard:   NewShard(0, 2),
    67  			from:    0,
    68  			through: 1 << 63,
    69  		},
    70  		{
    71  			shard:   NewShard(1, 2),
    72  			from:    1 << 63,
    73  			through: math.MaxUint64,
    74  		},
    75  		{
    76  			shard:   NewShard(1, 4),
    77  			from:    1 << 62,
    78  			through: 2 << 62,
    79  		},
    80  		{
    81  			shard:   NewShard(2, 4),
    82  			from:    2 << 62,
    83  			through: 3 << 62,
    84  		},
    85  		{
    86  			shard:   NewShard(3, 4),
    87  			from:    3 << 62,
    88  			through: math.MaxUint64,
    89  		},
    90  	} {
    91  		t.Run(tc.shard.String(), func(t *testing.T) {
    92  			from, through := tc.shard.Bounds()
    93  			require.Equal(t, model.Fingerprint(tc.from), from)
    94  			require.Equal(t, model.Fingerprint(tc.through), through)
    95  		})
    96  	}
    97  }