Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelli321 committed Dec 1, 2024
1 parent d86e716 commit 98c7c34
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
9 changes: 9 additions & 0 deletions receiver/statsdreceiver/internal/transport/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ func (s *StatsD) connect() error {
if err != nil {
return err
}
case "unixgram":
unixAddr, err := net.ResolveUnixAddr(s.transport, s.address)
if err != nil {
return err
}
s.conn, err = net.DialUnix(s.transport, nil, unixAddr)
if err != nil {
return err
}
default:
return fmt.Errorf("unknown/unsupported transport: %s", s.transport)
}
Expand Down
20 changes: 20 additions & 0 deletions receiver/statsdreceiver/internal/transport/packet_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ func (u *packetServer) ListenAndServe(
buf := make([]byte, 65527) // max size for udp packet body (assuming ipv6)
for {
n, addr, err := u.packetConn.ReadFrom(buf)
if addr == nil && u.transport == UDS {
addr = &udsAddr{
network: u.transport.String(),
address: u.packetConn.LocalAddr().String(),
}
}

if n > 0 {
u.handlePacket(n, buf, addr, transferChan)
}
Expand Down Expand Up @@ -62,3 +69,16 @@ func (u *packetServer) handlePacket(
}
}
}

type udsAddr struct {
network string
address string
}

func (u *udsAddr) Network() string {
return u.network
}

func (u *udsAddr) String() string {
return u.address
}
18 changes: 18 additions & 0 deletions receiver/statsdreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ func Test_statsdreceiver_EndToEnd(t *testing.T) {
return c
},
},
{
name: "UDS server with 4s interval",
addr: "/tmp/statsd_test.sock",
configFn: func() *Config {
return &Config{
NetAddr: confignet.AddrConfig{
Endpoint: "/tmp/statsd_test.sock",
Transport: confignet.TransportTypeUnixgram,
},
AggregationInterval: 4 * time.Second,
}
},
clientFn: func(t *testing.T, addr string) *client.StatsD {
c, err := client.NewStatsD("unixgram", addr)
require.NoError(t, err)
return c
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 98c7c34

Please sign in to comment.