Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix race condition in (*Spinner).Print #46

Merged
merged 4 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/signal"
"runtime"
"sync"
"time"

"github.com/chelnak/ysmrr/pkg/animations"
Expand All @@ -32,7 +33,9 @@ type SpinnerManager interface {
}

type spinnerManager struct {
spinners []*Spinner
spinners []*Spinner
mutex sync.RWMutex

chars []string
frameDuration time.Duration
spinnerColor colors.Color
Expand All @@ -58,12 +61,18 @@ func (sm *spinnerManager) AddSpinner(message string) *Spinner {
HasUpdate: sm.hasUpdate,
}
spinner := NewSpinner(opts)

sm.mutex.Lock()
defer sm.mutex.Unlock()
sm.spinners = append(sm.spinners, spinner)

return spinner
}

// GetSpinners returns the spinners managed by the manager.
func (sm *spinnerManager) GetSpinners() []*Spinner {
chelnak marked this conversation as resolved.
Show resolved Hide resolved
sm.mutex.RLock()
defer sm.mutex.RUnlock()
return sm.spinners
}

Expand Down Expand Up @@ -96,6 +105,8 @@ func (sm *spinnerManager) Stop() {
sm.ticks.Stop()

// Persist the final frame for each spinner.
sm.mutex.Lock()
defer sm.mutex.Unlock()
for _, s := range sm.spinners {
tput.ClearLine(sm.writer)
s.Print(sm.writer, sm.chars[sm.frame])
Expand Down Expand Up @@ -173,11 +184,13 @@ func (sm *spinnerManager) renderFrame(animate bool) {
return
}

tput.BufScreen(sm.writer, len(sm.spinners))
tput.Cuu(sm.writer, len(sm.spinners))
spinners := sm.GetSpinners()

tput.BufScreen(sm.writer, len(spinners))
tput.Cuu(sm.writer, len(spinners))
tput.Sc(sm.writer)

for _, s := range sm.spinners {
for _, s := range spinners {
tput.ClearLine(sm.writer)
s.Print(sm.writer, sm.chars[sm.frame])
}
Expand Down
22 changes: 22 additions & 0 deletions manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ysmrr_test

import (
"bytes"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -105,3 +106,24 @@ func TestGetMessageColor(t *testing.T) {

assert.Equal(t, colors.FgHiBlue, spinnerManager.GetMessageColor())
}

func TestManagerConcurrentSpinnerUsage(t *testing.T) {
var out bytes.Buffer
spinnerManager := ysmrr.NewSpinnerManager(ysmrr.WithWriter(&out))
spinnerManager.Start()

var wg sync.WaitGroup
for _, msg := range []string{"test-1", "test-2", "test-3"} {
wg.Add(1)
go func(wg *sync.WaitGroup, msg string) {
defer wg.Done()

s := spinnerManager.AddSpinner(msg)
s.UpdateMessage(msg + "-update")
s.Complete()
}(&wg, msg)
}
wg.Wait()

spinnerManager.Stop()
}
2 changes: 2 additions & 0 deletions spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func (s *Spinner) Print(w io.Writer, char string) {
print(w, char, s.spinnerColor)
}

s.mutex.Lock()
defer s.mutex.Unlock()
message := fmt.Sprintf(" %s\r\n", s.message)
print(w, message, s.messageColor)
}
Expand Down