github.com/CyCoreSystems/ari@v4.8.4+incompatible/ext/record/record_test.go (about) 1 package record 2 3 /* 4 func TestRecordTimeout(t *testing.T) { 5 RecordingStartTimeout = 100 * time.Millisecond 6 7 ctrl := gomock.NewController(t) 8 defer ctrl.Finish() 9 10 bus := mock.NewMockBus(ctrl) 11 startedSub := mock.NewMockSubscription(ctrl) 12 startedCh := make(chan ari.Event) 13 startedSub.EXPECT().Events().Return(startedCh) 14 startedSub.EXPECT().Cancel() 15 16 finishedSub := mock.NewMockSubscription(ctrl) 17 finishedCh := make(chan ari.Event) 18 finishedSub.EXPECT().Events().Return(finishedCh) 19 finishedSub.EXPECT().Cancel() 20 21 failedSub := mock.NewMockSubscription(ctrl) 22 failedCh := make(chan ari.Event) 23 failedSub.EXPECT().Events().Return(failedCh) 24 failedSub.EXPECT().Cancel() 25 26 bus.EXPECT().Subscribe(ari.Events.RecordingStarted).Return(startedSub) 27 bus.EXPECT().Subscribe(ari.Events.RecordingFinished).Return(finishedSub) 28 bus.EXPECT().Subscribe(ari.Events.RecordingFailed).Return(failedSub) 29 30 recorder := testutils.NewRecorder() 31 recorder.Append(ari.NewLiveRecordingHandle("rc1", &testRecording{"rc1", false}), nil) 32 33 rec := Record(bus, recorder, "name1", nil) 34 <-rec.Done() 35 36 err := rec.Err() 37 38 <-time.After(1 * time.Millisecond) 39 40 if !isTimeout(err) { 41 t.Errorf("Expected timeout, got '%v'", err) 42 } 43 44 if err == nil || err.Error() != "Timeout waiting for recording to start" { 45 t.Errorf("Expected timeout waiting for recording to start, got '%v'", err) 46 } 47 48 if rec.Status() != Failed { 49 t.Errorf("Expected recording status to be Timeout, was '%v'", rec.Status()) 50 } 51 52 } 53 54 func TestRecord(t *testing.T) { 55 56 ctrl := gomock.NewController(t) 57 defer ctrl.Finish() 58 59 bus := mock.NewMockBus(ctrl) 60 startedSub := mock.NewMockSubscription(ctrl) 61 startedCh := make(chan ari.Event) 62 startedSub.EXPECT().Events().MinTimes(1).Return(startedCh) 63 startedSub.EXPECT().Cancel() 64 65 finishedSub := mock.NewMockSubscription(ctrl) 66 finishedCh := make(chan ari.Event) 67 finishedSub.EXPECT().Events().MinTimes(1).Return(finishedCh) 68 finishedSub.EXPECT().Cancel() 69 70 failedSub := mock.NewMockSubscription(ctrl) 71 failedCh := make(chan ari.Event) 72 failedSub.EXPECT().Events().MinTimes(1).Return(failedCh) 73 failedSub.EXPECT().Cancel() 74 75 bus.EXPECT().Subscribe(ari.Events.RecordingStarted).Return(startedSub) 76 bus.EXPECT().Subscribe(ari.Events.RecordingFinished).Return(finishedSub) 77 bus.EXPECT().Subscribe(ari.Events.RecordingFailed).Return(failedSub) 78 79 recorder := testutils.NewRecorder() 80 recorder.Append(ari.NewLiveRecordingHandle("rc1", &testRecording{"rc1", false}), nil) 81 82 var rec *Recording 83 var err error 84 85 rec = Record(bus, recorder, "rc1", nil) 86 87 startedCh <- recordingStarted("rc1") 88 finishedCh <- recordingFinished("rc1") 89 90 <-rec.Done() 91 92 err = rec.Err() 93 94 if err != nil { 95 t.Errorf("Unexpected err: '%v'", err) 96 } 97 98 if rec.Status() != Finished { 99 t.Errorf("Expected recording status to be Finished, was '%v'", rec.Status()) 100 } 101 } 102 103 func TestRecordCancel(t *testing.T) { 104 105 ctrl := gomock.NewController(t) 106 defer ctrl.Finish() 107 108 bus := mock.NewMockBus(ctrl) 109 startedSub := mock.NewMockSubscription(ctrl) 110 startedCh := make(chan ari.Event) 111 startedSub.EXPECT().Events().Return(startedCh) 112 startedSub.EXPECT().Cancel() 113 114 finishedSub := mock.NewMockSubscription(ctrl) 115 finishedCh := make(chan ari.Event) 116 finishedSub.EXPECT().Events().Return(finishedCh) 117 finishedSub.EXPECT().Cancel() 118 119 failedSub := mock.NewMockSubscription(ctrl) 120 failedCh := make(chan ari.Event) 121 failedSub.EXPECT().Events().Return(failedCh) 122 failedSub.EXPECT().Cancel() 123 124 bus.EXPECT().Subscribe(ari.Events.RecordingStarted).Return(startedSub) 125 bus.EXPECT().Subscribe(ari.Events.RecordingFinished).Return(finishedSub) 126 bus.EXPECT().Subscribe(ari.Events.RecordingFailed).Return(failedSub) 127 128 recorder := testutils.NewRecorder() 129 recorder.Append(ari.NewLiveRecordingHandle("rc1", &testRecording{"rc1", false}), nil) 130 131 rec := Record(bus, recorder, "rc1", nil) 132 133 rec.Cancel() 134 135 <-rec.Done() 136 137 err := rec.Err() 138 if err != nil { 139 t.Errorf("Unexpected error: '%v'", err) 140 } 141 142 if rec.Status() != Canceled { 143 t.Errorf("Expected recording status to be Canceled, was '%v'", rec.Status()) 144 } 145 } 146 147 func TestRecordFailOnRecord(t *testing.T) { 148 149 ctrl := gomock.NewController(t) 150 defer ctrl.Finish() 151 152 bus := mock.NewMockBus(ctrl) 153 startedSub := mock.NewMockSubscription(ctrl) 154 startedSub.EXPECT().Cancel() 155 156 finishedSub := mock.NewMockSubscription(ctrl) 157 finishedSub.EXPECT().Cancel() 158 159 failedSub := mock.NewMockSubscription(ctrl) 160 failedSub.EXPECT().Cancel() 161 162 bus.EXPECT().Subscribe(ari.Events.RecordingStarted).Return(startedSub) 163 bus.EXPECT().Subscribe(ari.Events.RecordingFinished).Return(finishedSub) 164 bus.EXPECT().Subscribe(ari.Events.RecordingFailed).Return(failedSub) 165 166 recorder := testutils.NewRecorder() 167 recorder.Append(nil, errors.New("Dummy record error")) 168 169 rec := Record(bus, recorder, "rc1", nil) 170 171 <-rec.Done() 172 173 err := rec.Err() 174 175 if err == nil || err.Error() != "Dummy record error" { 176 t.Errorf("Expected error 'Dummy record error', got: '%v'", err) 177 } 178 179 if rec.Status() != Failed { 180 t.Errorf("Expected recording status to be Failed, was '%v'", rec.Status()) 181 } 182 } 183 184 func TestRecordFailEvent(t *testing.T) { 185 186 RecordingStartTimeout = 10 * time.Second 187 188 ctrl := gomock.NewController(t) 189 defer ctrl.Finish() 190 191 bus := mock.NewMockBus(ctrl) 192 193 recorder := testutils.NewRecorder() 194 recorder.Append(ari.NewLiveRecordingHandle("rc1", &testRecording{"rc1", false}), nil) 195 196 startedSub := mock.NewMockSubscription(ctrl) 197 startedCh := make(chan ari.Event) 198 startedSub.EXPECT().Events().Return(startedCh) 199 startedSub.EXPECT().Cancel() 200 201 finishedSub := mock.NewMockSubscription(ctrl) 202 finishedCh := make(chan ari.Event) 203 finishedSub.EXPECT().Events().Return(finishedCh) 204 finishedSub.EXPECT().Cancel() 205 206 failedSub := mock.NewMockSubscription(ctrl) 207 failedCh := make(chan ari.Event) 208 failedSub.EXPECT().Events().Return(failedCh) 209 failedSub.EXPECT().Cancel() 210 211 bus.EXPECT().Subscribe(ari.Events.RecordingStarted).Return(startedSub) 212 bus.EXPECT().Subscribe(ari.Events.RecordingFinished).Return(finishedSub) 213 bus.EXPECT().Subscribe(ari.Events.RecordingFailed).Return(failedSub) 214 215 rec := Record(bus, recorder, "rc1", nil) 216 217 failedCh <- recordingFailed("rc1") 218 219 <-rec.Done() 220 221 err := rec.Err() 222 223 if err == nil || err.Error() != "Recording failed: Dummy Failure Error" { 224 t.Errorf("Expected error 'Recording failed: Dummy Failure Error', got: '%v'", err) 225 } 226 227 if rec.Status() != Failed { 228 t.Errorf("Expected recording status to be Failed, was '%v'", rec.Status()) 229 } 230 } 231 232 type testRecording struct { 233 id string 234 failData bool 235 } 236 237 func (tr *testRecording) Get(name string) *ari.LiveRecordingHandle { 238 panic("not implemented") 239 } 240 241 func (tr *testRecording) Data(name string) (ari.LiveRecordingData, error) { 242 panic("not implemented") 243 } 244 245 func (tr *testRecording) Stop(name string) error { 246 panic("not implemented") 247 } 248 249 func (tr *testRecording) Pause(name string) error { 250 panic("not implemented") 251 } 252 253 func (tr *testRecording) Resume(name string) error { 254 panic("not implemented") 255 } 256 257 func (tr *testRecording) Mute(name string) error { 258 panic("not implemented") 259 } 260 261 func (tr *testRecording) Unmute(name string) error { 262 panic("not implemented") 263 } 264 265 func (tr *testRecording) Delete(name string) error { 266 panic("not implemented") 267 } 268 269 func (tr *testRecording) Scrap(name string) error { 270 panic("not implemented") 271 } 272 273 func isTimeout(err error) bool { 274 275 type timeout interface { 276 Timeout() bool 277 } 278 279 te, ok := err.(timeout) 280 return ok && te.Timeout() 281 } 282 283 var recordingStarted = func(id string) ari.Event { 284 return &ari.RecordingStarted{ 285 EventData: ari.EventData{ 286 Message: ari.Message{ 287 Type: "RecordingStarted", 288 }, 289 }, 290 Recording: ari.LiveRecordingData{ 291 Name: id, 292 }, 293 } 294 } 295 296 var recordingFinished = func(id string) ari.Event { 297 return &ari.RecordingFinished{ 298 EventData: ari.EventData{ 299 Message: ari.Message{ 300 Type: "RecordingFinished", 301 }, 302 }, 303 Recording: ari.LiveRecordingData{ 304 Name: id, 305 }, 306 } 307 } 308 309 var recordingFailed = func(id string) ari.Event { 310 return &ari.RecordingFailed{ 311 EventData: ari.EventData{ 312 Message: ari.Message{ 313 Type: "RecordingFailed", 314 }, 315 }, 316 Recording: ari.LiveRecordingData{ 317 Name: id, 318 Cause: "Dummy Failure Error", 319 }, 320 } 321 } 322 323 */