Exemplo n.º 1
0
func BenchmarkAdd(b *testing.B) {

	c := New(time.Second)

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		c.Add(a)
	}

}
Exemplo n.º 2
0
func TestOnceEvery(t *testing.T) {
	once := New(time.Second)
	count := 0
	var countMu sync.Mutex

	go func() {
		for i := 0; i < 100; i++ {
			once.Do(func() {
				countMu.Lock()
				count++
				countMu.Unlock()
			})
		}
	}()

	time.Sleep(time.Second * 2)

	countMu.Lock()
	defer countMu.Unlock()

	if count != 2 {
		t.Errorf("function should be called two times, got '%d'", count)
	}

	once.Stop()

	defer func() {
		if err := recover(); err != nil {
			t.Errorf("Second stop should not panic: %s", err)
		}
	}()

	once.Stop()
}
Exemplo n.º 3
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()
}