Adding new simple test to relay package

Signed-off-by: Pedro Tanaka <pedro.tanaka@shopify.com>
This commit is contained in:
Pedro Tanaka 2022-04-21 08:27:16 +02:00
parent 4ad11fcafa
commit 78dcd3b7b2
No known key found for this signature in database
GPG key ID: D0D8389DA4EE060B
4 changed files with 66 additions and 1 deletions

1
go.mod
View file

@ -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
)

2
go.sum
View file

@ -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=

View file

@ -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 {

61
pkg/relay/relay_test.go Normal file
View file

@ -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)
})
})
}
}