Exemplo n.º 1
0
func TestServe(t *testing.T) {
	sche := New(time.Second)
	sche.ErrorHandler = func(evt Event, err error) {
		t.Errorf("%d, %s", evt.Id, err)
	}
	go sche.Serve()
	n := time.Duration(time.Now().UnixNano())

	var wg sync.WaitGroup
	wg.Add(6)
	id := ai.Id()
	sche.Add(Event{
		Id:       id,
		Start:    n + time.Second,
		Interval: time.Second,
		// negative number means running for ever;
		// 0 or 1 is one time event;
		// other numbers show how many time will be executed.
		Iterate: 1,
		Task:    &_task{t, &wg},
	})
	id = ai.Id()
	sche.Add(Event{
		Id:       id,
		Start:    n + time.Second,
		Interval: time.Second,
		Iterate:  3,
		Task:     &_task{t, &wg},
	})
	id = ai.Id()
	sche.Add(Event{
		Id:       id,
		Start:    n + 2*time.Second,
		Interval: time.Second,
		Iterate:  1,
		Task:     &_task{t, &wg},
	})
	id = ai.Id()
	sche.Add(Event{
		Id:       id,
		Start:    n + 2*time.Second,
		Interval: time.Second,
		Iterate:  1,
		Task:     &_task{t, &wg},
	})
	wg.Wait()
}