github.com/blend/go-sdk@v1.20220411.3/stats/mock_collector_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package stats
     9  
    10  import (
    11  	"fmt"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  )
    17  
    18  func TestMockCollectorDefaultTags(t *testing.T) {
    19  	assert := assert.New(t)
    20  
    21  	assertTags := func(actualTags []string) {
    22  		assert.Len(actualTags, 3)
    23  		assert.Equal("k1:v1", actualTags[0])
    24  		assert.Equal("k2:v2", actualTags[1])
    25  		assert.Equal("k3:v3", actualTags[2])
    26  	}
    27  
    28  	collector := NewMockCollector(32)
    29  	collector.AddDefaultTags(Tag("k1", "v1"))
    30  	collector.AddDefaultTags(Tag("k2", "v2"))
    31  
    32  	tags := collector.DefaultTags()
    33  	assert.Len(tags, 2)
    34  	assert.Equal("k1:v1", tags[0])
    35  	assert.Equal("k2:v2", tags[1])
    36  
    37  	assert.Nil(collector.Count("event", 10, "k3:v3"))
    38  	mockMetric := <-collector.Metrics
    39  	assertTags(mockMetric.Tags)
    40  
    41  	assert.Nil(collector.Increment("event", "k3:v3"))
    42  	mockMetric = <-collector.Metrics
    43  	assertTags(mockMetric.Tags)
    44  
    45  	assert.Nil(collector.Gauge("event", 0.1, "k3:v3"))
    46  	mockMetric = <-collector.Metrics
    47  	assertTags(mockMetric.Tags)
    48  
    49  	assert.Nil(collector.Histogram("event", 0.1, "k3:v3"))
    50  	mockMetric = <-collector.Metrics
    51  	assertTags(mockMetric.Tags)
    52  
    53  	assert.Nil(collector.TimeInMilliseconds("event", time.Second, "k3:v3"))
    54  	mockMetric = <-collector.Metrics
    55  	assertTags(mockMetric.Tags)
    56  }
    57  
    58  func TestMockCollectorCount(t *testing.T) {
    59  	assert := assert.New(t)
    60  
    61  	collector := NewMockCollector(32)
    62  
    63  	assert.Nil(collector.Count("event", 10))
    64  
    65  	mockMetric := <-collector.Metrics
    66  	assert.Equal("event", mockMetric.Name)
    67  	assert.Equal(10, mockMetric.Count)
    68  	assert.Zero(mockMetric.Gauge)
    69  	assert.Zero(mockMetric.Histogram)
    70  	assert.Zero(mockMetric.TimeInMilliseconds)
    71  }
    72  
    73  func TestMockCollectorIncrement(t *testing.T) {
    74  	assert := assert.New(t)
    75  
    76  	collector := NewMockCollector(32)
    77  
    78  	assert.Nil(collector.Increment("event", ""))
    79  
    80  	mockMetric := <-collector.Metrics
    81  	assert.Equal("event", mockMetric.Name)
    82  	assert.Equal(1, mockMetric.Count)
    83  	assert.Zero(mockMetric.Gauge)
    84  	assert.Zero(mockMetric.Histogram)
    85  	assert.Zero(mockMetric.TimeInMilliseconds)
    86  }
    87  
    88  func TestMockCollectorGauge(t *testing.T) {
    89  	assert := assert.New(t)
    90  
    91  	collector := NewMockCollector(32)
    92  
    93  	assert.Nil(collector.Gauge("event", 0.1))
    94  
    95  	mockMetric := <-collector.Metrics
    96  	assert.Equal("event", mockMetric.Name)
    97  	assert.Equal(0.1, mockMetric.Gauge)
    98  	assert.Zero(mockMetric.Count)
    99  	assert.Zero(mockMetric.Histogram)
   100  	assert.Zero(mockMetric.TimeInMilliseconds)
   101  }
   102  
   103  func TestMockCollectorHistogram(t *testing.T) {
   104  	assert := assert.New(t)
   105  
   106  	collector := NewMockCollector(32)
   107  
   108  	assert.Nil(collector.Histogram("event", 0.1))
   109  
   110  	mockMetric := <-collector.Metrics
   111  	assert.Equal("event", mockMetric.Name)
   112  	assert.Equal(0.1, mockMetric.Histogram)
   113  	assert.Zero(mockMetric.Gauge)
   114  	assert.Zero(mockMetric.Count)
   115  	assert.Zero(mockMetric.TimeInMilliseconds)
   116  }
   117  
   118  func TestMockCollectorTimeInMilliseconds(t *testing.T) {
   119  	assert := assert.New(t)
   120  
   121  	collector := NewMockCollector(32)
   122  
   123  	assert.Nil(collector.TimeInMilliseconds("event", time.Second))
   124  
   125  	mockMetric := <-collector.Metrics
   126  	assert.Equal("event", mockMetric.Name)
   127  	assert.Equal(1000, mockMetric.TimeInMilliseconds)
   128  	assert.Zero(mockMetric.Gauge)
   129  	assert.Zero(mockMetric.Count)
   130  	assert.Zero(mockMetric.Histogram)
   131  }
   132  
   133  func TestMockCollectorFlush(t *testing.T) {
   134  	assert := assert.New(t)
   135  
   136  	collector := NewMockCollector(32)
   137  
   138  	err := collector.Flush()
   139  	assert.Nil(err)
   140  
   141  	expectedErr := fmt.Errorf("err")
   142  	collector.FlushErrors <- expectedErr
   143  	err = collector.Flush()
   144  	assert.Equal(expectedErr.Error(), err.Error())
   145  }
   146  
   147  func TestMockCollectorClose(t *testing.T) {
   148  	assert := assert.New(t)
   149  
   150  	collector := NewMockCollector(32)
   151  
   152  	err := collector.Close()
   153  	assert.Nil(err)
   154  
   155  	expectedErr := fmt.Errorf("err")
   156  	collector.CloseErrors <- expectedErr
   157  	err = collector.Close()
   158  	assert.Equal(expectedErr.Error(), err.Error())
   159  }