Skip to content

Commit

Permalink
Shadow atomic.Value from sync/atomic (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
billf authored Apr 11, 2017
1 parent 1dcf4eb commit 4e33664
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions atomic.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,7 @@ func (f *Float64) Sub(s float64) float64 {
func (f *Float64) CAS(old, new float64) bool {
return atomic.CompareAndSwapUint64(&f.v, math.Float64bits(old), math.Float64bits(new))
}

// Value shadows the type of the same name from sync/atomic
// https://godoc.org/sync/atomic#Value
type Value struct{ atomic.Value }
14 changes: 14 additions & 0 deletions atomic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package atomic
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -131,3 +132,16 @@ func TestFloat64(t *testing.T) {
require.Equal(t, float64(42.5), atom.Add(0.5), "Add didn't work.")
require.Equal(t, float64(42.0), atom.Sub(0.5), "Sub didn't work.")
}

func TestValue(t *testing.T) {
var v Value
assert.Nil(t, v.Load(), "initial Value is not nil")

v.Store(42)
assert.Equal(t, 42, v.Load())

v.Store(84)
assert.Equal(t, 84, v.Load())

assert.Panics(t, func() { v.Store("foo") })
}

0 comments on commit 4e33664

Please sign in to comment.