github.com/msales/pkg/v3@v3.24.0/stats/statsd_internal_test.go (about)

     1  package stats
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/cactus/go-statsd-client/statsd"
     8  	"github.com/cactus/go-statsd-client/statsd/statsdtest"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestNewStatsd(t *testing.T) {
    13  	s, err := NewStatsd("127.0.0.1:1234", "test")
    14  	assert.NoError(t, err)
    15  	defer s.Close()
    16  
    17  	assert.IsType(t, &Statsd{}, s)
    18  
    19  	_, err = NewStatsd("127.0", "test")
    20  	assert.Error(t, err)
    21  }
    22  
    23  func TestStatsd_Inc(t *testing.T) {
    24  	sender := statsdtest.NewRecordingSender()
    25  	client, err := statsd.NewClientWithSender(sender, "test")
    26  	assert.NoError(t, err)
    27  
    28  	s := &Statsd{
    29  		client: client,
    30  	}
    31  
    32  	s.Inc("test", 2, 1.0, "test", "test")
    33  
    34  	sent := sender.GetSent()
    35  	assert.Len(t, sent, 1)
    36  	assert.Equal(t, "test.test,test=test", sent[0].Stat)
    37  	assert.Equal(t, "2", sent[0].Value)
    38  }
    39  
    40  func TestStatsd_Dec(t *testing.T) {
    41  	sender := statsdtest.NewRecordingSender()
    42  	client, err := statsd.NewClientWithSender(sender, "test")
    43  	assert.NoError(t, err)
    44  
    45  	s := &Statsd{
    46  		client: client,
    47  	}
    48  
    49  	s.Dec("test", 2, 1.0, "test", "test")
    50  
    51  	sent := sender.GetSent()
    52  	assert.Len(t, sent, 1)
    53  	assert.Equal(t, "test.test,test=test", sent[0].Stat)
    54  	assert.Equal(t, "-2", sent[0].Value)
    55  }
    56  
    57  func TestStatsd_Gauge(t *testing.T) {
    58  	sender := statsdtest.NewRecordingSender()
    59  	client, err := statsd.NewClientWithSender(sender, "test")
    60  	assert.NoError(t, err)
    61  
    62  	s := &Statsd{
    63  		client: client,
    64  	}
    65  
    66  	s.Gauge("test", 2.0, 1.0, "test", "test")
    67  
    68  	sent := sender.GetSent()
    69  	assert.Len(t, sent, 1)
    70  	assert.Equal(t, "test.test,test=test", sent[0].Stat)
    71  	assert.Equal(t, "2", sent[0].Value)
    72  }
    73  
    74  func TestStatsd_Timing(t *testing.T) {
    75  	sender := statsdtest.NewRecordingSender()
    76  	client, err := statsd.NewClientWithSender(sender, "test")
    77  	assert.NoError(t, err)
    78  
    79  	s := &Statsd{
    80  		client: client,
    81  	}
    82  
    83  	s.Timing("test", time.Second, 1.0, "test", "test")
    84  
    85  	sent := sender.GetSent()
    86  	assert.Len(t, sent, 1)
    87  	assert.Equal(t, "test.test,test=test", sent[0].Stat)
    88  	assert.Equal(t, "1000", sent[0].Value)
    89  }
    90  
    91  func TestNewBufferedStatsd(t *testing.T) {
    92  	s, err := NewBufferedStatsd("127.0.0.1:1234", "test", WithFlushInterval(time.Second), WithFlushBytes(1))
    93  	assert.NoError(t, err)
    94  	defer s.Close()
    95  
    96  	assert.IsType(t, &BufferedStatsd{}, s)
    97  	assert.Equal(t, time.Second, s.flushInterval)
    98  	assert.Equal(t, 1, s.flushBytes)
    99  
   100  	_, err = NewBufferedStatsd("127.0", "test")
   101  	assert.Error(t, err)
   102  }
   103  
   104  func TestBufferedStatsd_Inc(t *testing.T) {
   105  	sender := statsdtest.NewRecordingSender()
   106  	client, err := statsd.NewClientWithSender(sender, "test")
   107  	assert.NoError(t, err)
   108  
   109  	s := &BufferedStatsd{
   110  		client: client,
   111  	}
   112  
   113  	s.Inc("test", 2, 1.0, "test", "test")
   114  
   115  	sent := sender.GetSent()
   116  	assert.Len(t, sent, 1)
   117  	assert.Equal(t, "test.test,test=test", sent[0].Stat)
   118  	assert.Equal(t, "2", sent[0].Value)
   119  }
   120  
   121  func TestBufferedStatsd_Dec(t *testing.T) {
   122  	sender := statsdtest.NewRecordingSender()
   123  	client, err := statsd.NewClientWithSender(sender, "test")
   124  	assert.NoError(t, err)
   125  
   126  	s := &BufferedStatsd{
   127  		client: client,
   128  	}
   129  
   130  	s.Dec("test", 2, 1.0, "test", "test")
   131  
   132  	sent := sender.GetSent()
   133  	assert.Len(t, sent, 1)
   134  	assert.Equal(t, "test.test,test=test", sent[0].Stat)
   135  	assert.Equal(t, "-2", sent[0].Value)
   136  }
   137  
   138  func TestBufferedStatsd_Gauge(t *testing.T) {
   139  	sender := statsdtest.NewRecordingSender()
   140  	client, err := statsd.NewClientWithSender(sender, "test")
   141  	assert.NoError(t, err)
   142  
   143  	s := &BufferedStatsd{
   144  		client: client,
   145  	}
   146  
   147  	s.Gauge("test", 2.0, 1.0, "test", "test")
   148  
   149  	sent := sender.GetSent()
   150  	assert.Len(t, sent, 1)
   151  	assert.Equal(t, "test.test,test=test", sent[0].Stat)
   152  	assert.Equal(t, "2", sent[0].Value)
   153  }
   154  
   155  func TestBufferedStatsd_Timing(t *testing.T) {
   156  	sender := statsdtest.NewRecordingSender()
   157  	client, err := statsd.NewClientWithSender(sender, "test")
   158  	assert.NoError(t, err)
   159  
   160  	s := &BufferedStatsd{
   161  		client: client,
   162  	}
   163  
   164  	s.Timing("test", time.Second, 1.0, "test", "test")
   165  
   166  	sent := sender.GetSent()
   167  	assert.Len(t, sent, 1)
   168  	assert.Equal(t, "test.test,test=test", sent[0].Stat)
   169  	assert.Equal(t, "1000", sent[0].Value)
   170  }
   171  
   172  func TestFormatStatsdTags(t *testing.T) {
   173  	tags := []interface{}{
   174  		"bool", true,
   175  		"float32", float32(1.23),
   176  		"float64", float64(1.23),
   177  		"int", 1,
   178  		"int8", int8(2),
   179  		"int16", int16(2),
   180  		"int32", int32(2),
   181  		"int64", int64(2),
   182  		"uint", uint(1),
   183  		"uint8", uint8(2),
   184  		"uint16", uint16(2),
   185  		"uint32", uint32(2),
   186  		"uint64", uint64(2),
   187  		"test", "test",
   188  		"foo", "bar",
   189  		"test", "baz",
   190  		"struct", struct {
   191  			Name string
   192  		}{Name: "blah"},
   193  	}
   194  
   195  	assert.Equal(t, "", formatStatsdTags(nil))
   196  	assert.Equal(t, "", formatStatsdTags([]interface{}{}))
   197  
   198  	got := formatStatsdTags(tags)
   199  
   200  	expect := ",bool=true,float32=1.2300000190734863,float64=1.23,int=1,int8=2,int16=2,int32=2,int64=2,uint=1,uint8=2,uint16=2,uint32=2,uint64=2,test=baz,foo=bar,struct={Name:blah}"
   201  	assert.Equal(t, expect, got)
   202  }
   203  
   204  func TestFormatStatsdTags_Tags(t *testing.T) {
   205  	tags := Tags{}.
   206  		With("test", "test").
   207  		With("foo", "bar").
   208  		With("test", "test")
   209  
   210  	got := formatStatsdTags([]interface{}{tags})
   211  	assert.Contains(t, got, ",test=test")
   212  	assert.Contains(t, got, ",foo=bar")
   213  }
   214  
   215  func TestFormatStatsdTags_Uneven(t *testing.T) {
   216  	tags := []interface{}{
   217  		"test", "test",
   218  		"foo",
   219  	}
   220  
   221  	s := formatStatsdTags(tags)
   222  
   223  	assert.Equal(t, ",test=test,foo=nil,STATS_ERROR=Normalised odd number of tags by adding nil", s)
   224  }
   225  
   226  func BenchmarkFormatStatsdTags(b *testing.B) {
   227  	tags := []interface{}{
   228  		"string", "test",
   229  		"float", 1.2,
   230  		"int", 1,
   231  		"bool", true,
   232  	}
   233  
   234  	b.ReportAllocs()
   235  	for i := 0; i < b.N; i++ {
   236  		formatStatsdTags(tags)
   237  	}
   238  }