Skip to content

Commit

Permalink
Add a Client test that can detect data races when emitting metrics fr…
Browse files Browse the repository at this point in the history
…om separate goroutines.
  • Loading branch information
matthewdale authored and hush-hush committed Feb 12, 2021
1 parent a9ca2d2 commit 8b8be9a
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions statsd/statsd_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"net"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -35,6 +36,62 @@ func TestCustomWriterBufferConfiguration(t *testing.T) {
assert.Equal(t, DefaultUDPBufferPoolSize, cap(client.sender.queue))
}

// TestConcurrentSend sends various metric types in separate goroutines to
// trigger any possible data races. It is intended to be run with the data race
// detector enabled.
func TestConcurrentSend(t *testing.T) {
tests := []struct {
description string
clientOptions []Option
}{
{
description: "Client with default options",
clientOptions: []Option{},
},
{
description: "Client with mutex mode enabled",
clientOptions: []Option{WithMutexMode()},
},
{
description: "Client with channel mode enabled",
clientOptions: []Option{WithChannelMode()},
},
}

for _, test := range tests {
test := test // Capture range variable.
t.Run(test.description, func(t *testing.T) {
t.Parallel()

client, err := New("localhost:9876", test.clientOptions...)
require.Nil(t, err, fmt.Sprintf("failed to create client: %s", err))

var wg sync.WaitGroup
wg.Add(1)
go func() {
client.Gauge("name", 1, []string{"tag"}, 0.1)
wg.Done()
}()

wg.Add(1)
go func() {
client.Count("name", 1, []string{"tag"}, 0.1)
wg.Done()
}()

wg.Add(1)
go func() {
client.Timing("name", 1, []string{"tag"}, 0.1)
wg.Done()
}()

wg.Wait()
err = client.Close()
require.Nil(t, err, fmt.Sprintf("failed to close client: %s", err))
})
}
}

func getTestServer(t *testing.T, addr string) *net.UDPConn {
udpAddr, err := net.ResolveUDPAddr("udp", addr)
require.Nil(t, err, fmt.Sprintf("could not resolve udp '%s': %s", addr, err))
Expand Down

0 comments on commit 8b8be9a

Please sign in to comment.