diff --git a/internal/loader/loader.go b/internal/loader/loader.go index 3ec57a0..ca4faa5 100644 --- a/internal/loader/loader.go +++ b/internal/loader/loader.go @@ -2,11 +2,12 @@ package loader import ( "fmt" + "sync/atomic" "time" ) type Loader struct { - running bool + running int32 } func New() *Loader { @@ -14,13 +15,13 @@ func New() *Loader { } func (l *Loader) Start() { - l.running = true + atomic.StoreInt32(&l.running, 1) chars := []rune{'|', '/', '-', '\\'} idx := 0 go func() { - for l.running { + for atomic.LoadInt32(&l.running) == 1 { fmt.Printf("\r%c Loading...", chars[idx]) idx = (idx + 1) % len(chars) time.Sleep(100 * time.Millisecond) @@ -29,6 +30,6 @@ func (l *Loader) Start() { } func (l *Loader) Stop() { - l.running = false + atomic.StoreInt32(&l.running, 0) fmt.Printf("\r\033[K") } diff --git a/internal/loader/loader_test.go b/internal/loader/loader_test.go index dd04cd0..15ff46b 100644 --- a/internal/loader/loader_test.go +++ b/internal/loader/loader_test.go @@ -1,58 +1,69 @@ package loader import ( - "testing" - "time" + "sync/atomic" + "testing" + "time" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/assert" ) -func Test_Start(t *testing.T) { - tests := []struct { - name string - waitTime time.Duration - expect bool - }{ - { - name: "Running", - waitTime: 50 * time.Millisecond, - expect: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - loader := New() - loader.Start() - defer loader.Stop() - - time.Sleep(tt.waitTime) - assert.Equal(t, tt.expect, loader.running) - }) - } +func TestLoader_Start(t *testing.T) { + tests := []struct { + name string + waitTime time.Duration + expectRunning bool + }{ + { + name: "Loader starts and runs", + waitTime: 200 * time.Millisecond, + expectRunning: true, + }, + { + name: "Loader runs for a short time", + waitTime: 100 * time.Millisecond, + expectRunning: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + l := New() + + l.Start() + time.Sleep(tt.waitTime) + + running := atomic.LoadInt32(&l.running) == 1 + assert.Equal(t, tt.expectRunning, running) + + l.Stop() + }) + } } -func Test_Stop(t *testing.T) { - tests := []struct { - name string - waitTime time.Duration - expect bool - }{ - { - name: "Stopped", - waitTime: 50 * time.Millisecond, - expect: false, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - loader := New() - loader.Start() - loader.Stop() - - time.Sleep(tt.waitTime) - assert.Equal(t, tt.expect, loader.running) - }) - } +func TestLoader_Stop(t *testing.T) { + tests := []struct { + name string + waitTime time.Duration + expectRunning bool + }{ + { + name: "Loader stops after start", + waitTime: 100 * time.Millisecond, + expectRunning: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + l := New() + + l.Start() + l.Stop() + time.Sleep(tt.waitTime) + + running := atomic.LoadInt32(&l.running) == 1 + assert.Equal(t, tt.expectRunning, running) + }) + } }