Removed list_generator completely

This commit is contained in:
Deluan
2020-10-27 10:19:58 -04:00
parent 3037ea01e2
commit 6152fadd92
11 changed files with 99 additions and 149 deletions
+106
View File
@@ -0,0 +1,106 @@
package core
import (
"container/list"
"sync"
"time"
)
const NowPlayingExpire = 60 * time.Minute
type NowPlayingInfo struct {
TrackID string
Start time.Time
Username string
PlayerId int
PlayerName string
}
// This repo must have the semantics of a FIFO queue, for each playerId
type NowPlaying interface {
// Insert at the head of the queue
Enqueue(*NowPlayingInfo) error
// Returns all heads from all playerIds
GetAll() ([]*NowPlayingInfo, error)
}
var playerMap = sync.Map{}
type nowPlayingRepository struct{}
func NewNowPlayingRepository() NowPlaying {
r := &nowPlayingRepository{}
return r
}
func (r *nowPlayingRepository) Enqueue(info *NowPlayingInfo) error {
l := r.getList(info.PlayerId)
l.PushFront(info)
return nil
}
func (r *nowPlayingRepository) GetAll() ([]*NowPlayingInfo, error) {
var all []*NowPlayingInfo
playerMap.Range(func(playerId, l interface{}) bool {
ll := l.(*list.List)
e := checkExpired(ll, ll.Front)
if e != nil {
all = append(all, e.Value.(*NowPlayingInfo))
}
return true
})
return all, nil
}
func (r *nowPlayingRepository) getList(id int) *list.List {
l, _ := playerMap.LoadOrStore(id, list.New())
return l.(*list.List)
}
func (r *nowPlayingRepository) dequeue(playerId int) (*NowPlayingInfo, error) {
l := r.getList(playerId)
e := checkExpired(l, l.Back)
if e == nil {
return nil, nil
}
l.Remove(e)
return e.Value.(*NowPlayingInfo), nil
}
func (r *nowPlayingRepository) head(playerId int) (*NowPlayingInfo, error) {
l := r.getList(playerId)
e := checkExpired(l, l.Front)
if e == nil {
return nil, nil
}
return e.Value.(*NowPlayingInfo), nil
}
func (r *nowPlayingRepository) tail(playerId int) (*NowPlayingInfo, error) {
l := r.getList(playerId)
e := checkExpired(l, l.Back)
if e == nil {
return nil, nil
}
return e.Value.(*NowPlayingInfo), nil
}
func (r *nowPlayingRepository) count(playerId int) (int64, error) {
l := r.getList(playerId)
return int64(l.Len()), nil
}
func checkExpired(l *list.List, f func() *list.Element) *list.Element {
for {
e := f()
if e == nil {
return nil
}
start := e.Value.(*NowPlayingInfo).Start
if time.Since(start) < NowPlayingExpire {
return e
}
l.Remove(e)
}
}
+61
View File
@@ -0,0 +1,61 @@
package core
import (
"sync"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("NowPlaying", func() {
var repo *nowPlayingRepository
var now = time.Now()
var past = time.Time{}
BeforeEach(func() {
playerMap = sync.Map{}
repo = NewNowPlayingRepository().(*nowPlayingRepository)
})
It("enqueues and dequeues records", func() {
Expect(repo.Enqueue(&NowPlayingInfo{PlayerId: 1, TrackID: "AAA", Start: now})).To(BeNil())
Expect(repo.Enqueue(&NowPlayingInfo{PlayerId: 1, TrackID: "BBB", Start: now})).To(BeNil())
Expect(repo.tail(1)).To(Equal(&NowPlayingInfo{PlayerId: 1, TrackID: "AAA", Start: now}))
Expect(repo.head(1)).To(Equal(&NowPlayingInfo{PlayerId: 1, TrackID: "BBB", Start: now}))
Expect(repo.count(1)).To(Equal(int64(2)))
Expect(repo.dequeue(1)).To(Equal(&NowPlayingInfo{PlayerId: 1, TrackID: "AAA", Start: now}))
Expect(repo.count(1)).To(Equal(int64(1)))
})
It("handles multiple players", func() {
Expect(repo.Enqueue(&NowPlayingInfo{PlayerId: 1, TrackID: "AAA", Start: now})).To(BeNil())
Expect(repo.Enqueue(&NowPlayingInfo{PlayerId: 1, TrackID: "BBB", Start: now})).To(BeNil())
Expect(repo.Enqueue(&NowPlayingInfo{PlayerId: 2, TrackID: "CCC", Start: now})).To(BeNil())
Expect(repo.Enqueue(&NowPlayingInfo{PlayerId: 2, TrackID: "DDD", Start: now})).To(BeNil())
Expect(repo.GetAll()).To(ConsistOf([]*NowPlayingInfo{
{PlayerId: 1, TrackID: "BBB", Start: now},
{PlayerId: 2, TrackID: "DDD", Start: now},
}))
Expect(repo.count(2)).To(Equal(int64(2)))
Expect(repo.count(2)).To(Equal(int64(2)))
Expect(repo.tail(1)).To(Equal(&NowPlayingInfo{PlayerId: 1, TrackID: "AAA", Start: now}))
Expect(repo.head(2)).To(Equal(&NowPlayingInfo{PlayerId: 2, TrackID: "DDD", Start: now}))
})
It("handles expired items", func() {
Expect(repo.Enqueue(&NowPlayingInfo{PlayerId: 1, TrackID: "AAA", Start: past})).To(BeNil())
Expect(repo.Enqueue(&NowPlayingInfo{PlayerId: 2, TrackID: "BBB", Start: now})).To(BeNil())
Expect(repo.GetAll()).To(ConsistOf([]*NowPlayingInfo{
{PlayerId: 2, TrackID: "BBB", Start: now},
}))
})
})
+1
View File
@@ -16,6 +16,7 @@ var Set = wire.NewSet(
NewTranscodingCache,
NewImageCache,
NewArchiver,
NewNowPlayingRepository,
NewExternalInfo,
NewCacheWarmer,
LastFMNewClient,