github.com/m3db/m3@v1.5.0/src/cluster/placement/options_test.go (about) 1 // Copyright (c) 2016 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 placement 22 23 import ( 24 "testing" 25 "time" 26 27 "github.com/m3db/m3/src/cluster/shard" 28 "github.com/m3db/m3/src/x/instrument" 29 30 "github.com/stretchr/testify/assert" 31 ) 32 33 func TestDeploymentOptions(t *testing.T) { 34 dopts := NewDeploymentOptions() 35 assert.Equal(t, defaultMaxStepSize, dopts.MaxStepSize()) 36 dopts = dopts.SetMaxStepSize(5) 37 assert.Equal(t, 5, dopts.MaxStepSize()) 38 } 39 40 func TestPlacementOptions(t *testing.T) { 41 t.Run("defaults", func(t *testing.T) { 42 o := NewOptions() 43 assert.True(t, o.AllowPartialReplace()) 44 assert.False(t, o.AddAllCandidates()) 45 assert.True(t, o.IsSharded()) 46 assert.Equal(t, IncludeTransitionalShardStates, o.ShardStateMode()) 47 assert.False(t, o.Dryrun()) 48 assert.False(t, o.IsMirrored()) 49 assert.False(t, o.IsStaged()) 50 assert.False(t, o.Compress()) 51 assert.NotNil(t, o.InstrumentOptions()) 52 assert.Equal(t, int64(0), o.PlacementCutoverNanosFn()()) 53 assert.Equal(t, int64(0), o.ShardCutoffNanosFn()()) 54 assert.Equal(t, int64(0), o.ShardCutoffNanosFn()()) 55 assert.Nil(t, o.InstanceSelector()) 56 }) 57 58 t.Run("setters", func(t *testing.T) { 59 o := NewOptions() 60 o = o.SetAllowPartialReplace(false) 61 assert.False(t, o.AllowPartialReplace()) 62 63 o = o.SetAddAllCandidates(true) 64 assert.True(t, o.AddAllCandidates()) 65 66 o = o.SetIsSharded(false) 67 assert.False(t, o.IsSharded()) 68 69 o = o.SetShardStateMode(StableShardStateOnly) 70 assert.Equal(t, StableShardStateOnly, o.ShardStateMode()) 71 72 o = o.SetDryrun(true) 73 assert.True(t, o.Dryrun()) 74 75 o = o.SetIsMirrored(true) 76 assert.True(t, o.IsMirrored()) 77 78 o = o.SetIsStaged(true) 79 assert.True(t, o.IsStaged()) 80 81 o = o.SetCompress(true) 82 assert.True(t, o.Compress()) 83 84 iopts := instrument.NewOptions(). 85 SetTimerOptions(instrument.TimerOptions{StandardSampleRate: 0.5}) 86 o = o.SetInstrumentOptions(iopts) 87 assert.Equal(t, iopts, o.InstrumentOptions()) 88 89 o = o.SetPlacementCutoverNanosFn(func() int64 { 90 return int64(10) 91 }) 92 assert.Equal(t, int64(10), o.PlacementCutoverNanosFn()()) 93 94 o = o.SetShardCutoverNanosFn(func() int64 { 95 return int64(20) 96 }) 97 assert.Equal(t, int64(20), o.ShardCutoverNanosFn()()) 98 99 o = o.SetShardCutoffNanosFn(func() int64 { 100 return int64(30) 101 }) 102 assert.Equal(t, int64(30), o.ShardCutoffNanosFn()()) 103 104 now := time.Unix(0, 0) 105 o = o.SetNowFn(func() time.Time { 106 return now 107 }) 108 assert.Equal(t, now, o.NowFn()()) 109 110 o = o.SetIsShardCutoverFn(func(s shard.Shard) error { 111 return nil 112 }) 113 assert.Nil(t, o.IsShardCutoverFn()(nil)) 114 115 o = o.SetIsShardCutoffFn(func(s shard.Shard) error { 116 return nil 117 }) 118 assert.Nil(t, o.IsShardCutoffFn()(nil)) 119 120 o = o.SetInstanceSelector(NewMockInstanceSelector(nil)) 121 assert.NotNil(t, o.InstanceSelector()) 122 }) 123 }