github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/aggregator/integration/topic.go (about)

     1  // Copyright (c) 2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package integration
    22  
    23  import (
    24  	"testing"
    25  	"time"
    26  
    27  	clusterclient "github.com/m3db/m3/src/cluster/client"
    28  	"github.com/m3db/m3/src/cluster/placement"
    29  	"github.com/m3db/m3/src/cluster/services"
    30  	"github.com/m3db/m3/src/msg/topic"
    31  
    32  	"github.com/stretchr/testify/require"
    33  )
    34  
    35  func initializeTopic(
    36  	topicName string, //nolint:unparam
    37  	clusterClient clusterclient.Client,
    38  	numShards int,
    39  ) (topic.Service, error) {
    40  	serviceID := services.NewServiceID().SetName(defaultServiceName)
    41  	cs := topic.NewConsumerService().
    42  		SetServiceID(serviceID).
    43  		SetConsumptionType(topic.Replicated).
    44  		SetMessageTTLNanos(time.Minute.Nanoseconds())
    45  	ingestTopic := topic.NewTopic().
    46  		SetName(topicName).
    47  		SetNumberOfShards(uint32(numShards)).
    48  		SetConsumerServices([]topic.ConsumerService{cs})
    49  
    50  	topicServiceOpts := topic.NewServiceOptions().
    51  		SetConfigService(clusterClient)
    52  	topicService, err := topic.NewService(topicServiceOpts)
    53  	if err != nil {
    54  		return topicService, err
    55  	}
    56  
    57  	_, err = topicService.CheckAndSet(ingestTopic, 0)
    58  
    59  	return topicService, err
    60  }
    61  
    62  func removeAllTopicConsumers(
    63  	topicService topic.Service,
    64  	topicName string, //nolint:unparam
    65  ) error {
    66  	topic, err := topicService.Get(topicName)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	for len(topic.ConsumerServices()) > 0 {
    71  		topic, err = topic.RemoveConsumerService(topic.ConsumerServices()[0].ServiceID())
    72  		if err != nil {
    73  			return err
    74  		}
    75  	}
    76  	_, err = topicService.CheckAndSet(topic, topic.Version())
    77  	return err
    78  }
    79  
    80  func setupTopic(t *testing.T, serverOpts testServerOptions, placement placement.Placement) testServerOptions {
    81  	topicService, err := initializeTopic(defaultTopicName, serverOpts.ClusterClient(), placement.NumShards())
    82  	require.NoError(t, err)
    83  	return serverOpts.
    84  		SetTopicService(topicService).
    85  		SetTopicName(defaultTopicName)
    86  }