You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: ring_test.go
    path of file: ./repos/pyroscope/pkg/util/servicediscovery
    
    the code of the file until where you have to start completion: // SPDX-License-Identifier: AGPL-3.0-only

package servicediscovery

import (
	"context"
	"sort"
	"sync"
	"testing"
	"time"

	"github.com/go-kit/log"
	"github.com/grafana/dskit/kv/consul"
	"github.com/grafana/dskit/ring"
	"github.com/grafana/dskit/services"
	"github.com/grafana/dskit/test"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestRingServiceDiscovery_WithoutMaxUsedInstances(t *testing.T) {
	const (
		ringKey         = "test"
		ringCheckPeriod = 100 * time.Millisecond // Check very frequently to speed up the test.
	)

	// Use an in-memory KV store.
	inmem, closer := consul.NewInMemoryClient(ring.GetCodec(), log.NewNopLogger(), nil)
	t.Cleanup(func() { _ = closer.Close() })

	// Create a ring client.
	ringCfg := ring.Config{HeartbeatTimeout: time.Minute, ReplicationFactor: 1}
	ringClient, err := ring.NewWithStoreClientAndStrategy(ringCfg, "test", ringKey, inmem, ring.NewDefaultReplicationStrategy(), nil, log.NewNopLogger())
	require.NoError(t, err)

	// Create an empty ring.
	ctx := context.Background()
	require.NoError(t, inmem.CAS(ctx, ringKey, func(in interface{}) (out interface{}, retry bool, err error) {
		return ring.NewDesc(), true, nil
	}))

	// Mock a receiver to keep track of all notified addresses.
	receiver := newNotificationsReceiverMock()

	sd := NewRing(ringClient, ringCheckPeriod, 0, receiver)

	// Start the service discovery.
	require.NoError(t, services.StartAndAwaitRunning(ctx, sd))
	t.Cleanup(func() {
		require.NoError(t, services.
    
func TestSelectInUseInstances(t *testing.T) {
	tests := map[string]struct {
		input        []ring.InstanceDesc
		maxInstances int
		expected     []ring.InstanceDesc
	}{
		"should return the input on empty list of instances": {
			input:        nil,
			maxInstances: 3,
			expected:     nil,
		},
		"should return the input on a number of instances < max instances": {
			input:        []ring.InstanceDesc{{Addr: "1.1.1.1"}, {Addr: "3.3.3.3"}},
			maxInstances: 3,
			expected:     []ring.InstanceDesc{{Addr: "1.1.1.1"}, {Addr: "3.3.3.3"}},
		},
		"should return the input on a number of instances = max instances": {
			input:        []ring.InstanceDesc{{Addr: "1.1.1.1"}, {Addr: "3.3.3.3"}, {Addr: "2.2.2.2"}},
			maxInstances: 3,
			expected:     []ring.InstanceDesc{{Addr: "1.1.1.1"}, {Addr: "3.3.3.3"}, {Addr: "2.2.2.2"}},
		},
		"should return a subset of the input on a number of instances > max instances": {
			input:        []ring.InstanceDesc{{Addr: "1.1.1.1"}, {Addr: "3.3.3.3"}, {Addr: "2.2.2.2"}, {Addr: "4.4.4.4"}, {Addr: "5.5.5.5"}},
			maxInstances: 3,
			expected:     []ring.InstanceDesc{{Addr: "1.1.1.1"}, {Addr: "2.2.2.2"}, {Addr: "3.3.3.3"}},
		},
		"should return the input if max instances is 0": {
			input:        []ring.InstanceDesc{{Addr: "1.1.1.1"}, {Addr: "3.3.3.3"}, {Addr: "2.2.2.2"}},
			maxInstances: 0,
			expected:     []ring.InstanceDesc{{Addr: "1.1.1.1"}, {Addr: "3.3.3.3"}, {Addr: "2.2.2.2"}},
		},
	}

	for testName, testData := range tests {
		t.Run(testName, func(t *testing.T) {
			assert.Equal(t, testData.expected, selectInUseInstances(testData.input, testData.maxInstances))
		})
	}
}