github.com/CyCoreSystems/ari@v4.8.4+incompatible/ext/play/sequence_test.go (about) 1 package play 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 8 "time" 9 10 "github.com/CyCoreSystems/ari" 11 "github.com/CyCoreSystems/ari/client/arimocks" 12 "github.com/stretchr/testify/mock" 13 ) 14 15 type sequenceTest struct { 16 playbackStartedChan chan ari.Event 17 playbackStarted *arimocks.Subscription 18 19 playbackEndChan chan ari.Event 20 playbackEnd *arimocks.Subscription 21 22 playback *arimocks.Playback 23 24 key *ari.Key 25 } 26 27 func (p *sequenceTest) Setup(handle string) { 28 29 p.playbackStarted = &arimocks.Subscription{} 30 p.playbackEnd = &arimocks.Subscription{} 31 p.playback = &arimocks.Playback{} 32 33 p.key = ari.NewKey(ari.PlaybackKey, handle) 34 35 p.playbackStartedChan = make(chan ari.Event) 36 p.playbackStarted.On("Events").Return((<-chan ari.Event)(p.playbackStartedChan)) 37 38 p.playbackStarted.On("Cancel").Times(1).Return(nil) 39 p.playback.On("Subscribe", p.key, ari.Events.PlaybackStarted).Return(p.playbackStarted) 40 p.playback.On("Stop", p.key).Times(1).Return(nil) 41 42 p.playbackEndChan = make(chan ari.Event) 43 p.playbackEnd.On("Events").Return((<-chan ari.Event)(p.playbackEndChan)) 44 p.playbackEnd.On("Cancel").Times(1).Return(nil) 45 p.playback.On("Subscribe", p.key, ari.Events.PlaybackFinished).Return(p.playbackEnd) 46 } 47 48 func TestSequence(t *testing.T) { 49 t.Run("noItems", testSequenceNoItems) 50 t.Run("someItemsTimeoutStart", testSequenceSomeItemsTimeoutStart) 51 t.Run("someItems", testSequenceSomeItems) 52 t.Run("someItemsStopEarly", testSequenceSomeItemsStopEarly) 53 t.Run("someItemsCancelEarly", testSequenceSomeItemsCancelEarly) 54 t.Run("someItemsStagePlayFailure", testSequenceSomeItemsStagePlayFailure) 55 } 56 57 func testSequenceNoItems(t *testing.T) { 58 player := &arimocks.Player{} 59 ctx, cancel := context.WithCancel(context.Background()) 60 defer cancel() 61 62 seq := newSequence(newPlaySession(NewDefaultOptions())) 63 64 seq.Play(ctx, player) 65 66 player.AssertNotCalled(t, "StagePlay") 67 } 68 69 func testSequenceSomeItemsTimeoutStart(t *testing.T) { 70 player := &arimocks.Player{} 71 ctx, cancel := context.WithCancel(context.Background()) 72 defer cancel() 73 74 var s sequenceTest 75 s.Setup("ph1") 76 77 var s2 sequenceTest 78 s2.Setup("ph2") 79 80 ph1 := ari.NewPlaybackHandle(ari.NewKey(ari.PlaybackKey, "ph1"), s.playback, nil) 81 ph2 := ari.NewPlaybackHandle(ari.NewKey(ari.PlaybackKey, "ph2"), s2.playback, nil) 82 83 player.On("StagePlay", mock.Anything, "sound:1").Return(ph1, nil) 84 player.On("StagePlay", mock.Anything, "sound:2").Return(ph2, nil) 85 86 opts := NewDefaultOptions() 87 opts.uriList.Add("sound:1") 88 opts.uriList.Add("sound:2") 89 opts.playbackStartTimeout = 10 * time.Millisecond 90 seq := newSequence(newPlaySession(opts)) 91 92 seq.Play(ctx, player) 93 94 player.AssertCalled(t, "StagePlay", mock.Anything, "sound:1") 95 player.AssertNotCalled(t, "StagePlay", mock.Anything, "sound:2") 96 97 if err := seq.s.result.Error; err == nil || err.Error() != "failure in playback: timeout waiting for playback to start" { 98 t.Errorf("Expected error: %s, got %v", "failure in playback: timeout waiting for playback to start", err) 99 } 100 if seq.s.result.Status != Timeout { 101 t.Errorf("Expected status '%v', got '%v'", Timeout, seq.s.result.Status) 102 } 103 } 104 105 func testSequenceSomeItems(t *testing.T) { 106 player := &arimocks.Player{} 107 ctx, cancel := context.WithCancel(context.Background()) 108 defer cancel() 109 110 var s sequenceTest 111 s.Setup("ph1") 112 113 var s2 sequenceTest 114 s2.Setup("ph2") 115 116 ph1 := ari.NewPlaybackHandle(ari.NewKey(ari.PlaybackKey, "ph1"), s.playback, nil) 117 ph2 := ari.NewPlaybackHandle(ari.NewKey(ari.PlaybackKey, "ph2"), s2.playback, nil) 118 119 player.On("StagePlay", mock.Anything, "sound:1").Return(ph1, nil) 120 player.On("StagePlay", mock.Anything, "sound:2").Return(ph2, nil) 121 122 opts := NewDefaultOptions() 123 opts.uriList.Add("sound:1") 124 opts.uriList.Add("sound:2") 125 seq := newSequence(newPlaySession(opts)) 126 127 go func() { 128 s.playbackStartedChan <- &ari.PlaybackStarted{} 129 <-time.After(20 * time.Millisecond) 130 s.playbackEndChan <- &ari.PlaybackFinished{} 131 132 <-time.After(20 * time.Millisecond) 133 134 s2.playbackStartedChan <- &ari.PlaybackStarted{} 135 <-time.After(20 * time.Millisecond) 136 s2.playbackEndChan <- &ari.PlaybackFinished{} 137 }() 138 139 seq.Play(ctx, player) 140 141 player.AssertCalled(t, "StagePlay", mock.Anything, "sound:1") 142 player.AssertCalled(t, "StagePlay", mock.Anything, "sound:2") 143 144 if seq.s.result.Error != nil { 145 t.Errorf("Unexpected error: %v", seq.s.result.Error) 146 } 147 if seq.s.result.Status != Finished { 148 t.Errorf("Expected status '%v', got '%v'", Finished, seq.s.result.Status) 149 } 150 } 151 152 func testSequenceSomeItemsCancelEarly(t *testing.T) { 153 player := &arimocks.Player{} 154 ctx, cancel := context.WithCancel(context.Background()) 155 defer cancel() 156 157 var s sequenceTest 158 s.Setup("ph1") 159 160 var s2 sequenceTest 161 s2.Setup("ph2") 162 163 ph1 := ari.NewPlaybackHandle(ari.NewKey(ari.PlaybackKey, "ph1"), s.playback, nil) 164 ph2 := ari.NewPlaybackHandle(ari.NewKey(ari.PlaybackKey, "ph2"), s2.playback, nil) 165 166 player.On("StagePlay", mock.Anything, "sound:1").Return(ph1, nil) 167 player.On("StagePlay", mock.Anything, "sound:2").Return(ph2, nil) 168 169 opts := NewDefaultOptions() 170 opts.uriList.Add("sound:1") 171 opts.uriList.Add("sound:2") 172 seq := newSequence(newPlaySession(opts)) 173 174 go func() { 175 s.playbackStartedChan <- &ari.PlaybackStarted{} 176 <-time.After(20 * time.Millisecond) 177 s.playbackEndChan <- &ari.PlaybackFinished{} 178 179 <-time.After(20 * time.Millisecond) 180 181 s2.playbackStartedChan <- &ari.PlaybackStarted{} 182 <-time.After(20 * time.Millisecond) 183 184 cancel() 185 186 }() 187 188 seq.Play(ctx, player) 189 190 player.AssertCalled(t, "StagePlay", mock.Anything, "sound:1") 191 player.AssertCalled(t, "StagePlay", mock.Anything, "sound:2") 192 193 if seq.s.result.Error != nil { 194 t.Errorf("Unexpected error: %v", seq.s.result.Error) 195 } 196 if seq.s.result.Status != Cancelled { 197 t.Errorf("Expected status '%v', got '%v'", Cancelled, seq.s.result.Status) 198 } 199 } 200 201 func testSequenceSomeItemsStopEarly(t *testing.T) { 202 player := &arimocks.Player{} 203 ctx, cancel := context.WithCancel(context.Background()) 204 defer cancel() 205 206 var s sequenceTest 207 s.Setup("ph1") 208 209 var s2 sequenceTest 210 s2.Setup("ph2") 211 212 ph1 := ari.NewPlaybackHandle(ari.NewKey(ari.PlaybackKey, "ph1"), s.playback, nil) 213 ph2 := ari.NewPlaybackHandle(ari.NewKey(ari.PlaybackKey, "ph2"), s2.playback, nil) 214 215 player.On("StagePlay", mock.Anything, "sound:1").Return(ph1, nil) 216 player.On("StagePlay", mock.Anything, "sound:2").Return(ph2, nil) 217 218 opts := NewDefaultOptions() 219 opts.uriList.Add("sound:1") 220 opts.uriList.Add("sound:2") 221 seq := newSequence(newPlaySession(opts)) 222 223 go func() { 224 s.playbackStartedChan <- &ari.PlaybackStarted{} 225 <-time.After(20 * time.Millisecond) 226 s.playbackEndChan <- &ari.PlaybackFinished{} 227 228 <-time.After(20 * time.Millisecond) 229 230 s2.playbackStartedChan <- &ari.PlaybackStarted{} 231 <-time.After(20 * time.Millisecond) 232 233 seq.Stop() 234 235 }() 236 237 seq.Play(ctx, player) 238 239 player.AssertCalled(t, "StagePlay", mock.Anything, "sound:1") 240 player.AssertCalled(t, "StagePlay", mock.Anything, "sound:2") 241 242 if seq.s.result.Error != nil { 243 t.Errorf("Unexpected error: %v", seq.s.result.Error) 244 } 245 if seq.s.result.Status != Cancelled { 246 t.Errorf("Expected status '%v', got '%v'", Cancelled, seq.s.result.Status) 247 } 248 } 249 250 func testSequenceSomeItemsStagePlayFailure(t *testing.T) { 251 player := &arimocks.Player{} 252 ctx, cancel := context.WithCancel(context.Background()) 253 defer cancel() 254 255 var s sequenceTest 256 s.Setup("ph1") 257 258 var s2 sequenceTest 259 s2.Setup("ph2") 260 261 ph1 := ari.NewPlaybackHandle(ari.NewKey(ari.PlaybackKey, "ph1"), s.playback, nil) 262 263 player.On("StagePlay", mock.Anything, "sound:1").Return(ph1, nil) 264 player.On("StagePlay", mock.Anything, "sound:2").Return(nil, errors.New("unknown error")) 265 266 opts := NewDefaultOptions() 267 opts.uriList.Add("sound:1") 268 opts.uriList.Add("sound:2") 269 seq := newSequence(newPlaySession(opts)) 270 271 go func() { 272 s.playbackStartedChan <- &ari.PlaybackStarted{} 273 <-time.After(20 * time.Millisecond) 274 s.playbackEndChan <- &ari.PlaybackFinished{} 275 276 <-time.After(20 * time.Millisecond) 277 }() 278 279 seq.Play(ctx, player) 280 281 player.AssertCalled(t, "StagePlay", mock.Anything, "sound:1") 282 player.AssertCalled(t, "StagePlay", mock.Anything, "sound:2") 283 284 if err := seq.s.result.Error; err == nil || err.Error() != "failed to stage playback: unknown error" { 285 t.Errorf("Expected error: %v, got %v", "failed to stage playback: unknown error", err) 286 } 287 if seq.s.result.Status != Failed { 288 t.Errorf("Expected status '%v', got '%v'", Failed, seq.s.result.Status) 289 } 290 }