Пример #1
0
func (s *TickerTestSuite) TestSleepTime60s(t *check.C) {
	// Fri Sep 27 18:11:37.385120 -0700 PDT 2013 =
	now := int64(1380330697385120263)

	c := make(chan time.Time)
	et := ticker.NewEvenTicker(60, sleep)
	et.Add(c)
	go et.Run(now)
	<-c
	got := slept.Nanoseconds()
	expect := int64(614879744 + (22 * time.Second))
	if got != expect {
		t.Errorf("Got %d, expected %d\n", got, expect)
	}
	et.Stop()
}
Пример #2
0
func (s *TickerTestSuite) TestSleepTime2s(t *check.C) {
	/*
	 * To sync at intervals, we must first sleep N number of nanoseconds
	 * until the next interval.  So we specify the curren time (now) in
	 * nanaseconds, and an interval time (2), and then we know how long
	 * the syncer should sleep to wait from now until the next interval
	 * time.
	 */

	// Fri Sep 27 18:11:37.385120 -0700 PDT 2013 =
	now := int64(1380330697385120263)

	// The next 2s interval, 18:11:38.000, is 0.61488 seconds away,
	// so that's how long syncer should tell our sleep func to sleep.
	c := make(chan time.Time)
	et := ticker.NewEvenTicker(2, sleep)
	et.Add(c)

	// Run ticker then wait for first tick.
	go et.Run(now)
	<-c

	// Check how long ticker slept.
	got := slept.Nanoseconds()
	expect := int64(614879744)
	if got != expect {
		t.Errorf("Got %d, expected %d\n", got, expect)
	}

	// ETA (time to next tick) should be reported as the same: 0.614880s
	d := et.ETA(now)
	if d < 0.614 || d > 0.615 {
		t.Errorf("Got ETA %f, expected 0.614880\n", d)
	}

	et.Stop()
}
Пример #3
0
func (s *TickerTestSuite) TestTickerTime(t *check.C) {
	/*
	 * The ticker returned by the syncer should tick at this given interval,
	 * 2s in this case.  We test this by ensuring that the current time at
	 * a couple of ticks is divisible by 2, and that the fractional seconds
	 * part is < ~1 millisecond, e.g.:
	 *   00:00:02.000123456  OK
	 *   00:00:04.000123456  OK
	 *   00:00:06.001987654  BAD
	 * This may fail on slow test machines.
	 */
	c1 := make(chan time.Time)
	c2 := make(chan time.Time)
	et := ticker.NewEvenTicker(2, time.Sleep)
	et.Add(c1)
	et.Add(c2)
	go et.Run(time.Now().UnixNano())

	// Get 2 ticks but only on c1.  "Time waits for nobody" and neither does
	// the ticker, so c2 not receiving should not affect c1.
	maxOffBy := 900000 // 900,000 ns = ~1ms
	var lastTick time.Time
	for i := 0; i < 2; i++ {
		select {
		case tick := <-c1:
			sec := tick.Second()
			if sec%2 > 0 {
				t.Errorf("Tick %d not 2s interval: %d", i, sec)
			}
			nano := tick.Nanosecond()
			if nano >= maxOffBy {
				t.Errorf("Tick %d failed: %d >= %d", i, nano, maxOffBy)
			}
			lastTick = tick
		}
	}

	// Remove c1 and recv on c2 now.  Even though c2 missed previous 2 ticks,
	// it should be able to start receiving new ticks.  By contrast, c1 should
	// not receive the tick.
	et.Remove(c1)
	timeout := time.After(2500 * time.Millisecond)
	var c2Tick time.Time
TICK_LOOP:
	for {
		select {
		case tick := <-c2:
			if !c2Tick.IsZero() {
				t.Error("c2 gets only 1 tick")
			} else {
				c2Tick = tick
			}
		case <-c1:
			t.Error("c1 does not get current tick")
		case <-timeout:
			break TICK_LOOP
		}
	}

	if c2Tick == lastTick || c2Tick.Before(lastTick) {
		t.Error("c2 gets current tick")
	}

	et.Stop()
}