func TestRecorderStart(t *testing.T) { assert := wcg.NewAssert(t) receiver := make(chan []Record) recorder := NewRecorder((<-chan []Record)(receiver)) interval := time.Duration(30) * time.Millisecond wait := 10 go recorder.Start(interval) now := time.Now() r1 := NewDummyRecord("r1") // cancel before starting r1.startAt = now r1.endAt = now.Add(time.Duration(5 * time.Second)) receiver <- []Record{r1} err := util.WaitFor(func() bool { return len(recorder.controls) == 1 }, wait) assert.Nil(err, "check r1 in controls.") ctrl := recorder.controls[r1.Key()] err = util.WaitFor(func() bool { return r1.done == true }, wait) assert.Nil(err, "check r1 has been done.") assert.EqInt(int(RSSucceeded), int(ctrl.state), "Record state should be RSSucceeded") recorder.Stop() }
func TestRecorderStart_CancelWhileRecording(t *testing.T) { assert := wcg.NewAssert(t) receiver := make(chan []Record) recorder := NewRecorder((<-chan []Record)(receiver)) interval := time.Duration(30) * time.Millisecond wait := 5 go recorder.Start(interval) now := time.Now() r1 := NewDummyRecord("r1") // cancel before starting r1.startAt = now r1.endAt = now.Add(time.Duration(30 * time.Minute)) receiver <- []Record{r1} err := util.WaitFor(func() bool { return len(recorder.controls) == 1 }, wait) assert.Nil(err, "check r1 in controls.") assert.EqStr(r1.Key(), recorder.controls[r1.Key()].record.Key(), "check r1 in controls.") err = util.WaitFor(func() bool { return recorder.controls[r1.Key()].state == RSRecording }, wait) assert.Nil(err, "check r1 in RSRecording state.") receiver <- []Record{} err = util.WaitFor(func() bool { return len(recorder.controls) == 0 }, wait) assert.Nil(err, "check r1 removed from controls.") recorder.Stop() }
func TestRecorderStart_CancelBeforeStart(t *testing.T) { assert := wcg.NewAssert(t) receiver := make(chan []Record) recorder := NewRecorder((<-chan []Record)(receiver)) interval := time.Duration(30) * time.Millisecond wait := 10 go recorder.Start(interval) now := time.Now() r1 := NewDummyRecord("r1") // cancel before starting r1.startAt = now.Add(time.Duration(30 * time.Minute)) r1.endAt = now.Add(time.Duration(60 * time.Minute)) // r2 := NewDummyRecord("r2") // cancel after starting // r3 := NewDummyRecord("r3") // succeeded // r4 := NewDummyRecord("r4") // failed receiver <- []Record{r1} err := util.WaitFor(func() bool { return len(recorder.controls) == 1 }, wait) assert.Nil(err, "check r1 in controls.") assert.EqStr(r1.Key(), recorder.controls[r1.Key()].record.Key(), "check r1 in controls.") receiver <- []Record{} err = util.WaitFor(func() bool { return len(recorder.controls) == 0 }, wait) assert.Nil(err, "check r1 removed from controls.") recorder.Stop() }
func TestPt1Record_Start(t *testing.T) { assert := wcg.NewAssert(t) now := time.Now() tvrecord := models.NewTvRecord( "Title", "Category", now, now.Add(time.Duration(5)*time.Second), "cid", "sid", "uid", ) util.WithTempDir(func(dir string) { record := NewPt1Record(tvrecord, genPt1Config(dir)) err := record.Start() assert.Nil(err, "Pt1Record started.") time.Sleep(1 * time.Second) assert.Ok(record.IsRunning(), "Pt1Record is running.") err = record.Stop() assert.Nil(err, "Stop record.") err = util.WaitFor(func() bool { return record.IsRunning() == false }, 10) assert.Nil(err, "Pt1Record has stopped.") content, _ := ioutil.ReadFile(record.filepath) lines := bytes.Split(content, []byte("\n")) assert.EqStr("OK", string(lines[len(lines)-1]), "Check the mock record content.") }) }
func TestRecorderUpcomming(t *testing.T) { assert := wcg.NewAssert(t) receiver := make(chan []Record) recorder := NewRecorder((<-chan []Record)(receiver)) interval := time.Duration(30) * time.Millisecond wait := 10 go recorder.Start(interval) r1 := NewDummyRecord("r1") r1.startAt = time.Now().Add(time.Duration(2 * time.Hour)) r1.endAt = r1.startAt.Add(time.Duration(10 * time.Minute)) r2 := NewDummyRecord("r2") r2.startAt = time.Now().Add(time.Duration(4 * time.Hour)) r2.endAt = r2.startAt.Add(time.Duration(10 * time.Minute)) r3 := NewDummyRecord("r3") r3.startAt = time.Now().Add(time.Duration(8 * time.Hour)) r3.endAt = r3.startAt.Add(time.Duration(10 * time.Minute)) receiver <- []Record{r1, r2, r3} err := util.WaitFor(func() bool { return len(recorder.controls) == 3 }, wait) assert.Nil(err, "check all records in controls.") upcome := recorder.Upcomming() assert.Ok(r1.startAt.Equal(upcome), "Upcomming should be r2") recorder.Stop() }