From a134b1b608902b69333ad48128f4de6c276605ce Mon Sep 17 00:00:00 2001 From: Deluan Date: Wed, 15 Feb 2023 21:21:59 -0500 Subject: [PATCH] Use sync/atomic package, now that we are at Go 1.19 --- tests/mock_ffmpeg.go | 9 ++++----- utils/cache/file_caches_test.go | 2 +- utils/pl/pipelines_test.go | 20 +++++++++++--------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/tests/mock_ffmpeg.go b/tests/mock_ffmpeg.go index 9431c157..09e651d9 100644 --- a/tests/mock_ffmpeg.go +++ b/tests/mock_ffmpeg.go @@ -5,8 +5,7 @@ import ( "io" "strings" "sync" - - "github.com/navidrome/navidrome/utils" + "sync/atomic" ) func NewMockFFmpeg(data string) *MockFFmpeg { @@ -16,7 +15,7 @@ func NewMockFFmpeg(data string) *MockFFmpeg { type MockFFmpeg struct { io.Reader lock sync.Mutex - closed utils.AtomicBool + closed atomic.Bool Error error } @@ -54,10 +53,10 @@ func (ff *MockFFmpeg) Read(p []byte) (n int, err error) { } func (ff *MockFFmpeg) Close() error { - ff.closed.Set(true) + ff.closed.Store(true) return nil } func (ff *MockFFmpeg) IsClosed() bool { - return ff.closed.Get() + return ff.closed.Load() } diff --git a/utils/cache/file_caches_test.go b/utils/cache/file_caches_test.go index 5b5154c1..5884d8f4 100644 --- a/utils/cache/file_caches_test.go +++ b/utils/cache/file_caches_test.go @@ -17,7 +17,7 @@ import ( // Call NewFileCache and wait for it to be ready func callNewFileCache(name, cacheSize, cacheFolder string, maxItems int, getReader ReadFunc) *fileCache { fc := NewFileCache(name, cacheSize, cacheFolder, maxItems, getReader).(*fileCache) - Eventually(func() bool { return fc.ready.Get() }).Should(BeTrue()) + Eventually(func() bool { return fc.ready.Load() }).Should(BeTrue()) return fc } diff --git a/utils/pl/pipelines_test.go b/utils/pl/pipelines_test.go index 39e7d014..f5da6e49 100644 --- a/utils/pl/pipelines_test.go +++ b/utils/pl/pipelines_test.go @@ -53,13 +53,15 @@ var _ = Describe("Pipeline", func() { } close(inC) - var current, count, max int32 + current := atomic.Int32{} + count := atomic.Int32{} + max := atomic.Int32{} outC, _ := pl.Stage(context.Background(), maxWorkers, inC, func(ctx context.Context, in int) (int, error) { - defer atomic.AddInt32(¤t, -1) - c := atomic.AddInt32(¤t, 1) - atomic.AddInt32(&count, 1) - if c > atomic.LoadInt32(&max) { - atomic.StoreInt32(&max, c) + defer current.Add(-1) + c := current.Add(1) + count.Add(1) + if c > max.Load() { + max.Store(c) } time.Sleep(10 * time.Millisecond) // Slow process return 0, nil @@ -68,9 +70,9 @@ var _ = Describe("Pipeline", func() { for range outC { } - Expect(count).To(Equal(int32(numJobs))) - Expect(current).To(Equal(int32(0))) - Expect(max).To(Equal(int32(maxWorkers))) + Expect(count.Load()).To(Equal(int32(numJobs))) + Expect(current.Load()).To(Equal(int32(0))) + Expect(max.Load()).To(Equal(int32(maxWorkers))) }) }) When("the context is canceled", func() {