Skip to content

Commit

Permalink
streamer/queue: make queue return copies of entries (fixes #145)
Browse files Browse the repository at this point in the history
radio: add Song.Copy and QueueEntry.Copy
  • Loading branch information
Wessie committed Dec 20, 2024
1 parent b905296 commit 3e765ac
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
18 changes: 18 additions & 0 deletions radio.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,11 @@ type QueueEntry struct {
ExpectedStartTime time.Time
}

func (qe QueueEntry) Copy() QueueEntry {
qe.Song = qe.Song.Copy()
return qe
}

func (qe QueueEntry) String() string {
est := qe.ExpectedStartTime.Format("2006-01-02 15:04:05")
if qe.IsUserRequest {
Expand Down Expand Up @@ -679,6 +684,19 @@ type DatabaseTrack struct {
RequestCount int
}

// Copy copies the song and returns it
func (s Song) Copy() Song {
if s.DatabaseTrack != nil {
dt := *s.DatabaseTrack
s.DatabaseTrack = &dt
}
if s.LastPlayedBy != nil {
lpb := *s.LastPlayedBy
s.LastPlayedBy = &lpb
}
return s
}

// Requestable returns whether this song can be requested by a user
func (s *Song) Requestable() bool {
if s == nil || s.DatabaseTrack == nil {
Expand Down
32 changes: 32 additions & 0 deletions radio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,3 +577,35 @@ func TestSongHashJSON(t *testing.T) {
}))
p.TestingRun(t)
}

func TestSongCopy(t *testing.T) {
a := arbitrary.DefaultArbitraries()
p := gopter.NewProperties(nil)
p.Property("copy is equal", a.ForAll(func(in Song) bool {
out := in.Copy()

return reflect.DeepEqual(in, out)
}))
p.Property("copy is an actual copy", a.ForAll(func(in Song, in2 DatabaseTrack) bool {
in.DatabaseTrack = &in2

out := in.Copy()

out.TrackID = out.TrackID + 2000
out.Artist = out.Artist + "added artist"
out.Title = out.Title + "added title"
out.Album = out.Album + "added album"
out.FilePath = out.FilePath + "added filepath"
out.Tags = out.Tags + "added tags"
out.Acceptor = out.Acceptor + "added acceptor"
out.LastEditor = out.LastEditor + "added lasteditor"
out.Priority = out.Priority + 20
out.Usable = !out.Usable
out.NeedReplacement = !out.NeedReplacement
out.RequestCount = out.RequestCount + 5
out.LastRequested = out.LastRequested.Add(time.Minute * 4)

return !reflect.DeepEqual(in, out)
}))
p.TestingRun(t)
}
10 changes: 6 additions & 4 deletions streamer/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (qs *QueueService) AddRequest(ctx context.Context, song radio.Song, identif
defer qs.mu.Unlock()

qs.append(ctx, radio.QueueEntry{
Song: song,
Song: song.Copy(),
IsUserRequest: true,
UserIdentifier: identifier,
})
Expand All @@ -156,7 +156,7 @@ func (qs *QueueService) ReserveNext(ctx context.Context) (*radio.QueueEntry, err
return nil, errors.E(op, errors.QueueExhausted)
}

entry := qs.queue[qs.reservedIndex]
entry := qs.queue[qs.reservedIndex].Copy()
qs.reservedIndex++
qs.logger.Info().Str("entry", entry.String()).Msg("reserve in queue")

Expand Down Expand Up @@ -234,8 +234,10 @@ func (qs *QueueService) Entries(ctx context.Context) (radio.Queue, error) {
qs.mu.Lock()
defer qs.mu.Unlock()

all := make([]radio.QueueEntry, len(qs.queue))
copy(all, qs.queue)
all := make([]radio.QueueEntry, 0, len(qs.queue))
for _, e := range qs.queue {
all = append(all, e.Copy())
}
return all, nil
}

Expand Down

0 comments on commit 3e765ac

Please sign in to comment.