Skip to content

Commit

Permalink
Fix race conditions in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Nov 26, 2022
1 parent 0bb133a commit 88823fc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:

- name: Test
continue-on-error: ${{contains(matrix.go_version, 'beta') || contains(matrix.go_version, 'rc')}}
run: go test -cover ./... -v
run: go test -race -cover ./... -v

js:
name: Build JS bundle
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ watch: ##@Development Start Go tests in watch mode (re-run when code changes)
.PHONY: watch

test: ##@Development Run Go tests
go test ./...
go test -race ./...
.PHONY: test

testall: test ##@Development Run Go and JS tests
Expand Down
27 changes: 19 additions & 8 deletions core/media_streamer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"io"
"os"
"strings"
"sync"
"sync/atomic"

"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/log"
Expand All @@ -18,7 +20,7 @@ import (
var _ = Describe("MediaStreamer", func() {
var streamer MediaStreamer
var ds model.DataStore
ffmpeg := &fakeFFmpeg{Data: "fake data"}
ffmpeg := newFakeFFmpeg("fake data")
ctx := log.NewContext(context.TODO())

BeforeEach(func() {
Expand Down Expand Up @@ -63,7 +65,7 @@ var _ = Describe("MediaStreamer", func() {
Expect(err).To(BeNil())
_, _ = io.ReadAll(s)
_ = s.Close()
Eventually(func() bool { return ffmpeg.closed }, "3s").Should(BeTrue())
Eventually(func() bool { return ffmpeg.IsClosed() }, "3s").Should(BeTrue())

s, err = streamer.NewStream(ctx, "123", "mp3", 32)
Expect(err).To(BeNil())
Expand Down Expand Up @@ -193,22 +195,31 @@ var _ = Describe("MediaStreamer", func() {
})
})

func newFakeFFmpeg(data string) *fakeFFmpeg {
return &fakeFFmpeg{Reader: strings.NewReader(data)}
}

type fakeFFmpeg struct {
Data string
r io.Reader
closed bool
io.Reader
lock sync.Mutex
closed atomic.Bool
}

func (ff *fakeFFmpeg) Start(ctx context.Context, cmd, path string, maxBitRate int) (f io.ReadCloser, err error) {
ff.r = strings.NewReader(ff.Data)
return ff, nil
}

func (ff *fakeFFmpeg) Read(p []byte) (n int, err error) {
return ff.r.Read(p)
ff.lock.Lock()
defer ff.lock.Unlock()
return ff.Reader.Read(p)
}

func (ff *fakeFFmpeg) Close() error {
ff.closed = true
ff.closed.Store(true)
return nil
}

func (ff *fakeFFmpeg) IsClosed() bool {
return ff.closed.Load()
}
6 changes: 3 additions & 3 deletions scanner/walk_dir_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ var _ = Describe("walk_dir_tree", func() {
It("reads all info correctly", func() {
var collected = dirMap{}
results := make(walkResults, 5000)
var err error
var errC = make(chan error)
go func() {
err = walkDirTree(context.TODO(), baseDir, results)
errC <- walkDirTree(context.Background(), baseDir, results)
}()

for {
Expand All @@ -32,7 +32,7 @@ var _ = Describe("walk_dir_tree", func() {
collected[stats.Path] = stats
}

Expect(err).To(BeNil())
Eventually(errC).Should(Receive(nil))
Expect(collected[baseDir]).To(MatchFields(IgnoreExtras, Fields{
"HasImages": BeTrue(),
"HasPlaylist": BeFalse(),
Expand Down

0 comments on commit 88823fc

Please sign in to comment.