1// Copyright (c) 2020 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 21package atomic 22 23import ( 24 "encoding/json" 25 "testing" 26 "time" 27 28 "github.com/stretchr/testify/assert" 29 "github.com/stretchr/testify/require" 30) 31 32func TestDuration(t *testing.T) { 33 atom := NewDuration(5 * time.Minute) 34 35 require.Equal(t, 5*time.Minute, atom.Load(), "Load didn't work.") 36 require.Equal(t, 6*time.Minute, atom.Add(time.Minute), "Add didn't work.") 37 require.Equal(t, 4*time.Minute, atom.Sub(2*time.Minute), "Sub didn't work.") 38 39 require.True(t, atom.CAS(4*time.Minute, time.Minute), "CAS didn't report a swap.") 40 require.Equal(t, time.Minute, atom.Load(), "CAS didn't set the correct value.") 41 42 require.Equal(t, time.Minute, atom.Swap(2*time.Minute), "Swap didn't return the old value.") 43 require.Equal(t, 2*time.Minute, atom.Load(), "Swap didn't set the correct value.") 44 45 atom.Store(10 * time.Minute) 46 require.Equal(t, 10*time.Minute, atom.Load(), "Store didn't set the correct value.") 47 48 t.Run("JSON/Marshal", func(t *testing.T) { 49 atom.Store(time.Second) 50 bytes, err := json.Marshal(atom) 51 require.NoError(t, err, "json.Marshal errored unexpectedly.") 52 require.Equal(t, []byte("1000000000"), bytes, "json.Marshal encoded the wrong bytes.") 53 }) 54 55 t.Run("JSON/Unmarshal", func(t *testing.T) { 56 err := json.Unmarshal([]byte("1000000000"), &atom) 57 require.NoError(t, err, "json.Unmarshal errored unexpectedly.") 58 require.Equal(t, time.Second, atom.Load(), "json.Unmarshal didn't set the correct value.") 59 }) 60 61 t.Run("JSON/Unmarshal/Error", func(t *testing.T) { 62 err := json.Unmarshal([]byte("\"1000000000\""), &atom) 63 require.Error(t, err, "json.Unmarshal didn't error as expected.") 64 assertErrorJSONUnmarshalType(t, err, 65 "json.Unmarshal failed with unexpected error %v, want UnmarshalTypeError.", err) 66 }) 67 68 t.Run("String", func(t *testing.T) { 69 assert.Equal(t, "42s", NewDuration(42*time.Second).String(), 70 "String() returned an unexpected value.") 71 }) 72} 73