github.com/blend/go-sdk@v1.20240719.1/datadog/collector_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - 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 datadog
     9  
    10  import (
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/blend/go-sdk/ref"
    15  
    16  	"github.com/DataDog/datadog-go/v5/statsd"
    17  
    18  	"github.com/blend/go-sdk/assert"
    19  	"github.com/blend/go-sdk/stats"
    20  	"github.com/blend/go-sdk/uuid"
    21  )
    22  
    23  func TestConvertEvent(t *testing.T) {
    24  	assert := assert.New(t)
    25  
    26  	original := stats.Event{
    27  		Title:          uuid.V4().String(),
    28  		Text:           uuid.V4().String(),
    29  		Timestamp:      time.Now().UTC(),
    30  		Hostname:       uuid.V4().String(),
    31  		AggregationKey: uuid.V4().String(),
    32  		Priority:       uuid.V4().String(),
    33  		SourceTypeName: uuid.V4().String(),
    34  		AlertType:      uuid.V4().String(),
    35  		Tags:           []string{uuid.V4().String()},
    36  	}
    37  
    38  	converted := ConvertEvent(original)
    39  	assert.Equal(original.Title, converted.Title)
    40  	assert.Equal(original.Text, converted.Text)
    41  	assert.Equal(original.Timestamp, converted.Timestamp)
    42  	assert.Equal(original.Hostname, converted.Hostname)
    43  	assert.Equal(original.AggregationKey, converted.AggregationKey)
    44  	assert.Equal(original.Priority, converted.Priority)
    45  	assert.Equal(original.SourceTypeName, converted.SourceTypeName)
    46  	assert.Equal(original.AlertType, converted.AlertType)
    47  	assert.Equal(original.Tags, converted.Tags)
    48  }
    49  
    50  func TestCollectorFlush(t *testing.T) {
    51  	assert := assert.New(t)
    52  
    53  	// `client` is `nil`
    54  	c := Collector{}
    55  	assert.Nil(c.Flush())
    56  
    57  	// `client` is not `nil`
    58  	client, err := statsd.New("localhost:8125")
    59  	assert.Nil(err)
    60  	defer client.Close()
    61  
    62  	c = Collector{client: client}
    63  	assert.Nil(c.Flush())
    64  }
    65  
    66  func TestCollectorClose(t *testing.T) {
    67  	assert := assert.New(t)
    68  
    69  	// `client` is `nil`
    70  	c := Collector{}
    71  	assert.Nil(c.Close())
    72  
    73  	// `client` is not `nil`
    74  	client, err := statsd.New("localhost:8125")
    75  	assert.Nil(err)
    76  
    77  	c = Collector{client: client}
    78  	assert.Nil(c.Close())
    79  }
    80  
    81  func TestCollectorNew(t *testing.T) {
    82  	assert := assert.New(t)
    83  	cfg := Config{
    84  		Address: "localhost:8125",
    85  	}
    86  	c, err := New(cfg)
    87  	assert.Nil(err)
    88  	assert.NotNil(c)
    89  	assert.NotNil(c.client)
    90  	assert.Empty(c.defaultTags)
    91  
    92  	c, err = New(cfg, statsd.WithNamespace("hello"))
    93  	assert.Nil(err)
    94  	assert.NotNil(c)
    95  	assert.NotNil(c.client)
    96  
    97  	cfg.BufferDepth = 10
    98  	cfg.Buffered = ref.Bool(true)
    99  
   100  	c, err = New(cfg, statsd.WithNamespace("hello"))
   101  	assert.Nil(err)
   102  	assert.NotNil(c)
   103  	assert.NotNil(c.client)
   104  }