mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 15:04:01 +03:00
Fix detected race
This commit is contained in:
+18
-11
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -20,13 +21,14 @@ const statsdSleepTime = 3 * statsd.DefaultFlushInterval
|
|||||||
type statsdFakeServer struct {
|
type statsdFakeServer struct {
|
||||||
conn *net.UDPConn
|
conn *net.UDPConn
|
||||||
buf *bytes.Buffer
|
buf *bytes.Buffer
|
||||||
|
mutex sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s statsdFakeServer) Addr() string {
|
func (s *statsdFakeServer) Addr() string {
|
||||||
return s.conn.LocalAddr().String()
|
return s.conn.LocalAddr().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s statsdFakeServer) Close() error {
|
func (s *statsdFakeServer) Close() error {
|
||||||
if s.conn != nil {
|
if s.conn != nil {
|
||||||
return s.conn.Close()
|
return s.conn.Close()
|
||||||
}
|
}
|
||||||
@@ -34,11 +36,14 @@ func (s statsdFakeServer) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s statsdFakeServer) String() string {
|
func (s *statsdFakeServer) String() string {
|
||||||
|
s.mutex.Lock()
|
||||||
|
defer s.mutex.Unlock()
|
||||||
|
|
||||||
return strings.TrimSpace(s.buf.String())
|
return strings.TrimSpace(s.buf.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func statsdNewFakeServer() statsdFakeServer {
|
func statsdNewFakeServer() *statsdFakeServer {
|
||||||
conn, err := net.ListenUDP("udp", &net.UDPAddr{
|
conn, err := net.ListenUDP("udp", &net.UDPAddr{
|
||||||
IP: net.ParseIP("127.0.0.1"),
|
IP: net.ParseIP("127.0.0.1"),
|
||||||
Port: 0,
|
Port: 0,
|
||||||
@@ -47,7 +52,10 @@ func statsdNewFakeServer() statsdFakeServer {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := &bytes.Buffer{}
|
rv := &statsdFakeServer{
|
||||||
|
conn: conn,
|
||||||
|
buf: &bytes.Buffer{},
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
currentBuffer := make([]byte, 4096)
|
currentBuffer := make([]byte, 4096)
|
||||||
@@ -55,7 +63,9 @@ func statsdNewFakeServer() statsdFakeServer {
|
|||||||
for {
|
for {
|
||||||
n, _, err := conn.ReadFromUDP(currentBuffer)
|
n, _, err := conn.ReadFromUDP(currentBuffer)
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
buf.Write(currentBuffer[:n])
|
rv.mutex.Lock()
|
||||||
|
rv.buf.Write(currentBuffer[:n])
|
||||||
|
rv.mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -64,16 +74,13 @@ func statsdNewFakeServer() statsdFakeServer {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return statsdFakeServer{
|
return rv
|
||||||
conn: conn,
|
|
||||||
buf: buf,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type StatsdTestSuite struct {
|
type StatsdTestSuite struct {
|
||||||
suite.Suite
|
suite.Suite
|
||||||
|
|
||||||
statsdServer statsdFakeServer
|
statsdServer *statsdFakeServer
|
||||||
factory stats.StatsdFactory
|
factory stats.StatsdFactory
|
||||||
statsd events.Observer
|
statsd events.Observer
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user