MCPcopy
hub / github.com/grafana/dskit / TestRing_GetAllHealthy

Function TestRing_GetAllHealthy

ring/ring_test.go:958–1015  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

956}
957
958func TestRing_GetAllHealthy(t *testing.T) {
959 const heartbeatTimeout = time.Minute
960 now := time.Now()
961
962 tests := map[string]struct {
963 ringInstances map[string]InstanceDesc
964 expectedErrForRead error
965 expectedSetForRead []string
966 expectedErrForWrite error
967 expectedSetForWrite []string
968 expectedErrForReporting error
969 expectedSetForReporting []string
970 }{
971 "should return error on empty ring": {
972 ringInstances: nil,
973 expectedErrForRead: ErrEmptyRing,
974 expectedErrForWrite: ErrEmptyRing,
975 expectedErrForReporting: ErrEmptyRing,
976 },
977 "should return all healthy instances for the given operation": {
978 ringInstances: map[string]InstanceDesc{
979 "instance-1": {Addr: "127.0.0.1", State: ACTIVE, Timestamp: now.Unix()},
980 "instance-2": {Addr: "127.0.0.2", State: PENDING, Timestamp: now.Add(-10 * time.Second).Unix()},
981 "instance-3": {Addr: "127.0.0.3", State: JOINING, Timestamp: now.Add(-20 * time.Second).Unix()},
982 "instance-4": {Addr: "127.0.0.4", State: LEAVING, Timestamp: now.Add(-30 * time.Second).Unix()},
983 "instance-5": {Addr: "127.0.0.5", State: ACTIVE, Timestamp: now.Add(-2 * time.Minute).Unix()},
984 },
985 expectedSetForRead: []string{"127.0.0.1", "127.0.0.2", "127.0.0.4"},
986 expectedSetForWrite: []string{"127.0.0.1"},
987 expectedSetForReporting: []string{"127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.0.4"},
988 },
989 }
990
991 for testName, testData := range tests {
992 t.Run(testName, func(t *testing.T) {
993 // Init the ring.
994 ringDesc := &Desc{Ingesters: testData.ringInstances}
995 for id, instance := range ringDesc.Ingesters {
996 ringDesc.Ingesters[id] = instance
997 }
998
999 ring := newRingForTesting(Config{HeartbeatTimeout: heartbeatTimeout}, false)
1000 ring.setRingStateFromDesc(ringDesc, false, false, false)
1001
1002 set, err := ring.GetAllHealthy(Read)
1003 require.Equal(t, testData.expectedErrForRead, err)
1004 assert.ElementsMatch(t, testData.expectedSetForRead, set.GetAddresses())
1005
1006 set, err = ring.GetAllHealthy(Write)
1007 require.Equal(t, testData.expectedErrForWrite, err)
1008 assert.ElementsMatch(t, testData.expectedSetForWrite, set.GetAddresses())
1009
1010 set, err = ring.GetAllHealthy(Reporting)
1011 require.Equal(t, testData.expectedErrForReporting, err)
1012 assert.ElementsMatch(t, testData.expectedSetForReporting, set.GetAddresses())
1013 })
1014 }
1015}

Callers

nothing calls this directly

Calls 7

newRingForTestingFunction · 0.85
RunMethod · 0.80
setRingStateFromDescMethod · 0.80
GetAddressesMethod · 0.80
AddMethod · 0.65
GetAllHealthyMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected