From 26f0033ebb5f8e7c204a042709e3dfa885eac868 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Sat, 22 Jul 2023 15:15:47 +0200 Subject: fix applyTimeBonus by always using Song pointers Storing song objects in the Songs slice and using pointers to the underlying array slots breaks if the underlying array must be grown. When the array is grown the objects are copied to the new array and the pointers used in queue point to the old objects. This causes that Playlist.applyTimeBonus may not correctly apply to the most recent list of songs. This is fixed by always using Song pointers. --- wrms_test.go | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) (limited to 'wrms_test.go') diff --git a/wrms_test.go b/wrms_test.go index 7d0c4d1..29ee2b2 100644 --- a/wrms_test.go +++ b/wrms_test.go @@ -1,22 +1,22 @@ package main import ( + "fmt" "github.com/google/uuid" "io" - "muhq.space/go/wrms/llog" "testing" ) type mockPlayer struct{} -func (p *mockPlayer) Play(*Song) {} -func (p *mockPlayer) PlayUri(string) {} -func (p *mockPlayer) PlayData(io.Reader) {} -func (p *mockPlayer) Search(pattern map[string]string) (res chan []Song) { return } -func (p *mockPlayer) Playing() bool { return false } -func (p *mockPlayer) Pause() {} -func (p *mockPlayer) Continue() {} -func (p *mockPlayer) Stop() {} +func (p *mockPlayer) Play(*Song) {} +func (p *mockPlayer) PlayUri(string) {} +func (p *mockPlayer) PlayData(io.Reader) {} +func (p *mockPlayer) Search(pattern map[string]string) (res chan []*Song) { return } +func (p *mockPlayer) Playing() bool { return false } +func (p *mockPlayer) Pause() {} +func (p *mockPlayer) Continue() {} +func (p *mockPlayer) Stop() {} var alice, _ = uuid.NewRandom() @@ -33,7 +33,7 @@ func TestWrmsAdd(t *testing.T) { func TestWrmsAdd3Next3(t *testing.T) { wrms := Wrms{Player: &mockPlayer{}} - songs := []Song{ + songs := []*Song{ NewDummySong("song1", "snfmt"), NewDummySong("song2", "snfmt"), NewDummySong("song3", "snfmt"), @@ -94,7 +94,6 @@ func TestWrmsUpUpDownNextNext(t *testing.T) { wrms.AddSong(s2) wrms.AddSong(s3) - llog.Info("UpUpDown test") wrms.AdjustSongWeight(alice, s1.Uri, "up") wrms.AdjustSongWeight(alice, s2.Uri, "down") wrms.AdjustSongWeight(alice, s3.Uri, "up") @@ -114,7 +113,6 @@ func TestWrmsDoubleAdd(t *testing.T) { wrms := Wrms{Player: &mockPlayer{}} s1 := NewDummySong("song1", "snfmt") - llog.Info("Double Add test") wrms.AddSong(s1) wrms.AdjustSongWeight(alice, s1.Uri, "up") wrms.Next() @@ -141,3 +139,23 @@ func TestFlipVote(t *testing.T) { t.Fail() } } + +func TestTimeBonus(t *testing.T) { + wrms := Wrms{Player: &mockPlayer{}} + wrms.Config.timeBonus = 0.1 + songs := []*Song{ + NewDummySong("song1", "snfmt"), + NewDummySong("song2", "snfmt"), + NewDummySong("song3", "snfmt"), + } + + for _, s := range songs { + wrms.AddSong(s) + } + + exp := 0.1 * float64(len(songs)-1) + if wrms.Songs[0].Weight != exp { + t.Log(fmt.Sprintf("TimeBonus not correctly applied song %p %v expected: %v", &wrms.Songs[0], wrms.Songs[0], exp)) + t.Fail() + } +} -- cgit v1.2.3