(t *testing.T)
| 1613 | } |
| 1614 | |
| 1615 | func TestRing_GetWritableInstancesWithTokensCounts(t *testing.T) { |
| 1616 | gen := initTokenGenerator(t) |
| 1617 | |
| 1618 | tests := map[string]struct { |
| 1619 | ringInstances map[string]InstanceDesc |
| 1620 | expectedWritableInstancesWithTokensCount int |
| 1621 | expectedWritableInstancesWithTokensCountPerZone map[string]int |
| 1622 | }{ |
| 1623 | "empty ring": { |
| 1624 | ringInstances: nil, |
| 1625 | expectedWritableInstancesWithTokensCount: 0, |
| 1626 | expectedWritableInstancesWithTokensCountPerZone: map[string]int{}, |
| 1627 | }, |
| 1628 | "single zone, no tokens": { |
| 1629 | ringInstances: map[string]InstanceDesc{ |
| 1630 | "instance-1": {Addr: "127.0.0.1", Zone: "zone-a", State: ACTIVE, Tokens: []uint32{}}, |
| 1631 | "instance-2": {Addr: "127.0.0.2", Zone: "zone-a", State: LEAVING, Tokens: []uint32{}, ReadOnly: true}, |
| 1632 | "instance-3": {Addr: "127.0.0.3", Zone: "zone-a", State: PENDING, Tokens: []uint32{}}, |
| 1633 | "instance-4": {Addr: "127.0.0.4", Zone: "zone-a", State: JOINING, Tokens: []uint32{}}, |
| 1634 | }, |
| 1635 | expectedWritableInstancesWithTokensCount: 0, |
| 1636 | expectedWritableInstancesWithTokensCountPerZone: map[string]int{"zone-a": 0}, |
| 1637 | }, |
| 1638 | "single zone, some tokens": { |
| 1639 | ringInstances: map[string]InstanceDesc{ |
| 1640 | "instance-1": {Addr: "127.0.0.1", Zone: "zone-a", State: ACTIVE, Tokens: gen.GenerateTokens(128, nil)}, |
| 1641 | "instance-2": {Addr: "127.0.0.2", Zone: "zone-a", State: ACTIVE, Tokens: []uint32{}}, |
| 1642 | "instance-3": {Addr: "127.0.0.3", Zone: "zone-a", State: LEAVING, Tokens: gen.GenerateTokens(128, nil)}, |
| 1643 | "instance-4": {Addr: "127.0.0.4", Zone: "zone-a", State: LEAVING, Tokens: []uint32{}}, |
| 1644 | "instance-5": {Addr: "127.0.0.5", Zone: "zone-a", State: PENDING, Tokens: gen.GenerateTokens(128, nil)}, |
| 1645 | "instance-6": {Addr: "127.0.0.6", Zone: "zone-a", State: PENDING, Tokens: []uint32{}, ReadOnly: true}, |
| 1646 | "instance-7": {Addr: "127.0.0.7", Zone: "zone-a", State: JOINING, Tokens: gen.GenerateTokens(128, nil), ReadOnly: true}, |
| 1647 | "instance-8": {Addr: "127.0.0.8", Zone: "zone-a", State: JOINING, Tokens: []uint32{}, ReadOnly: true}, |
| 1648 | }, |
| 1649 | expectedWritableInstancesWithTokensCount: 3, |
| 1650 | expectedWritableInstancesWithTokensCountPerZone: map[string]int{"zone-a": 3}, |
| 1651 | }, |
| 1652 | "multiple zones": { |
| 1653 | ringInstances: map[string]InstanceDesc{ |
| 1654 | "instance-1": {Addr: "127.0.0.1", Zone: "zone-a", State: ACTIVE, Tokens: gen.GenerateTokens(128, nil)}, |
| 1655 | "instance-2": {Addr: "127.0.0.2", Zone: "zone-a", State: ACTIVE, Tokens: []uint32{}, ReadOnly: true}, |
| 1656 | "instance-3": {Addr: "127.0.0.3", Zone: "zone-b", State: LEAVING, Tokens: gen.GenerateTokens(128, nil), ReadOnly: true}, |
| 1657 | "instance-4": {Addr: "127.0.0.4", Zone: "zone-b", State: LEAVING, Tokens: []uint32{}}, |
| 1658 | "instance-5": {Addr: "127.0.0.5", Zone: "zone-c", State: PENDING, Tokens: gen.GenerateTokens(128, nil)}, |
| 1659 | "instance-6": {Addr: "127.0.0.6", Zone: "zone-d", State: PENDING, Tokens: []uint32{}}, |
| 1660 | "instance-7": {Addr: "127.0.0.7", Zone: "zone-c", State: JOINING, Tokens: gen.GenerateTokens(128, nil)}, |
| 1661 | "instance-8": {Addr: "127.0.0.8", Zone: "zone-d", State: JOINING, Tokens: []uint32{}, ReadOnly: true}, |
| 1662 | }, |
| 1663 | expectedWritableInstancesWithTokensCount: 3, |
| 1664 | expectedWritableInstancesWithTokensCountPerZone: map[string]int{"zone-a": 1, "zone-b": 0, "zone-c": 2, "zone-d": 0}, |
| 1665 | }, |
| 1666 | } |
| 1667 | |
| 1668 | for testName, testData := range tests { |
| 1669 | t.Run(testName, func(t *testing.T) { |
| 1670 | // Init the ring. |
| 1671 | ringDesc := &Desc{Ingesters: testData.ringInstances} |
| 1672 | for id, instance := range ringDesc.Ingesters { |
nothing calls this directly
no test coverage detected