github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/metricworker/sender_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package metricworker_test
     5  
     6  import (
     7  	"sync"
     8  	"time"
     9  
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/juju/testing"
    13  	coretesting "github.com/juju/juju/testing"
    14  	"github.com/juju/juju/worker/metricworker"
    15  )
    16  
    17  type SenderSuite struct {
    18  	testing.JujuConnSuite
    19  }
    20  
    21  var _ = gc.Suite(&SenderSuite{})
    22  
    23  func (s *SenderSuite) SetUpTest(c *gc.C) {
    24  	s.JujuConnSuite.SetUpTest(c)
    25  }
    26  
    27  // TestSend create 2 metrics, one sent and one not sent.
    28  // It confirms that one metric is sent.
    29  func (s *SenderSuite) TestSender(c *gc.C) {
    30  	notify := make(chan string)
    31  	cleanup := metricworker.PatchNotificationChannel(notify)
    32  	defer cleanup()
    33  	client := &mockClient{}
    34  	worker := metricworker.NewSender(client)
    35  	select {
    36  	case <-notify:
    37  	case <-time.After(coretesting.LongWait):
    38  		c.Fatalf("the cleanup function should have fired by now")
    39  	}
    40  	c.Assert(client.calls, gc.DeepEquals, []string{"SendMetrics"})
    41  	worker.Kill()
    42  	c.Assert(worker.Wait(), gc.IsNil)
    43  }
    44  
    45  type mockClient struct {
    46  	calls []string
    47  	lock  sync.RWMutex
    48  }
    49  
    50  func (m *mockClient) CleanupOldMetrics() error {
    51  	m.lock.Lock()
    52  	defer m.lock.Unlock()
    53  	m.calls = append(m.calls, "CleanupOldMetrics")
    54  	return nil
    55  }
    56  func (m *mockClient) SendMetrics() error {
    57  	m.lock.Lock()
    58  	defer m.lock.Unlock()
    59  	m.calls = append(m.calls, "SendMetrics")
    60  	return nil
    61  }