github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/worker/uniter/metrics_test.go (about)

     1  // Copyright 2012-2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package uniter_test
     5  
     6  import (
     7  	"time"
     8  
     9  	gc "gopkg.in/check.v1"
    10  
    11  	coretesting "github.com/juju/juju/testing"
    12  	"github.com/juju/juju/worker/uniter"
    13  )
    14  
    15  type CollectMetricsTimerSuite struct{}
    16  
    17  var _ = gc.Suite(&CollectMetricsTimerSuite{})
    18  
    19  func (*CollectMetricsTimerSuite) TestTimer(c *gc.C) {
    20  	now := time.Now()
    21  	defaultInterval := coretesting.ShortWait / 5
    22  	testCases := []struct {
    23  		about        string
    24  		now          time.Time
    25  		lastRun      time.Time
    26  		interval     time.Duration
    27  		expectSignal bool
    28  	}{{
    29  		"Timer firing after delay.",
    30  		now,
    31  		now.Add(-defaultInterval / 2),
    32  		defaultInterval,
    33  		true,
    34  	}, {
    35  		"Timer firing the first time.",
    36  		now,
    37  		time.Unix(0, 0),
    38  		defaultInterval,
    39  		true,
    40  	}, {
    41  		"Timer not firing soon.",
    42  		now,
    43  		now,
    44  		coretesting.ShortWait * 2,
    45  		false,
    46  	}}
    47  
    48  	for i, t := range testCases {
    49  		c.Logf("running test %d", i)
    50  		sig := (*uniter.ActiveMetricsTimer)(t.now, t.lastRun, t.interval)
    51  		select {
    52  		case <-sig:
    53  			if !t.expectSignal {
    54  				c.Errorf("not expecting a signal")
    55  			}
    56  		case <-time.After(coretesting.ShortWait):
    57  			if t.expectSignal {
    58  				c.Errorf("expected a signal")
    59  			}
    60  		}
    61  	}
    62  }