mirror of
https://github.com/pomerium/pomerium.git
synced 2025-08-03 08:50:42 +02:00
atomicutil: use atomicutil.Value wherever possible (#3517)
* atomicutil: use atomicutil.Value wherever possible * fix test * fix mux router
This commit is contained in:
parent
5c14d2c994
commit
0ac7e45a21
23 changed files with 121 additions and 215 deletions
|
@ -17,7 +17,16 @@ func NewValue[T any](init T) *Value[T] {
|
|||
|
||||
// Load loads the value atomically.
|
||||
func (v *Value[T]) Load() T {
|
||||
return v.value.Load().(T)
|
||||
var def T
|
||||
if v == nil {
|
||||
return def
|
||||
}
|
||||
|
||||
cur := v.value.Load()
|
||||
if cur == nil {
|
||||
return def
|
||||
}
|
||||
return cur.(T)
|
||||
}
|
||||
|
||||
// Store stores the value atomically.
|
||||
|
|
21
internal/atomicutil/value_test.go
Normal file
21
internal/atomicutil/value_test.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
package atomicutil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValue(t *testing.T) {
|
||||
v := NewValue(5)
|
||||
assert.Equal(t, 5, v.Load())
|
||||
|
||||
t.Run("nil", func(t *testing.T) {
|
||||
var v *Value[int]
|
||||
assert.Equal(t, 0, v.Load())
|
||||
})
|
||||
t.Run("default", func(t *testing.T) {
|
||||
var v Value[int]
|
||||
assert.Equal(t, 0, v.Load())
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue