github.com/grafana/pyroscope@v1.18.0/pkg/segmentwriter/client/distributor/placement/placement_test.go (about)

     1  package placement
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/grafana/dskit/ring"
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/grafana/pyroscope/pkg/iter"
    10  )
    11  
    12  func Test_ActiveInstances(t *testing.T) {
    13  	for _, test := range []struct {
    14  		description string
    15  		instances   []ring.InstanceDesc
    16  		expected    []ring.InstanceDesc
    17  	}{
    18  		{
    19  			description: "empty",
    20  		},
    21  		{
    22  			description: "all active",
    23  			instances: []ring.InstanceDesc{
    24  				{Id: "a", State: ring.ACTIVE},
    25  				{Id: "b", State: ring.ACTIVE},
    26  				{Id: "c", State: ring.ACTIVE},
    27  			},
    28  			expected: []ring.InstanceDesc{
    29  				{Id: "a", State: ring.ACTIVE},
    30  				{Id: "b", State: ring.ACTIVE},
    31  				{Id: "c", State: ring.ACTIVE},
    32  			},
    33  		},
    34  		{
    35  			description: "active duplicate",
    36  			instances: []ring.InstanceDesc{
    37  				{Id: "a", State: ring.ACTIVE},
    38  				{Id: "a", State: ring.ACTIVE},
    39  			},
    40  			expected: []ring.InstanceDesc{
    41  				{Id: "a", State: ring.ACTIVE},
    42  			},
    43  		},
    44  		{
    45  			description: "non-active duplicate",
    46  			instances: []ring.InstanceDesc{
    47  				{Id: "a", State: ring.PENDING},
    48  				{Id: "a", State: ring.PENDING},
    49  			},
    50  		},
    51  		{
    52  			description: "mixed",
    53  			instances: []ring.InstanceDesc{
    54  				{Id: "a", State: ring.PENDING},
    55  				{Id: "b", State: ring.ACTIVE},
    56  				{Id: "c", State: ring.JOINING},
    57  				{Id: "d", State: ring.LEAVING},
    58  				{Id: "c", State: ring.JOINING},
    59  				{Id: "e", State: ring.ACTIVE},
    60  				{Id: "e", State: ring.ACTIVE},
    61  			},
    62  			expected: []ring.InstanceDesc{
    63  				{Id: "b", State: ring.ACTIVE},
    64  				{Id: "e", State: ring.ACTIVE},
    65  			},
    66  		},
    67  	} {
    68  		active := ActiveInstances(iter.NewSliceIterator(test.instances))
    69  		assert.Equal(t, test.expected, iter.MustSlice(active))
    70  	}
    71  }