...

Source file src/go.uber.org/atomic/error_test.go

Documentation: go.uber.org/atomic

     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 atomic
    22  
    23  import (
    24  	"errors"
    25  	"testing"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestErrorByValue(t *testing.T) {
    32  	err := &Error{}
    33  	require.Nil(t, err.Load(), "Initial value shall be nil")
    34  }
    35  
    36  func TestNewErrorWithNilArgument(t *testing.T) {
    37  	err := NewError(nil)
    38  	require.Nil(t, err.Load(), "Initial value shall be nil")
    39  }
    40  
    41  func TestErrorCanStoreNil(t *testing.T) {
    42  	err := NewError(errors.New("hello"))
    43  	err.Store(nil)
    44  	require.Nil(t, err.Load(), "Stored value shall be nil")
    45  }
    46  
    47  func TestNewErrorWithError(t *testing.T) {
    48  	err1 := errors.New("hello1")
    49  	err2 := errors.New("hello2")
    50  
    51  	atom := NewError(err1)
    52  	require.Equal(t, err1, atom.Load(), "Expected Load to return initialized value")
    53  
    54  	atom.Store(err2)
    55  	require.Equal(t, err2, atom.Load(), "Expected Load to return overridden value")
    56  }
    57  
    58  func TestErrorSwap(t *testing.T) {
    59  	err1 := errors.New("hello1")
    60  	err2 := errors.New("hello2")
    61  
    62  	atom := NewError(err1)
    63  	require.Equal(t, err1, atom.Load(), "Expected Load to return initialized value")
    64  
    65  	old := atom.Swap(err2)
    66  	require.Equal(t, err2, atom.Load(), "Expected Load to return overridden value")
    67  	require.Equal(t, err1, old, "Expected old to be initial value")
    68  }
    69  
    70  func TestErrorCompareAndSwap(t *testing.T) {
    71  	err1 := errors.New("hello1")
    72  	err2 := errors.New("hello2")
    73  
    74  	atom := NewError(err1)
    75  	require.Equal(t, err1, atom.Load(), "Expected Load to return initialized value")
    76  
    77  	swapped := atom.CompareAndSwap(err2, err2)
    78  	require.False(t, swapped, "Expected swapped to be false")
    79  	require.Equal(t, err1, atom.Load(), "Expected Load to return initial value")
    80  
    81  	swapped = atom.CompareAndSwap(err1, err2)
    82  	require.True(t, swapped, "Expected swapped to be true")
    83  	require.Equal(t, err2, atom.Load(), "Expected Load to return overridden value")
    84  }
    85  
    86  func TestError_InitializeDefaults(t *testing.T) {
    87  	tests := []struct {
    88  		msg      string
    89  		newError func() *Error
    90  	}{
    91  		{
    92  			msg: "Uninitialized",
    93  			newError: func() *Error {
    94  				var e Error
    95  				return &e
    96  			},
    97  		},
    98  		{
    99  			msg: "NewError with default",
   100  			newError: func() *Error {
   101  				return NewError(nil)
   102  			},
   103  		},
   104  		{
   105  			msg: "Error swapped with default",
   106  			newError: func() *Error {
   107  				e := NewError(assert.AnError)
   108  				e.Swap(nil)
   109  				return e
   110  			},
   111  		},
   112  		{
   113  			msg: "Error CAS'd with default",
   114  			newError: func() *Error {
   115  				e := NewError(assert.AnError)
   116  				e.CompareAndSwap(assert.AnError, nil)
   117  				return e
   118  			},
   119  		},
   120  	}
   121  
   122  	for _, tt := range tests {
   123  		t.Run(tt.msg, func(t *testing.T) {
   124  			t.Run("CompareAndSwap", func(t *testing.T) {
   125  				e := tt.newError()
   126  				require.True(t, e.CompareAndSwap(nil, assert.AnError))
   127  				assert.Equal(t, assert.AnError, e.Load())
   128  			})
   129  
   130  			t.Run("Swap", func(t *testing.T) {
   131  				e := tt.newError()
   132  				assert.Equal(t, nil, e.Swap(assert.AnError))
   133  			})
   134  		})
   135  	}
   136  }
   137  

View as plain text