github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/storage/stores/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: 5287603155525329, 26 exp: true, 27 }, 28 { 29 shard: NewShard(0, 2), 30 fp: 1 << 63, 31 exp: false, 32 }, 33 { 34 shard: NewShard(1, 2), 35 fp: 0, 36 exp: false, 37 }, 38 { 39 shard: NewShard(1, 2), 40 fp: 1 << 63, 41 exp: true, 42 }, 43 { 44 shard: NewShard(2, 4), 45 fp: 0, 46 exp: false, 47 }, 48 { 49 shard: NewShard(2, 4), 50 fp: 1 << 63, 51 exp: true, 52 }, 53 { 54 shard: NewShard(2, 4), 55 fp: 3 << 62, 56 exp: false, 57 }, 58 { 59 shard: NewShard(0, 1), 60 fp: 5287603155525329, 61 exp: true, 62 }, 63 } { 64 t.Run(fmt.Sprint(tc.shard, tc.fp), func(t *testing.T) { 65 require.Equal(t, tc.exp, tc.shard.Match(model.Fingerprint(tc.fp))) 66 }) 67 } 68 } 69 70 func TestShardBounds(t *testing.T) { 71 for _, tc := range []struct { 72 shard ShardAnnotation 73 from, through uint64 74 }{ 75 { 76 shard: NewShard(0, 1), 77 from: 0, 78 through: math.MaxUint64, 79 }, 80 { 81 shard: NewShard(0, 2), 82 from: 0, 83 through: 1 << 63, 84 }, 85 { 86 shard: NewShard(1, 2), 87 from: 1 << 63, 88 through: math.MaxUint64, 89 }, 90 { 91 shard: NewShard(1, 4), 92 from: 1 << 62, 93 through: 2 << 62, 94 }, 95 { 96 shard: NewShard(2, 4), 97 from: 2 << 62, 98 through: 3 << 62, 99 }, 100 { 101 shard: NewShard(3, 4), 102 from: 3 << 62, 103 through: math.MaxUint64, 104 }, 105 } { 106 t.Run(tc.shard.String(), func(t *testing.T) { 107 from, through := tc.shard.Bounds() 108 require.Equal(t, model.Fingerprint(tc.from), from) 109 require.Equal(t, model.Fingerprint(tc.through), through) 110 }) 111 } 112 } 113 114 func TestShardValidate(t *testing.T) { 115 for _, tc := range []struct { 116 desc string 117 factor uint32 118 err bool 119 }{ 120 { 121 factor: 0, 122 err: false, 123 }, 124 { 125 factor: 1, 126 err: true, 127 }, 128 { 129 factor: 2, 130 err: false, 131 }, 132 } { 133 t.Run(fmt.Sprint(tc.factor), func(t *testing.T) { 134 err := NewShard(0, tc.factor).Validate() 135 if tc.err { 136 require.NotNil(t, err) 137 } else { 138 require.Nil(t, err) 139 } 140 }) 141 } 142 }