Refactored NowPlaying
Also added a test case for skipping range
This commit is contained in:
@@ -12,6 +12,7 @@ func CreateMockNowPlayingRepo() *MockNowPlaying {
|
|||||||
type MockNowPlaying struct {
|
type MockNowPlaying struct {
|
||||||
NowPlayingRepository
|
NowPlayingRepository
|
||||||
data []NowPlayingInfo
|
data []NowPlayingInfo
|
||||||
|
t time.Time
|
||||||
err bool
|
err bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,20 +20,19 @@ func (m *MockNowPlaying) SetError(err bool) {
|
|||||||
m.err = err
|
m.err = err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockNowPlaying) Enqueue(playerId int, playerName string, trackId, username string) error {
|
func (m *MockNowPlaying) Enqueue(info *NowPlayingInfo) error {
|
||||||
if m.err {
|
if m.err {
|
||||||
return errors.New("Error!")
|
return errors.New("Error!")
|
||||||
}
|
}
|
||||||
info := NowPlayingInfo{}
|
|
||||||
info.TrackId = trackId
|
|
||||||
info.Username = username
|
|
||||||
info.Start = time.Now()
|
|
||||||
info.PlayerId = playerId
|
|
||||||
info.PlayerName = playerName
|
|
||||||
|
|
||||||
m.data = append(m.data, NowPlayingInfo{})
|
m.data = append(m.data, NowPlayingInfo{})
|
||||||
copy(m.data[1:], m.data[0:])
|
copy(m.data[1:], m.data[0:])
|
||||||
m.data[0] = info
|
m.data[0] = *info
|
||||||
|
|
||||||
|
if !m.t.IsZero() {
|
||||||
|
m.data[0].Start = m.t
|
||||||
|
m.t = time.Time{}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -80,3 +80,7 @@ func (m *MockNowPlaying) ClearAll() {
|
|||||||
m.data = make([]NowPlayingInfo, 0)
|
m.data = make([]NowPlayingInfo, 0)
|
||||||
m.err = false
|
m.err = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *MockNowPlaying) OverrideNow(t time.Time) {
|
||||||
|
m.t = t
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ type NowPlayingInfo struct {
|
|||||||
// This repo must have the semantics of a FIFO queue, for each playerId
|
// This repo must have the semantics of a FIFO queue, for each playerId
|
||||||
type NowPlayingRepository interface {
|
type NowPlayingRepository interface {
|
||||||
// Insert at the head of the queue
|
// Insert at the head of the queue
|
||||||
Enqueue(playerId int, playerName string, trackId, username string) error
|
Enqueue(*NowPlayingInfo) error
|
||||||
|
|
||||||
// Removes and returns the element at the end of the queue
|
// Removes and returns the element at the end of the queue
|
||||||
Dequeue(playerId int) (*NowPlayingInfo, error)
|
Dequeue(playerId int) (*NowPlayingInfo, error)
|
||||||
|
|||||||
+9
-3
@@ -10,6 +10,11 @@ import (
|
|||||||
"github.com/deluan/gosonic/itunesbridge"
|
"github.com/deluan/gosonic/itunesbridge"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
minSkipped = time.Duration(3) * time.Second
|
||||||
|
maxSkipped = time.Duration(20) * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
type Scrobbler interface {
|
type Scrobbler interface {
|
||||||
Register(playerId int, trackId string, playDate time.Time) (*domain.MediaFile, error)
|
Register(playerId int, trackId string, playDate time.Time) (*domain.MediaFile, error)
|
||||||
NowPlaying(playerId int, playerName, trackId, username string) (*domain.MediaFile, error)
|
NowPlaying(playerId int, playerName, trackId, username string) (*domain.MediaFile, error)
|
||||||
@@ -46,7 +51,7 @@ func (s *scrobbler) detectSkipped(playerId int, trackId string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *scrobbler) Register(playerId int, trackId string, playDate time.Time) (*domain.MediaFile, error) {
|
func (s *scrobbler) Register(playerId int, trackId string, playTime time.Time) (*domain.MediaFile, error) {
|
||||||
s.detectSkipped(playerId, trackId)
|
s.detectSkipped(playerId, trackId)
|
||||||
|
|
||||||
mf, err := s.mfRepo.Get(trackId)
|
mf, err := s.mfRepo.Get(trackId)
|
||||||
@@ -58,7 +63,7 @@ func (s *scrobbler) Register(playerId int, trackId string, playDate time.Time) (
|
|||||||
return nil, errors.New(fmt.Sprintf(`Id "%s" not found`, trackId))
|
return nil, errors.New(fmt.Sprintf(`Id "%s" not found`, trackId))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.itunes.MarkAsPlayed(trackId, playDate); err != nil {
|
if err := s.itunes.MarkAsPlayed(trackId, playTime); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return mf, nil
|
return mf, nil
|
||||||
@@ -74,5 +79,6 @@ func (s *scrobbler) NowPlaying(playerId int, playerName, trackId, username strin
|
|||||||
return nil, errors.New(fmt.Sprintf(`Id "%s" not found`, trackId))
|
return nil, errors.New(fmt.Sprintf(`Id "%s" not found`, trackId))
|
||||||
}
|
}
|
||||||
|
|
||||||
return mf, s.npRepo.Enqueue(playerId, playerName, trackId, username)
|
info := &NowPlayingInfo{TrackId: trackId, Username: username, Start: time.Now(), PlayerId: playerId, PlayerName: playerName}
|
||||||
|
return mf, s.npRepo.Enqueue(info)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,21 +80,45 @@ func TestScrobbler(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSkipping(t *testing.T) {
|
||||||
|
Init(t, false)
|
||||||
|
|
||||||
|
mfRepo := persistence.CreateMockMediaFileRepo()
|
||||||
|
npRepo := engine.CreateMockNowPlayingRepo()
|
||||||
|
itCtrl := &mockItunesControl{}
|
||||||
|
|
||||||
|
scrobbler := engine.NewScrobbler(itCtrl, mfRepo, npRepo)
|
||||||
|
|
||||||
Convey("Given a DB with three songs", t, func() {
|
Convey("Given a DB with three songs", t, func() {
|
||||||
mfRepo.SetData(`[{"Id":"1","Title":"Femme Fatale"},{"Id":"2","Title":"Here She Comes Now"},{"Id":"3","Title":"Lady Godiva's Operation"}]`, 3)
|
mfRepo.SetData(`[{"Id":"1","Title":"Femme Fatale"},{"Id":"2","Title":"Here She Comes Now"},{"Id":"3","Title":"Lady Godiva's Operation"}]`, 3)
|
||||||
itCtrl.skipped = make(map[string]time.Time)
|
itCtrl.skipped = make(map[string]time.Time)
|
||||||
npRepo.ClearAll()
|
npRepo.ClearAll()
|
||||||
Convey("When I play one song", func() {
|
Convey("When I play one song", func() {
|
||||||
|
start := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
|
||||||
|
npRepo.OverrideNow(start)
|
||||||
scrobbler.NowPlaying(1, "DSub", "1", "deluan")
|
scrobbler.NowPlaying(1, "DSub", "1", "deluan")
|
||||||
Convey("And I play the other song without scrobbling the first one", func() {
|
Convey("And I skip it before 20 seconds", func() {
|
||||||
|
npRepo.OverrideNow(start.Add(time.Duration(5) * time.Second))
|
||||||
scrobbler.NowPlaying(1, "DSub", "2", "deluan")
|
scrobbler.NowPlaying(1, "DSub", "2", "deluan")
|
||||||
mf, err := scrobbler.Register(1, "2", time.Now())
|
|
||||||
Convey("Then the first song should be marked as skipped", func() {
|
Convey("Then the first song should be marked as skipped", func() {
|
||||||
|
mf, err := scrobbler.Register(1, "2", start.Add(time.Duration(3)*time.Minute))
|
||||||
So(mf.Id, ShouldEqual, "2")
|
So(mf.Id, ShouldEqual, "2")
|
||||||
So(itCtrl.skipped, ShouldContainKey, "1")
|
So(itCtrl.skipped, ShouldContainKey, "1")
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
SkipConvey("And I skip it after 20 seconds", func() {
|
||||||
|
npRepo.OverrideNow(start.Add(time.Duration(30) * time.Second))
|
||||||
|
scrobbler.NowPlaying(1, "DSub", "2", "deluan")
|
||||||
|
Convey("Then the first song should be marked as skipped", func() {
|
||||||
|
mf, err := scrobbler.Register(1, "2", start.Add(time.Duration(3)*time.Minute))
|
||||||
|
So(mf.Id, ShouldEqual, "2")
|
||||||
|
So(itCtrl.skipped, ShouldBeEmpty)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
})
|
||||||
|
})
|
||||||
Convey("And I scrobble it before starting to play the other song", func() {
|
Convey("And I scrobble it before starting to play the other song", func() {
|
||||||
mf, err := scrobbler.Register(1, "1", time.Now())
|
mf, err := scrobbler.Register(1, "1", time.Now())
|
||||||
Convey("Then the first song should NOT marked as skipped", func() {
|
Convey("Then the first song should NOT marked as skipped", func() {
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package persistence
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/deluan/gosonic/engine"
|
"github.com/deluan/gosonic/engine"
|
||||||
)
|
)
|
||||||
@@ -26,15 +25,13 @@ func nowPlayingKeyName(playerId int) string {
|
|||||||
return fmt.Sprintf("%s:%d", nowPlayingKeyPrefix, playerId)
|
return fmt.Sprintf("%s:%d", nowPlayingKeyPrefix, playerId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *nowPlayingRepository) Enqueue(playerId int, playerName, id, username string) error {
|
func (r *nowPlayingRepository) Enqueue(info *engine.NowPlayingInfo) error {
|
||||||
m := &engine.NowPlayingInfo{TrackId: id, Username: username, Start: time.Now(), PlayerId: playerId, PlayerName: playerName}
|
h, err := json.Marshal(info)
|
||||||
|
|
||||||
h, err := json.Marshal(m)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
keyName := []byte(nowPlayingKeyName(playerId))
|
keyName := []byte(nowPlayingKeyName(info.PlayerId))
|
||||||
|
|
||||||
_, err = Db().LPush(keyName, []byte(h))
|
_, err = Db().LPush(keyName, []byte(h))
|
||||||
Db().LExpire(keyName, int64(engine.NowPlayingExpire.Seconds()))
|
Db().LExpire(keyName, int64(engine.NowPlayingExpire.Seconds()))
|
||||||
|
|||||||
Reference in New Issue
Block a user