github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/aggregator/sharding/shard_set_test.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package sharding
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  	yaml "gopkg.in/yaml.v2"
    28  )
    29  
    30  func TestShardSetParseShardSetErrors(t *testing.T) {
    31  	tests := []struct {
    32  		yaml        string
    33  		expectedErr string
    34  	}{
    35  		{yaml: `huh`, expectedErr: "invalid range 'huh'"},
    36  		{yaml: `2..1`, expectedErr: "invalid range: 2 > 1"},
    37  	}
    38  
    39  	for _, test := range tests {
    40  		var shards ShardSet
    41  		err := yaml.Unmarshal([]byte(test.yaml), &shards)
    42  		require.Error(t, err)
    43  		require.Equal(t, test.expectedErr, err.Error())
    44  		require.Equal(t, 0, len(shards))
    45  	}
    46  }
    47  
    48  func TestShardSetParseShardSet(t *testing.T) {
    49  	tests := []struct {
    50  		yaml     string
    51  		expected []uint32
    52  	}{
    53  		{yaml: `shards: 76`, expected: []uint32{76}},
    54  		{yaml: `shards: [3, 6, 5]`, expected: []uint32{3, 5, 6}},
    55  		{yaml: `shards: ["3"]`, expected: []uint32{3}},
    56  		{yaml: `shards: ["3..8"]`, expected: []uint32{3, 4, 5, 6, 7, 8}},
    57  		{yaml: `shards: ["3", "3..8"]`, expected: []uint32{3, 4, 5, 6, 7, 8}},
    58  		{yaml: `shards: ["3", "3..8", 9]`, expected: []uint32{3, 4, 5, 6, 7, 8, 9}},
    59  		{yaml: `shards: 3`, expected: []uint32{3}},
    60  		{yaml: `shards: "3"`, expected: []uint32{3}},
    61  		{yaml: `shards: "3..8"`, expected: []uint32{3, 4, 5, 6, 7, 8}},
    62  	}
    63  
    64  	for i, test := range tests {
    65  		var cfg struct {
    66  			Shards ShardSet
    67  		}
    68  
    69  		err := yaml.Unmarshal([]byte(test.yaml), &cfg)
    70  		require.NoError(t, err, "received error for test %d", i)
    71  		ValidateShardSet(t, test.expected, cfg.Shards)
    72  	}
    73  }
    74  
    75  func TestParseShardSet(t *testing.T) {
    76  	tests := []struct {
    77  		str      string
    78  		expected []uint32
    79  	}{
    80  		{str: `76`, expected: []uint32{76}},
    81  		{str: `3..8`, expected: []uint32{3, 4, 5, 6, 7, 8}},
    82  		{str: `3..3`, expected: []uint32{3}},
    83  	}
    84  
    85  	for _, test := range tests {
    86  		parsed, err := ParseShardSet(test.str)
    87  		require.NoError(t, err)
    88  		ValidateShardSet(t, test.expected, parsed)
    89  	}
    90  }
    91  
    92  func TestParseShardSetErrors(t *testing.T) {
    93  	tests := []string{
    94  		`huh`,
    95  		`76..`,
    96  		`2..1`,
    97  	}
    98  
    99  	for _, test := range tests {
   100  		_, err := ParseShardSet(test)
   101  		require.Error(t, err)
   102  	}
   103  }
   104  
   105  func TestMustParseShardSet(t *testing.T) {
   106  	tests := []struct {
   107  		str      string
   108  		expected []uint32
   109  	}{
   110  		{str: `76`, expected: []uint32{76}},
   111  		{str: `3..8`, expected: []uint32{3, 4, 5, 6, 7, 8}},
   112  		{str: `3..3`, expected: []uint32{3}},
   113  	}
   114  
   115  	for _, test := range tests {
   116  		parsed := MustParseShardSet(test.str)
   117  		ValidateShardSet(t, test.expected, parsed)
   118  	}
   119  }
   120  
   121  func TestMustParseShardSetPanics(t *testing.T) {
   122  	tests := []string{
   123  		`huh`,
   124  		`76..`,
   125  		`2..1`,
   126  	}
   127  
   128  	for _, test := range tests {
   129  		require.Panics(t, func() { MustParseShardSet(test) })
   130  	}
   131  }