atomic_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. package atomic
  21. import (
  22. "testing"
  23. "time"
  24. "github.com/stretchr/testify/assert"
  25. "github.com/stretchr/testify/require"
  26. )
  27. func TestInt32(t *testing.T) {
  28. atom := NewInt32(42)
  29. require.Equal(t, int32(42), atom.Load(), "Load didn't work.")
  30. require.Equal(t, int32(46), atom.Add(4), "Add didn't work.")
  31. require.Equal(t, int32(44), atom.Sub(2), "Sub didn't work.")
  32. require.Equal(t, int32(45), atom.Inc(), "Inc didn't work.")
  33. require.Equal(t, int32(44), atom.Dec(), "Dec didn't work.")
  34. require.True(t, atom.CAS(44, 0), "CAS didn't report a swap.")
  35. require.Equal(t, int32(0), atom.Load(), "CAS didn't set the correct value.")
  36. require.Equal(t, int32(0), atom.Swap(1), "Swap didn't return the old value.")
  37. require.Equal(t, int32(1), atom.Load(), "Swap didn't set the correct value.")
  38. atom.Store(42)
  39. require.Equal(t, int32(42), atom.Load(), "Store didn't set the correct value.")
  40. }
  41. func TestInt64(t *testing.T) {
  42. atom := NewInt64(42)
  43. require.Equal(t, int64(42), atom.Load(), "Load didn't work.")
  44. require.Equal(t, int64(46), atom.Add(4), "Add didn't work.")
  45. require.Equal(t, int64(44), atom.Sub(2), "Sub didn't work.")
  46. require.Equal(t, int64(45), atom.Inc(), "Inc didn't work.")
  47. require.Equal(t, int64(44), atom.Dec(), "Dec didn't work.")
  48. require.True(t, atom.CAS(44, 0), "CAS didn't report a swap.")
  49. require.Equal(t, int64(0), atom.Load(), "CAS didn't set the correct value.")
  50. require.Equal(t, int64(0), atom.Swap(1), "Swap didn't return the old value.")
  51. require.Equal(t, int64(1), atom.Load(), "Swap didn't set the correct value.")
  52. atom.Store(42)
  53. require.Equal(t, int64(42), atom.Load(), "Store didn't set the correct value.")
  54. }
  55. func TestUint32(t *testing.T) {
  56. atom := NewUint32(42)
  57. require.Equal(t, uint32(42), atom.Load(), "Load didn't work.")
  58. require.Equal(t, uint32(46), atom.Add(4), "Add didn't work.")
  59. require.Equal(t, uint32(44), atom.Sub(2), "Sub didn't work.")
  60. require.Equal(t, uint32(45), atom.Inc(), "Inc didn't work.")
  61. require.Equal(t, uint32(44), atom.Dec(), "Dec didn't work.")
  62. require.True(t, atom.CAS(44, 0), "CAS didn't report a swap.")
  63. require.Equal(t, uint32(0), atom.Load(), "CAS didn't set the correct value.")
  64. require.Equal(t, uint32(0), atom.Swap(1), "Swap didn't return the old value.")
  65. require.Equal(t, uint32(1), atom.Load(), "Swap didn't set the correct value.")
  66. atom.Store(42)
  67. require.Equal(t, uint32(42), atom.Load(), "Store didn't set the correct value.")
  68. }
  69. func TestUint64(t *testing.T) {
  70. atom := NewUint64(42)
  71. require.Equal(t, uint64(42), atom.Load(), "Load didn't work.")
  72. require.Equal(t, uint64(46), atom.Add(4), "Add didn't work.")
  73. require.Equal(t, uint64(44), atom.Sub(2), "Sub didn't work.")
  74. require.Equal(t, uint64(45), atom.Inc(), "Inc didn't work.")
  75. require.Equal(t, uint64(44), atom.Dec(), "Dec didn't work.")
  76. require.True(t, atom.CAS(44, 0), "CAS didn't report a swap.")
  77. require.Equal(t, uint64(0), atom.Load(), "CAS didn't set the correct value.")
  78. require.Equal(t, uint64(0), atom.Swap(1), "Swap didn't return the old value.")
  79. require.Equal(t, uint64(1), atom.Load(), "Swap didn't set the correct value.")
  80. atom.Store(42)
  81. require.Equal(t, uint64(42), atom.Load(), "Store didn't set the correct value.")
  82. }
  83. func TestBool(t *testing.T) {
  84. atom := NewBool(false)
  85. require.False(t, atom.Toggle(), "Expected swap to return previous value.")
  86. require.True(t, atom.Load(), "Unexpected state after swap.")
  87. require.True(t, atom.CAS(true, true), "CAS should swap when old matches")
  88. require.True(t, atom.Load(), "CAS should have no effect")
  89. require.True(t, atom.CAS(true, false), "CAS should swap when old matches")
  90. require.False(t, atom.Load(), "CAS should have modified the value")
  91. require.False(t, atom.CAS(true, false), "CAS should fail on old mismatch")
  92. require.False(t, atom.Load(), "CAS should not have modified the value")
  93. atom.Store(false)
  94. require.False(t, atom.Load(), "Unexpected state after store.")
  95. prev := atom.Swap(false)
  96. require.False(t, prev, "Expected Swap to return previous value.")
  97. prev = atom.Swap(true)
  98. require.False(t, prev, "Expected Swap to return previous value.")
  99. }
  100. func TestFloat64(t *testing.T) {
  101. atom := NewFloat64(4.2)
  102. require.Equal(t, float64(4.2), atom.Load(), "Load didn't work.")
  103. require.True(t, atom.CAS(4.2, 0.5), "CAS didn't report a swap.")
  104. require.Equal(t, float64(0.5), atom.Load(), "CAS didn't set the correct value.")
  105. require.False(t, atom.CAS(0.0, 1.5), "CAS reported a swap.")
  106. atom.Store(42.0)
  107. require.Equal(t, float64(42.0), atom.Load(), "Store didn't set the correct value.")
  108. require.Equal(t, float64(42.5), atom.Add(0.5), "Add didn't work.")
  109. require.Equal(t, float64(42.0), atom.Sub(0.5), "Sub didn't work.")
  110. }
  111. func TestDuration(t *testing.T) {
  112. atom := NewDuration(5 * time.Minute)
  113. require.Equal(t, 5*time.Minute, atom.Load(), "Load didn't work.")
  114. require.Equal(t, 6*time.Minute, atom.Add(time.Minute), "Add didn't work.")
  115. require.Equal(t, 4*time.Minute, atom.Sub(2*time.Minute), "Sub didn't work.")
  116. require.True(t, atom.CAS(4*time.Minute, time.Minute), "CAS didn't report a swap.")
  117. require.Equal(t, time.Minute, atom.Load(), "CAS didn't set the correct value.")
  118. require.Equal(t, time.Minute, atom.Swap(2*time.Minute), "Swap didn't return the old value.")
  119. require.Equal(t, 2*time.Minute, atom.Load(), "Swap didn't set the correct value.")
  120. atom.Store(10 * time.Minute)
  121. require.Equal(t, 10*time.Minute, atom.Load(), "Store didn't set the correct value.")
  122. }
  123. func TestValue(t *testing.T) {
  124. var v Value
  125. assert.Nil(t, v.Load(), "initial Value is not nil")
  126. v.Store(42)
  127. assert.Equal(t, 42, v.Load())
  128. v.Store(84)
  129. assert.Equal(t, 84, v.Load())
  130. assert.Panics(t, func() { v.Store("foo") })
  131. }