github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/apiserver/metricsender/testing/mocksender.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testing
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/juju/state"
    10  
    11  	wireformat "github.com/juju/romulus/wireformat/metrics"
    12  	"github.com/juju/utils"
    13  )
    14  
    15  // MockSender implements the metric sender interface.
    16  type MockSender struct {
    17  	UnackedBatches map[string]struct{}
    18  	Data           [][]*wireformat.MetricBatch
    19  }
    20  
    21  // Send implements the Send interface.
    22  func (m *MockSender) Send(d []*wireformat.MetricBatch) (*wireformat.Response, error) {
    23  	m.Data = append(m.Data, d)
    24  	respUUID, err := utils.NewUUID()
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	var envResponses = make(wireformat.EnvironmentResponses)
    29  
    30  	for _, batch := range d {
    31  		if m.UnackedBatches != nil {
    32  			_, ok := m.UnackedBatches[fmt.Sprintf("%s/%s", batch.ModelUUID, batch.UUID)]
    33  			if ok {
    34  				continue
    35  			}
    36  		}
    37  		envResponses.Ack(batch.ModelUUID, batch.UUID)
    38  	}
    39  	return &wireformat.Response{
    40  		UUID:         respUUID.String(),
    41  		EnvResponses: envResponses,
    42  	}, nil
    43  }
    44  
    45  func (m *MockSender) IgnoreBatches(batches ...*state.MetricBatch) {
    46  	if m.UnackedBatches == nil {
    47  		m.UnackedBatches = make(map[string]struct{})
    48  	}
    49  	for _, batch := range batches {
    50  		m.UnackedBatches[fmt.Sprintf("%s/%s", batch.ModelUUID(), batch.UUID())] = struct{}{}
    51  	}
    52  }
    53  
    54  // ErrorSender implements the metric sender interface and is used
    55  // to return errors during testing
    56  type ErrorSender struct {
    57  	Err error
    58  }
    59  
    60  // Send implements the Send interface returning errors specified in the ErrorSender.
    61  func (e *ErrorSender) Send(d []*wireformat.MetricBatch) (*wireformat.Response, error) {
    62  	return &wireformat.Response{}, e.Err
    63  }