From 78dcd3b7b2e04218ec409ad703aec4aa9884c648 Mon Sep 17 00:00:00 2001 From: Pedro Tanaka Date: Thu, 21 Apr 2022 08:27:16 +0200 Subject: [PATCH] Adding new simple test to relay package Signed-off-by: Pedro Tanaka --- go.mod | 1 + go.sum | 2 ++ pkg/relay/relay.go | 3 +- pkg/relay/relay_test.go | 61 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 pkg/relay/relay_test.go diff --git a/go.mod b/go.mod index 1812e33..065a33e 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/prometheus/client_golang v1.11.0 github.com/prometheus/client_model v0.2.0 github.com/prometheus/common v0.30.0 + github.com/stvp/go-udp-testing v0.0.0-20201019212854-469649b16807 gopkg.in/alecthomas/kingpin.v2 v2.2.6 gopkg.in/yaml.v2 v2.4.0 ) diff --git a/go.sum b/go.sum index 6765984..e69f2f3 100644 --- a/go.sum +++ b/go.sum @@ -190,6 +190,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stvp/go-udp-testing v0.0.0-20201019212854-469649b16807 h1:LUsDduamlucuNnWcaTbXQ6aLILFcLXADpOzeEH3U+OI= +github.com/stvp/go-udp-testing v0.0.0-20201019212854-469649b16807/go.mod h1:7jxmlfBCDBXRzr0eAQJ48XC1hBu1np4CS5+cHEYfwpc= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/pkg/relay/relay.go b/pkg/relay/relay.go index e1b280c..900d86a 100644 --- a/pkg/relay/relay.go +++ b/pkg/relay/relay.go @@ -16,6 +16,7 @@ package relay import ( "bytes" "fmt" + "github.com/prometheus/statsd_exporter/pkg/clock" "net" "strings" "time" @@ -91,7 +92,7 @@ func (r *Relay) relayOutput() { var buffer bytes.Buffer var err error - relayInterval := time.NewTicker(1 * time.Second) + relayInterval := clock.NewTicker(1 * time.Second) defer relayInterval.Stop() for { diff --git a/pkg/relay/relay_test.go b/pkg/relay/relay_test.go new file mode 100644 index 0000000..967b670 --- /dev/null +++ b/pkg/relay/relay_test.go @@ -0,0 +1,61 @@ +package relay + +import ( + "github.com/go-kit/log" + "github.com/prometheus/statsd_exporter/pkg/clock" + "github.com/stvp/go-udp-testing" + "testing" + "time" +) + +func TestRelay_RelayLine(t *testing.T) { + type args struct { + lines []string + expected string + } + + tests := []struct { + name string + args args + }{ + { + name: "single line", + args: args{ + lines: []string{"foo5:100|c|#tag1:bar,#tag2:baz"}, + expected: "foo5:100|c|#tag1:bar,#tag2:baz\n", + }, + }, + } + + for _, tt := range tests { + udp.SetAddr(":1160") + t.Run(tt.name, func(t *testing.T) { + + tickerCh := make(chan time.Time) + clock.ClockInstance = &clock.Clock{ + TickerCh: tickerCh, + } + clock.ClockInstance.Instant = time.Unix(0, 0) + + logger := log.NewNopLogger() + r, err := NewRelay( + logger, + "localhost:1160", + 200, + ) + + if err != nil { + t.Errorf("Did not expect error while creating relay.") + } + + udp.ShouldReceive(t, tt.args.expected, func() { + for _, line := range tt.args.lines { + r.RelayLine(line) + // Tick time forward to trigger a flush + } + clock.ClockInstance.Instant = time.Unix(20000, 0) + clock.ClockInstance.TickerCh <- time.Unix(20000, 0) + }) + }) + } +}