github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/client/config_test.go (about)

     1  // Copyright (c) 2017 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 client
    22  
    23  import (
    24  	"fmt"
    25  	"io/ioutil"
    26  	"os"
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/m3db/m3/src/dbnode/encoding"
    31  	"github.com/m3db/m3/src/dbnode/topology"
    32  	xconfig "github.com/m3db/m3/src/x/config"
    33  	"github.com/m3db/m3/src/x/retry"
    34  
    35  	"github.com/stretchr/testify/assert"
    36  	"github.com/stretchr/testify/require"
    37  )
    38  
    39  func TestConfiguration(t *testing.T) {
    40  	in := `
    41  writeConsistencyLevel: majority
    42  readConsistencyLevel: unstrict_majority
    43  iterateEqualTimestampStrategy: iterate_lowest_value
    44  connectConsistencyLevel: any
    45  writeTimeout: 10s
    46  fetchTimeout: 15s
    47  connectTimeout: 20s
    48  writeRetry:
    49      initialBackoff: 500ms
    50      backoffFactor: 3
    51      maxRetries: 2
    52      jitter: true
    53  fetchRetry:
    54      initialBackoff: 500ms
    55      backoffFactor: 2
    56      maxRetries: 3
    57      jitter: true
    58  backgroundHealthCheckFailLimit: 4
    59  backgroundHealthCheckFailThrottleFactor: 0.5
    60  hashing:
    61    seed: 42
    62  proto:
    63    enabled: false
    64    schema_registry:
    65      "ns1:2d":
    66        schemaFilePath: "/path/to/schema"
    67        messageName: "ns1_msg_name"
    68      ns2:
    69        schemaDeployID: "deployID-345"
    70        messageName: "ns2_msg_name"
    71  `
    72  
    73  	fd, err := ioutil.TempFile("", "config.yaml")
    74  	require.NoError(t, err)
    75  	defer func() {
    76  		assert.NoError(t, fd.Close())
    77  		assert.NoError(t, os.Remove(fd.Name()))
    78  	}()
    79  
    80  	_, err = fd.Write([]byte(in))
    81  	require.NoError(t, err)
    82  
    83  	var cfg Configuration
    84  	err = xconfig.LoadFile(&cfg, fd.Name(), xconfig.Options{})
    85  	require.NoError(t, err)
    86  
    87  	var (
    88  		levelMajority        = topology.ConsistencyLevelMajority
    89  		readUnstrictMajority = topology.ReadConsistencyLevelUnstrictMajority
    90  		iterateLowestValue   = encoding.IterateLowestValue
    91  		connectAny           = topology.ConnectConsistencyLevelAny
    92  		second10             = 10 * time.Second
    93  		second15             = 15 * time.Second
    94  		second20             = 20 * time.Second
    95  		num4                 = 4
    96  		numHalf              = 0.5
    97  		boolTrue             = true
    98  	)
    99  
   100  	expected := Configuration{
   101  		WriteConsistencyLevel:         &levelMajority,
   102  		ReadConsistencyLevel:          &readUnstrictMajority,
   103  		IterateEqualTimestampStrategy: &iterateLowestValue,
   104  		ConnectConsistencyLevel:       &connectAny,
   105  		WriteTimeout:                  &second10,
   106  		FetchTimeout:                  &second15,
   107  		ConnectTimeout:                &second20,
   108  		WriteRetry: &retry.Configuration{
   109  			InitialBackoff: 500 * time.Millisecond,
   110  			BackoffFactor:  3,
   111  			MaxRetries:     2,
   112  			Jitter:         &boolTrue,
   113  		},
   114  		FetchRetry: &retry.Configuration{
   115  			InitialBackoff: 500 * time.Millisecond,
   116  			BackoffFactor:  2,
   117  			MaxRetries:     3,
   118  			Jitter:         &boolTrue,
   119  		},
   120  		BackgroundHealthCheckFailLimit:          &num4,
   121  		BackgroundHealthCheckFailThrottleFactor: &numHalf,
   122  		HashingConfiguration: &HashingConfiguration{
   123  			Seed: 42,
   124  		},
   125  		Proto: &ProtoConfiguration{
   126  			Enabled: false,
   127  			SchemaRegistry: map[string]NamespaceProtoSchema{
   128  				"ns1:2d": {SchemaFilePath: "/path/to/schema", MessageName: "ns1_msg_name"},
   129  				"ns2":    {SchemaDeployID: "deployID-345", MessageName: "ns2_msg_name"},
   130  			},
   131  		},
   132  	}
   133  
   134  	assert.Equal(t, expected, cfg)
   135  }
   136  
   137  func TestValidateConfig(t *testing.T) {
   138  	var (
   139  		boolTrue = true
   140  	)
   141  
   142  	config := Configuration{
   143  		ShardsLeavingCountTowardsConsistency:                &boolTrue,
   144  		ShardsLeavingAndInitializingCountTowardsConsistency: &boolTrue,
   145  	}
   146  	err := config.Validate()
   147  	require.Error(t, err)
   148  	require.Equal(t, err, fmt.Errorf("m3db client cannot have both shardsLeavingCountTowardsConsistency and "+
   149  		"shardsLeavingAndInitializingCountTowardsConsistency as true"))
   150  }