Fix race conditions in tests

This commit is contained in:
Deluan 2022-11-26 15:07:09 -05:00
parent 0bb133a6ac
commit 88823fca76
4 changed files with 24 additions and 13 deletions

View File

@ -68,7 +68,7 @@ jobs:
- name: Test - name: Test
continue-on-error: ${{contains(matrix.go_version, 'beta') || contains(matrix.go_version, 'rc')}} 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: js:
name: Build JS bundle name: Build JS bundle

View File

@ -29,7 +29,7 @@ watch: ##@Development Start Go tests in watch mode (re-run when code changes)
.PHONY: watch .PHONY: watch
test: ##@Development Run Go tests test: ##@Development Run Go tests
go test ./... go test -race ./...
.PHONY: test .PHONY: test
testall: test ##@Development Run Go and JS tests testall: test ##@Development Run Go and JS tests

View File

@ -5,6 +5,8 @@ import (
"io" "io"
"os" "os"
"strings" "strings"
"sync"
"sync/atomic"
"github.com/navidrome/navidrome/conf" "github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/log"
@ -18,7 +20,7 @@ import (
var _ = Describe("MediaStreamer", func() { var _ = Describe("MediaStreamer", func() {
var streamer MediaStreamer var streamer MediaStreamer
var ds model.DataStore var ds model.DataStore
ffmpeg := &fakeFFmpeg{Data: "fake data"} ffmpeg := newFakeFFmpeg("fake data")
ctx := log.NewContext(context.TODO()) ctx := log.NewContext(context.TODO())
BeforeEach(func() { BeforeEach(func() {
@ -63,7 +65,7 @@ var _ = Describe("MediaStreamer", func() {
Expect(err).To(BeNil()) Expect(err).To(BeNil())
_, _ = io.ReadAll(s) _, _ = io.ReadAll(s)
_ = s.Close() _ = 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) s, err = streamer.NewStream(ctx, "123", "mp3", 32)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
@ -193,22 +195,31 @@ var _ = Describe("MediaStreamer", func() {
}) })
}) })
func newFakeFFmpeg(data string) *fakeFFmpeg {
return &fakeFFmpeg{Reader: strings.NewReader(data)}
}
type fakeFFmpeg struct { type fakeFFmpeg struct {
Data string io.Reader
r io.Reader lock sync.Mutex
closed bool closed atomic.Bool
} }
func (ff *fakeFFmpeg) Start(ctx context.Context, cmd, path string, maxBitRate int) (f io.ReadCloser, err error) { 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 return ff, nil
} }
func (ff *fakeFFmpeg) Read(p []byte) (n int, err error) { 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 { func (ff *fakeFFmpeg) Close() error {
ff.closed = true ff.closed.Store(true)
return nil return nil
} }
func (ff *fakeFFmpeg) IsClosed() bool {
return ff.closed.Load()
}

View File

@ -19,9 +19,9 @@ var _ = Describe("walk_dir_tree", func() {
It("reads all info correctly", func() { It("reads all info correctly", func() {
var collected = dirMap{} var collected = dirMap{}
results := make(walkResults, 5000) results := make(walkResults, 5000)
var err error var errC = make(chan error)
go func() { go func() {
err = walkDirTree(context.TODO(), baseDir, results) errC <- walkDirTree(context.Background(), baseDir, results)
}() }()
for { for {
@ -32,7 +32,7 @@ var _ = Describe("walk_dir_tree", func() {
collected[stats.Path] = stats collected[stats.Path] = stats
} }
Expect(err).To(BeNil()) Eventually(errC).Should(Receive(nil))
Expect(collected[baseDir]).To(MatchFields(IgnoreExtras, Fields{ Expect(collected[baseDir]).To(MatchFields(IgnoreExtras, Fields{
"HasImages": BeTrue(), "HasImages": BeTrue(),
"HasPlaylist": BeFalse(), "HasPlaylist": BeFalse(),