github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/distributor/distributor_ring_test.go (about)

     1  package distributor
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/grafana/dskit/flagext"
     8  	"github.com/grafana/dskit/ring"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestRingConfig_DefaultConfigToLifecyclerConfig(t *testing.T) {
    13  	cfg := RingConfig{}
    14  	expected := ring.LifecyclerConfig{}
    15  
    16  	flagext.DefaultValues(&cfg)
    17  	flagext.DefaultValues(&expected)
    18  
    19  	// The default config of the distributor ring must be the exact same
    20  	// of the default lifecycler config, except few options which are
    21  	// intentionally overridden
    22  	expected.ListenPort = cfg.ListenPort
    23  	expected.RingConfig.ReplicationFactor = 1
    24  	expected.NumTokens = 1
    25  	expected.MinReadyDuration = 0
    26  	expected.FinalSleep = 0
    27  
    28  	assert.Equal(t, expected, cfg.ToLifecyclerConfig())
    29  }
    30  
    31  func TestRingConfig_CustomConfigToLifecyclerConfig(t *testing.T) {
    32  	cfg := RingConfig{}
    33  	expected := ring.LifecyclerConfig{}
    34  
    35  	flagext.DefaultValues(&cfg)
    36  	flagext.DefaultValues(&expected)
    37  
    38  	// Customize the distributor ring config
    39  	cfg.HeartbeatPeriod = 1 * time.Second
    40  	cfg.HeartbeatTimeout = 10 * time.Second
    41  	cfg.InstanceID = "test"
    42  	cfg.InstanceInterfaceNames = []string{"abc1"}
    43  	cfg.InstancePort = 10
    44  	cfg.InstanceAddr = "1.2.3.4"
    45  	cfg.ListenPort = 10
    46  
    47  	// The lifecycler config should be generated based upon the distributor
    48  	// ring config
    49  	expected.HeartbeatPeriod = cfg.HeartbeatPeriod
    50  	expected.RingConfig.HeartbeatTimeout = cfg.HeartbeatTimeout
    51  	expected.ID = cfg.InstanceID
    52  	expected.InfNames = cfg.InstanceInterfaceNames
    53  	expected.Port = cfg.InstancePort
    54  	expected.Addr = cfg.InstanceAddr
    55  	expected.ListenPort = cfg.ListenPort
    56  
    57  	// Hardcoded config
    58  	expected.RingConfig.ReplicationFactor = 1
    59  	expected.NumTokens = 1
    60  	expected.MinReadyDuration = 0
    61  	expected.FinalSleep = 0
    62  
    63  	assert.Equal(t, expected, cfg.ToLifecyclerConfig())
    64  }