github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/nomad/structs/config/limits_test.go (about) 1 package config 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/hashicorp/nomad/helper" 8 "github.com/stretchr/testify/require" 9 ) 10 11 // TestLimits_Defaults asserts the default limits are valid. 12 func TestLimits_Defaults(t *testing.T) { 13 t.Parallel() 14 15 l := DefaultLimits() 16 d, err := time.ParseDuration(l.HTTPSHandshakeTimeout) 17 require.NoError(t, err) 18 require.True(t, d > 0) 19 20 d, err = time.ParseDuration(l.RPCHandshakeTimeout) 21 require.NoError(t, err) 22 require.True(t, d > 0) 23 } 24 25 // TestLimits_Copy asserts Limits structs are deep copied. 26 func TestLimits_Copy(t *testing.T) { 27 t.Parallel() 28 29 o := DefaultLimits() 30 c := o.Copy() 31 32 // Assert changes to copy are not propagated to the original 33 c.HTTPSHandshakeTimeout = "1s" 34 c.HTTPMaxConnsPerClient = helper.IntToPtr(50) 35 c.RPCHandshakeTimeout = "1s" 36 c.RPCMaxConnsPerClient = helper.IntToPtr(50) 37 38 require.NotEqual(t, c.HTTPSHandshakeTimeout, o.HTTPSHandshakeTimeout) 39 40 // Pointers should be different 41 require.True(t, c.HTTPMaxConnsPerClient != o.HTTPMaxConnsPerClient) 42 43 require.NotEqual(t, c.HTTPMaxConnsPerClient, o.HTTPMaxConnsPerClient) 44 require.NotEqual(t, c.RPCHandshakeTimeout, o.RPCHandshakeTimeout) 45 46 // Pointers should be different 47 require.True(t, c.RPCMaxConnsPerClient != o.RPCMaxConnsPerClient) 48 49 require.NotEqual(t, c.RPCMaxConnsPerClient, o.RPCMaxConnsPerClient) 50 } 51 52 // TestLimits_Merge asserts non-zero fields from the method argument take 53 // precedence over the existing limits. 54 func TestLimits_Merge(t *testing.T) { 55 t.Parallel() 56 57 l := Limits{} 58 o := DefaultLimits() 59 m := l.Merge(o) 60 61 // Operands should not change 62 require.Equal(t, Limits{}, l) 63 require.Equal(t, DefaultLimits(), o) 64 65 // m == o 66 require.Equal(t, m, DefaultLimits()) 67 68 o.HTTPSHandshakeTimeout = "10s" 69 m2 := m.Merge(o) 70 71 // Operands should not change 72 require.Equal(t, m, DefaultLimits()) 73 74 // Use short struct initialization style so it fails to compile if 75 // fields are added 76 expected := Limits{"10s", helper.IntToPtr(100), "5s", helper.IntToPtr(100)} 77 require.Equal(t, expected, m2) 78 79 // Mergin in 0 values should not change anything 80 m3 := m2.Merge(Limits{}) 81 require.Equal(t, m2, m3) 82 }