vitess.io/vitess@v0.16.2/go/sync2/atomic_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package sync2 18 19 import ( 20 "testing" 21 "time" 22 23 "gotest.tools/assert" 24 ) 25 26 func TestAtomicInt32(t *testing.T) { 27 i := NewAtomicInt32(1) 28 assert.Equal(t, int32(1), i.Get()) 29 30 i.Set(2) 31 assert.Equal(t, int32(2), i.Get()) 32 33 i.Add(1) 34 assert.Equal(t, int32(3), i.Get()) 35 36 i.CompareAndSwap(3, 4) 37 assert.Equal(t, int32(4), i.Get()) 38 39 i.CompareAndSwap(3, 5) 40 assert.Equal(t, int32(4), i.Get()) 41 } 42 43 func TestAtomicInt64(t *testing.T) { 44 i := NewAtomicInt64(1) 45 assert.Equal(t, int64(1), i.Get()) 46 47 i.Set(2) 48 assert.Equal(t, int64(2), i.Get()) 49 50 i.Add(1) 51 assert.Equal(t, int64(3), i.Get()) 52 53 i.CompareAndSwap(3, 4) 54 assert.Equal(t, int64(4), i.Get()) 55 56 i.CompareAndSwap(3, 5) 57 assert.Equal(t, int64(4), i.Get()) 58 } 59 60 func TestAtomicFloat64(t *testing.T) { 61 i := NewAtomicFloat64(1.0) 62 assert.Equal(t, float64(1.0), i.Get()) 63 64 i.Set(2.0) 65 assert.Equal(t, float64(2.0), i.Get()) 66 { 67 swapped := i.CompareAndSwap(2.0, 4.0) 68 assert.Equal(t, float64(4), i.Get()) 69 assert.Equal(t, true, swapped) 70 } 71 { 72 swapped := i.CompareAndSwap(2.0, 5.0) 73 assert.Equal(t, float64(4), i.Get()) 74 assert.Equal(t, false, swapped) 75 } 76 } 77 78 func TestAtomicDuration(t *testing.T) { 79 d := NewAtomicDuration(time.Second) 80 assert.Equal(t, time.Second, d.Get()) 81 82 d.Set(time.Second * 2) 83 assert.Equal(t, time.Second*2, d.Get()) 84 85 d.Add(time.Second) 86 assert.Equal(t, time.Second*3, d.Get()) 87 88 d.CompareAndSwap(time.Second*3, time.Second*4) 89 assert.Equal(t, time.Second*4, d.Get()) 90 91 d.CompareAndSwap(time.Second*3, time.Second*5) 92 assert.Equal(t, time.Second*4, d.Get()) 93 } 94 95 func TestAtomicString(t *testing.T) { 96 var s AtomicString 97 assert.Equal(t, "", s.Get()) 98 99 s.Set("a") 100 assert.Equal(t, "a", s.Get()) 101 102 assert.Equal(t, false, s.CompareAndSwap("b", "c")) 103 assert.Equal(t, "a", s.Get()) 104 105 assert.Equal(t, true, s.CompareAndSwap("a", "c")) 106 assert.Equal(t, "c", s.Get()) 107 } 108 109 func TestAtomicBool(t *testing.T) { 110 b := NewAtomicBool(true) 111 assert.Equal(t, true, b.Get()) 112 113 b.Set(false) 114 assert.Equal(t, false, b.Get()) 115 116 b.Set(true) 117 assert.Equal(t, true, b.Get()) 118 119 assert.Equal(t, false, b.CompareAndSwap(false, true)) 120 121 assert.Equal(t, true, b.CompareAndSwap(true, false)) 122 123 assert.Equal(t, true, b.CompareAndSwap(false, false)) 124 125 assert.Equal(t, true, b.CompareAndSwap(false, true)) 126 127 assert.Equal(t, true, b.CompareAndSwap(true, true)) 128 }