2017-12-21 07:14
浏览 42


I use this statsd package to send metrics to our statsd server. To initialize the client, I call a metrics.Setup() in my main which does the init. This package looks like this:


package metrics

import (


// Client can be used to send stats to
var Client StatsdAccess

// Setup initialises metrics gathering
func Setup() {
    if Client == nil {
        prefix := fmt.Sprintf("app.%s", logging.GetHost())
        std, err := statsd.NewBufferedClient(fmt.Sprintf("localhost:1234", prefix, 0, 0)
        if err != nil {
            logrus.Errorf("unable to dial the statsd host: %q", err)
        Client = std

// StatsdAccess is used as interface to statsd functions
type StatsdAccess interface {
    Inc(stat string, value int64, rate float32) error
    Gauge(stat string, value int64, rate float32) error
    Timing(stat string, delta int64, rate float32) error

From this moment on, the other package send metrics though this global Client by doing this: metrics.Client.Inc("some.counter", 1, 1.0). This works fine, but now I have a problem with my test files. They now fail when a package actually uses the metrics package to send metrics. This is obvious, because the metrics package has not been initialized, etc. So my question -I think- is: how can I mock the statsd client in my test files?

图片转代码服务由CSDN问答提供 功能建议

我使用 statsd软件包,用于将指标发送到我们的 statsd服务器。 要初始化客户端,我在执行初始化的主程序中调用 metrics.Setup()。 该软件包如下所示:


“  fmt“ 
” github.com/cactus/go-statsd-client/statsd"

 //安装程序初始化 指标收集
func Setup(){
如果Client == nil {
前缀:= fmt.Sprintf(“ app。%s”,logging.GetHost())
 std,err:= statsd.NewBufferedClient(fmt  .Sprintf(“ localhost:1234”,前缀,0,0)
如果错误!= nil {
} \  n Client = std 
 // StatsdAccess用作statsd函数的接口
type StatsdAccess接口{
 Inc(stat字符串,值int64,rate float32)错误
 Gauge(状态字符串, 值int64,速率float32)错误

从此刻开始,另一个包发送 通过该全球客户的指标 这样做: metrics.Client.Inc(“ some.counter”,1,1.0)。 这可以正常工作,但是现在我的测试文件有问题。 现在,当程序包实际使用 metrics程序包发送度量标准时,它们将失败。 这很明显,因为未对指标包进行初始化等。因此,我的问题-我认为-是:如何在测试文件中模拟 statsd客户端 < / DIV>

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • donglv9116 2017-12-21 17:45

    Many of the project I work on use statsd and have teetered between just leaving the calls in the tests, because they are so lightweight, and programming to a metrics interface (as you have already done StasdAccess).

    Since the interface is already initialized, you should be able to use the interface to break the configuration dependency in your code, and provide a test implementation to use in your tests:


    // testing.go
    type StubStatsd struct {}
    func (s StubStatsd) Inc(stat string, value int64, rate float32) error {
      return nil
    func (s StubStatsd) Gauge(...
    func (s StubStatsd) Timing(...

    Now when your tests would like to call a method or initialize a component that requires a StatsdAccess parameter it could use the testing stub to meet the requirements

    func TestSomething_NeedsStatsd(t *testing.T) {
         statsdStub := StubStatsd{}
    解决 无用
    打赏 举报

相关推荐 更多相似问题