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

     1  // Copyright (c) 2018 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 topic
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/m3db/m3/src/cluster/client"
    27  	"github.com/m3db/m3/src/cluster/kv"
    28  	"github.com/m3db/m3/src/cluster/kv/mem"
    29  	"github.com/m3db/m3/src/cluster/services"
    30  	"github.com/m3db/m3/src/msg/generated/proto/msgpb"
    31  
    32  	"github.com/golang/mock/gomock"
    33  	"github.com/stretchr/testify/require"
    34  )
    35  
    36  func TestTopicService(t *testing.T) {
    37  	ctrl := gomock.NewController(t)
    38  	defer ctrl.Finish()
    39  
    40  	kvOpts := kv.NewOverrideOptions().SetNamespace("foo")
    41  	cs := client.NewMockClient(ctrl)
    42  	store := mem.NewStore()
    43  	cs.EXPECT().Store(kvOpts).Return(store, nil)
    44  
    45  	s, err := NewService(NewServiceOptions().SetConfigService(cs).SetKVOverrideOptions(kvOpts))
    46  	require.NoError(t, err)
    47  
    48  	topicName := "topic1"
    49  	_, err = s.Get(topicName)
    50  	require.Error(t, err)
    51  
    52  	_, err = s.CheckAndSet(NewTopic(), kv.UninitializedVersion)
    53  	require.Error(t, err)
    54  	require.Contains(t, err.Error(), "invalid topic")
    55  
    56  	w, err := s.Watch(topicName)
    57  	require.NoError(t, err)
    58  	require.Equal(t, 0, len(w.C()))
    59  
    60  	topic1 := NewTopic().
    61  		SetName(topicName).
    62  		SetNumberOfShards(100).
    63  		SetConsumerServices([]ConsumerService{
    64  			NewConsumerService().SetConsumptionType(Shared).SetServiceID(services.NewServiceID().SetName("s1")),
    65  			NewConsumerService().SetConsumptionType(Replicated).SetServiceID(services.NewServiceID().SetName("s2")),
    66  		})
    67  	topic1, err = s.CheckAndSet(topic1, kv.UninitializedVersion)
    68  	require.NoError(t, err)
    69  	topic1, err = s.CheckAndSet(topic1, 1)
    70  	require.NoError(t, err)
    71  
    72  	topic2, err := s.Get(topicName)
    73  	require.NoError(t, err)
    74  	require.Equal(t, 2, topic2.Version())
    75  	require.Equal(t, topic1.Name(), topic2.Name())
    76  	require.Equal(t, topic1.NumberOfShards(), topic2.NumberOfShards())
    77  	require.Equal(t, topic1.ConsumerServices(), topic2.ConsumerServices())
    78  
    79  	<-w.C()
    80  	topic, err := w.Get()
    81  	require.NoError(t, err)
    82  	require.Equal(t, topic2, topic)
    83  
    84  	err = s.Delete(topicName)
    85  	require.NoError(t, err)
    86  
    87  	<-w.C()
    88  	_, err = w.Get()
    89  	require.Error(t, err)
    90  
    91  	_, err = s.CheckAndSet(topic1, kv.UninitializedVersion)
    92  	require.NoError(t, err)
    93  
    94  	<-w.C()
    95  	topic, err = w.Get()
    96  	require.NoError(t, err)
    97  
    98  	topic3, err := s.Get(topicName)
    99  	require.NoError(t, err)
   100  	require.Equal(t, 1, topic3.Version())
   101  	require.Equal(t, topic3, topic)
   102  
   103  	version, err := store.Set(key(topicName), &msgpb.Message{Value: []byte("bad proto")})
   104  	require.NoError(t, err)
   105  	require.Equal(t, 2, version)
   106  
   107  	_, err = s.Get(topicName)
   108  	require.Error(t, err)
   109  
   110  	<-w.C()
   111  	_, err = w.Get()
   112  	require.Error(t, err)
   113  
   114  	w.Close()
   115  }