func TestClockUntilSleepFirst(t *testing.T) { c := clock.New(time.Time{}) zero := c.Zero() done := make(chan bool) go func() { zero := c.Zero() til := zero.Add(10 * time.Microsecond) c.Until(til) done <- true }() select { case <-done: t.Fatal("unexpected return from c.Until") case <-time.After(10 * time.Millisecond): } c.Set(zero.Add(9 * time.Microsecond)) select { case <-done: t.Fatal("unexpected return from c.Until") case <-time.After(10 * time.Millisecond): } c.Set(zero.Add(10 * time.Microsecond)) select { case <-done: case <-time.After(20 * time.Millisecond): t.Fatal("expected return from c.Until") } }
func TestClockUntilMultiple(t *testing.T) { c := clock.New(time.Time{}) done := make(chan bool, 10) go func() { til := c.Zero() for i := 0; i < 10; i++ { til = til.Add(10 * time.Microsecond) c.Until(til) done <- true } }() now := c.Zero() for i := 0; i < 5; i++ { now = now.Add(20 * time.Microsecond) c.Set(now) select { case <-done: case <-time.After(10 * time.Millisecond): t.Fatal("expected return from c.Until") } select { case <-done: case <-time.After(10 * time.Millisecond): t.Fatal("expected return from c.Until") } } }
func TestClockUntilMultipleGos(t *testing.T) { c := clock.New(time.Time{}) count := 10 done := make(chan bool, count) for i := 0; i < count; i++ { go func(i int) { c.Until(c.Zero().Add(time.Duration(i) * time.Second)) done <- true }(i) } now := c.Zero() for i := 0; i < count; i++ { now = now.Add(time.Duration(i) * time.Second) c.Set(now) select { case <-done: case <-time.After(20 * time.Millisecond): t.Fatalf("expected return from c.Until i: %d", i) } } }
// Helper test function for streamer func testStreamer( t *testing.T, name, script string, ) ( clock.Setter, *kapacitor.ExecutingTask, <-chan error, *kapacitor.TaskMaster, ) { if testing.Verbose() { wlog.SetLevel(wlog.DEBUG) } else { wlog.SetLevel(wlog.OFF) } //Create the task task, err := kapacitor.NewStreamer(name, script, dbrps) if err != nil { t.Fatal(err) } // Load test data dir, err := os.Getwd() if err != nil { t.Fatal(err) } data, err := os.Open(path.Join(dir, "data", name+".srpl")) if err != nil { t.Fatal(err) } // Use 1971 so that we don't get true negatives on Epoch 0 collisions c := clock.New(time.Date(1971, 1, 1, 0, 0, 0, 0, time.UTC)) r := kapacitor.NewReplay(c) // Create a new execution env tm := kapacitor.NewTaskMaster(logService) tm.HTTPDService = httpService tm.Open() //Start the task et, err := tm.StartTask(task) if err != nil { t.Fatal(err) } // Replay test data to executor stream, err := tm.Stream(name) if err != nil { t.Fatal(err) } replayErr := r.ReplayStream(data, stream, false, "s") t.Log(string(et.Task.Dot())) return r.Setter, et, replayErr, tm }
// Helper test function for batcher func testBatcher(t *testing.T, name, script string) (clock.Setter, *kapacitor.ExecutingTask, <-chan error, *kapacitor.TaskMaster) { if testing.Verbose() { wlog.SetLevel(wlog.DEBUG) } else { wlog.SetLevel(wlog.OFF) } // Create a new execution env tm := kapacitor.NewTaskMaster(logService) tm.HTTPDService = httpService tm.TaskStore = taskStore{} tm.Open() scope := tm.CreateTICKScope() // Create task task, err := kapacitor.NewTask(name, script, kapacitor.BatchTask, dbrps, 0, scope) if err != nil { t.Fatal(err) } // Load test data var allData []io.ReadCloser var data io.ReadCloser for i := 0; err == nil; { f := fmt.Sprintf("%s.%d.brpl", name, i) data, err = os.Open(path.Join("data", f)) if err == nil { allData = append(allData, data) i++ } } if len(allData) == 0 { t.Fatal("could not find any data files for", name) } // Use 1971 so that we don't get true negatives on Epoch 0 collisions c := clock.New(time.Date(1971, 1, 1, 0, 0, 0, 0, time.UTC)) r := kapacitor.NewReplay(c) //Start the task et, err := tm.StartTask(task) if err != nil { t.Fatal(err) } // Replay test data to executor batches := tm.BatchCollectors(name) replayErr := r.ReplayBatch(allData, batches, false) t.Log(string(et.Task.Dot())) return r.Setter, et, replayErr, tm }
func TestClockUntilStart(t *testing.T) { c := clock.New(time.Unix(0, 0)) zero := c.Zero() done := make(chan bool) go func() { zero := c.Zero() til := zero.Add(10 * time.Microsecond) c.Until(til) done <- true }() c.Set(zero.Add(10 * time.Microsecond)) select { case <-done: case <-time.After(10 * time.Millisecond): t.Fatal("expected return from c.Until") } }