Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Client test that can detect data races when emitting metrics from separate goroutines. #180

Merged
merged 1 commit into from Feb 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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