github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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 "time" 8 9 gc "gopkg.in/check.v1" 10 11 "github.com/juju/juju/juju/testing" 12 coretesting "github.com/juju/juju/testing" 13 "github.com/juju/juju/worker/metricworker" 14 ) 15 16 type SenderSuite struct { 17 testing.JujuConnSuite 18 } 19 20 var _ = gc.Suite(&SenderSuite{}) 21 22 func (s *SenderSuite) SetUpTest(c *gc.C) { 23 s.JujuConnSuite.SetUpTest(c) 24 } 25 26 // TestSend create 2 metrics, one sent and one not sent. 27 // It confirms that one metric is sent. 28 func (s *SenderSuite) TestSender(c *gc.C) { 29 notify := make(chan string) 30 cleanup := metricworker.PatchNotificationChannel(notify) 31 defer cleanup() 32 client := &mockClient{} 33 worker := metricworker.NewSender(client) 34 select { 35 case <-notify: 36 case <-time.After(coretesting.LongWait): 37 c.Fatalf("the cleanup function should have fired by now") 38 } 39 c.Assert(client.calls, gc.DeepEquals, []string{"SendMetrics"}) 40 worker.Kill() 41 c.Assert(worker.Wait(), gc.IsNil) 42 } 43 44 type mockClient struct { 45 calls []string 46 } 47 48 func (m *mockClient) CleanupOldMetrics() error { 49 m.calls = append(m.calls, "CleanupOldMetrics") 50 return nil 51 } 52 func (m *mockClient) SendMetrics() error { 53 m.calls = append(m.calls, "SendMetrics") 54 return nil 55 }