github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/msg/producer/config/writer_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 config
    22  
    23  import (
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/cluster/client"
    28  	"github.com/m3db/m3/src/cluster/kv"
    29  	"github.com/m3db/m3/src/cluster/services"
    30  	"github.com/m3db/m3/src/x/instrument"
    31  	xio "github.com/m3db/m3/src/x/io"
    32  
    33  	"github.com/golang/mock/gomock"
    34  	"github.com/stretchr/testify/require"
    35  	yaml "gopkg.in/yaml.v2"
    36  )
    37  
    38  func TestConnectionConfiguration(t *testing.T) {
    39  	str := `
    40  dialTimeout: 3s
    41  writeTimeout: 2s
    42  keepAlivePeriod: 20s
    43  resetDelay: 1s
    44  retry:
    45    initialBackoff: 1ms
    46    maxBackoff: 2ms
    47  flushInterval: 2s
    48  writeBufferSize: 100
    49  readBufferSize: 200
    50  `
    51  
    52  	var cfg ConnectionConfiguration
    53  	require.NoError(t, yaml.Unmarshal([]byte(str), &cfg))
    54  
    55  	cOpts := cfg.NewOptions(instrument.NewOptions())
    56  	require.Equal(t, 3*time.Second, cOpts.DialTimeout())
    57  	require.Equal(t, 2*time.Second, cOpts.WriteTimeout())
    58  	require.Equal(t, 20*time.Second, cOpts.KeepAlivePeriod())
    59  	require.Equal(t, time.Second, cOpts.ResetDelay())
    60  	require.Equal(t, time.Millisecond, cOpts.RetryOptions().InitialBackoff())
    61  	require.Equal(t, 2*time.Millisecond, cOpts.RetryOptions().MaxBackoff())
    62  	require.Equal(t, 2*time.Second, cOpts.FlushInterval())
    63  	require.Equal(t, 100, cOpts.WriteBufferSize())
    64  	require.Equal(t, 200, cOpts.ReadBufferSize())
    65  }
    66  
    67  func TestWriterConfiguration(t *testing.T) {
    68  	str := `
    69  topicName: testTopic
    70  topicServiceOverride:
    71    zone: z1
    72    namespace: n1
    73  topicWatchInitTimeout: 1s
    74  placementServiceOverride:
    75    namespaces:
    76      placement: n2
    77  placementWatchInitTimeout: 2s
    78  messageRetry:
    79    initialBackoff: 1ms
    80  messageQueueNewWritesScanInterval: 200ms
    81  messageQueueFullScanInterval: 10s
    82  messageQueueScanBatchSize: 1024
    83  initialAckMapSize: 1024
    84  closeCheckInterval: 2s
    85  ackErrorRetry:
    86    initialBackoff: 2ms
    87  connection:
    88    dialTimeout: 5s
    89  encoder:
    90    maxMessageSize: 100
    91  decoder:
    92    maxMessageSize: 200
    93  `
    94  	var cfg WriterConfiguration
    95  	require.NoError(t, yaml.Unmarshal([]byte(str), &cfg))
    96  
    97  	ctrl := gomock.NewController(t)
    98  	defer ctrl.Finish()
    99  
   100  	cs := client.NewMockClient(ctrl)
   101  	cs.EXPECT().Store(kv.NewOverrideOptions().SetZone("z1").SetNamespace("n1")).Return(nil, nil)
   102  	cs.EXPECT().Services(
   103  		services.NewOverrideOptions().SetNamespaceOptions(
   104  			services.NewNamespaceOptions().SetPlacementNamespace("n2"),
   105  		),
   106  	).Return(nil, nil)
   107  
   108  	wOpts, err := cfg.NewOptions(cs, instrument.NewOptions(), xio.NewOptions())
   109  	require.NoError(t, err)
   110  	require.Equal(t, "testTopic", wOpts.TopicName())
   111  	require.Equal(t, time.Second, wOpts.TopicWatchInitTimeout())
   112  	require.Equal(t, 2*time.Second, wOpts.PlacementWatchInitTimeout())
   113  	require.NotNil(t, wOpts.MessageRetryNanosFn())
   114  	require.Equal(t, 200*time.Millisecond, wOpts.MessageQueueNewWritesScanInterval())
   115  	require.Equal(t, 10*time.Second, wOpts.MessageQueueFullScanInterval())
   116  	require.Equal(t, 1024, wOpts.MessageQueueScanBatchSize())
   117  	require.Equal(t, 1024, wOpts.InitialAckMapSize())
   118  	require.Equal(t, 2*time.Second, wOpts.CloseCheckInterval())
   119  	require.Equal(t, 2*time.Millisecond, wOpts.AckErrorRetryOptions().InitialBackoff())
   120  	require.Equal(t, 5*time.Second, wOpts.ConnectionOptions().DialTimeout())
   121  	require.Equal(t, 100, wOpts.EncoderOptions().MaxMessageSize())
   122  	require.Equal(t, 200, wOpts.DecoderOptions().MaxMessageSize())
   123  }