mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-02-23 00:10:06 -05:00
Use atomic.Bool, added in Go 1.19
Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
This commit is contained in:
parent
256f0e64b3
commit
86906ac536
@ -22,7 +22,7 @@ func TestMain(m *testing.M) {
|
|||||||
func TestNew(t *testing.T) {
|
func TestNew(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
cleaner := New(&spyStopper{})
|
cleaner := New(&spyStopper{stopped: &atomic.Bool{}})
|
||||||
assert.NotNil(cleaner)
|
assert.NotNil(cleaner)
|
||||||
assert.NotEmpty(cleaner.stoppers)
|
assert.NotEmpty(cleaner.stoppers)
|
||||||
}
|
}
|
||||||
@ -30,40 +30,40 @@ func TestNew(t *testing.T) {
|
|||||||
func TestWith(t *testing.T) {
|
func TestWith(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
cleaner := New().With(&spyStopper{})
|
cleaner := New().With(&spyStopper{stopped: &atomic.Bool{}})
|
||||||
assert.NotEmpty(cleaner.stoppers)
|
assert.NotEmpty(cleaner.stoppers)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestClean(t *testing.T) {
|
func TestClean(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
stopper := &spyStopper{}
|
stopper := &spyStopper{stopped: &atomic.Bool{}}
|
||||||
cleaner := New(stopper)
|
cleaner := New(stopper)
|
||||||
go cleaner.Start()
|
go cleaner.Start()
|
||||||
cleaner.Clean()
|
cleaner.Clean()
|
||||||
cleaner.Done()
|
cleaner.Done()
|
||||||
assert.Equal(int64(1), atomic.LoadInt64(&stopper.stopped))
|
assert.True(stopper.stopped.Load())
|
||||||
// call again to make sure it doesn't panic or block or clean up again
|
// call again to make sure it doesn't panic or block or clean up again
|
||||||
cleaner.Clean()
|
cleaner.Clean()
|
||||||
assert.Equal(int64(1), atomic.LoadInt64(&stopper.stopped))
|
assert.True(stopper.stopped.Load())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCleanBeforeStart(t *testing.T) {
|
func TestCleanBeforeStart(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
// calling Clean before Start should work
|
// calling Clean before Start should work
|
||||||
stopper := &spyStopper{}
|
stopper := &spyStopper{stopped: &atomic.Bool{}}
|
||||||
cleaner := New(stopper)
|
cleaner := New(stopper)
|
||||||
cleaner.Clean()
|
cleaner.Clean()
|
||||||
cleaner.Start()
|
cleaner.Start()
|
||||||
cleaner.Done()
|
cleaner.Done()
|
||||||
assert.Equal(int64(1), atomic.LoadInt64(&stopper.stopped))
|
assert.True(stopper.stopped.Load())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConcurrent(t *testing.T) {
|
func TestConcurrent(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
// calling Clean concurrently should call Stop exactly once
|
// calling Clean concurrently should call Stop exactly once
|
||||||
|
|
||||||
stopper := &spyStopper{}
|
stopper := &spyStopper{stopped: &atomic.Bool{}}
|
||||||
cleaner := New(stopper)
|
cleaner := New(stopper)
|
||||||
|
|
||||||
parallelism := 10
|
parallelism := 10
|
||||||
@ -92,13 +92,13 @@ func TestConcurrent(t *testing.T) {
|
|||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
cleaner.Done()
|
cleaner.Done()
|
||||||
assert.Equal(int64(1), atomic.LoadInt64(&stopper.stopped))
|
assert.True(stopper.stopped.Load())
|
||||||
}
|
}
|
||||||
|
|
||||||
type spyStopper struct {
|
type spyStopper struct {
|
||||||
stopped int64
|
stopped *atomic.Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *spyStopper) Stop() {
|
func (s *spyStopper) Stop() {
|
||||||
atomic.AddInt64(&s.stopped, 1)
|
s.stopped.Store(true)
|
||||||
}
|
}
|
||||||
|
@ -37,8 +37,8 @@ type spinner struct {
|
|||||||
out io.Writer
|
out io.Writer
|
||||||
delay time.Duration
|
delay time.Duration
|
||||||
wg *sync.WaitGroup
|
wg *sync.WaitGroup
|
||||||
stop int32
|
stop *atomic.Bool
|
||||||
spinFunc func(out io.Writer, wg *sync.WaitGroup, stop *int32, delay time.Duration, text string, showDots bool)
|
spinFunc func(out io.Writer, wg *sync.WaitGroup, stop *atomic.Bool, delay time.Duration, text string, showDots bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSpinner(writer io.Writer) *spinner {
|
func newSpinner(writer io.Writer) *spinner {
|
||||||
@ -46,10 +46,11 @@ func newSpinner(writer io.Writer) *spinner {
|
|||||||
out: writer,
|
out: writer,
|
||||||
wg: &sync.WaitGroup{},
|
wg: &sync.WaitGroup{},
|
||||||
delay: time.Millisecond * 100,
|
delay: time.Millisecond * 100,
|
||||||
|
stop: &atomic.Bool{},
|
||||||
}
|
}
|
||||||
|
|
||||||
s.spinFunc = spinTTY
|
s.spinFunc = spinTTY
|
||||||
//
|
|
||||||
if !(writer == os.Stdout && tty.IsTerminal(os.Stdout.Fd())) {
|
if !(writer == os.Stdout && tty.IsTerminal(os.Stdout.Fd())) {
|
||||||
s.spinFunc = spinNoTTY
|
s.spinFunc = spinNoTTY
|
||||||
}
|
}
|
||||||
@ -61,12 +62,12 @@ func newSpinner(writer io.Writer) *spinner {
|
|||||||
func (s *spinner) Start(text string, showDots bool) {
|
func (s *spinner) Start(text string, showDots bool) {
|
||||||
s.wg.Add(1)
|
s.wg.Add(1)
|
||||||
|
|
||||||
go s.spinFunc(s.out, s.wg, &s.stop, s.delay, text, showDots)
|
go s.spinFunc(s.out, s.wg, s.stop, s.delay, text, showDots)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop stops the spinner.
|
// Stop stops the spinner.
|
||||||
func (s *spinner) Stop() {
|
func (s *spinner) Stop() {
|
||||||
atomic.StoreInt32(&s.stop, 1)
|
s.stop.Store(true)
|
||||||
s.wg.Wait()
|
s.wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,13 +77,13 @@ func (s *spinner) Write(p []byte) (n int, err error) {
|
|||||||
return s.out.Write(p)
|
return s.out.Write(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func spinTTY(out io.Writer, wg *sync.WaitGroup, stop *int32, delay time.Duration, text string, showDots bool) {
|
func spinTTY(out io.Writer, wg *sync.WaitGroup, stop *atomic.Bool, delay time.Duration, text string, showDots bool) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
fmt.Fprint(out, hideCursor)
|
fmt.Fprint(out, hideCursor)
|
||||||
|
|
||||||
for i := 0; ; i = (i + 1) % len(spinnerStates) {
|
for i := 0; ; i = (i + 1) % len(spinnerStates) {
|
||||||
if atomic.LoadInt32(stop) != 0 {
|
if stop.Load() {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
dotsState := ""
|
dotsState := ""
|
||||||
@ -102,7 +103,7 @@ func spinTTY(out io.Writer, wg *sync.WaitGroup, stop *int32, delay time.Duration
|
|||||||
fmt.Fprint(out, showCursor)
|
fmt.Fprint(out, showCursor)
|
||||||
}
|
}
|
||||||
|
|
||||||
func spinNoTTY(out io.Writer, wg *sync.WaitGroup, _ *int32, _ time.Duration, text string, _ bool) {
|
func spinNoTTY(out io.Writer, wg *sync.WaitGroup, _ *atomic.Bool, _ time.Duration, text string, _ bool) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
fmt.Fprintln(out, text+"...")
|
fmt.Fprintln(out, text+"...")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user