diff --git a/client_test.go b/client_test.go index d5599ab..0cda316 100644 --- a/client_test.go +++ b/client_test.go @@ -1,10 +1,13 @@ package eventkit import ( + "context" + "reflect" "testing" "time" "storj.io/eventkit/pb" + "storj.io/eventkit/transport" ) func BenchmarkOutgoingPacket(b *testing.B) { @@ -31,3 +34,51 @@ func BenchmarkOutgoingPacket(b *testing.B) { _ = packet.finalize() } } + +func TestZeroValueType(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + l, err := transport.ListenUDP("127.0.0.1:0") + requireNoError(t, err) + defer l.Close() + + go func() { + client := NewUDPClient("application", "v1.0.0", "instance", l.LocalAddr().String()) + client.FlushInterval = time.Millisecond + + go client.Run(ctx) + + event := &Event{ + Name: "Name", + Scope: []string{"package/name"}, + Tags: []*pb.Tag{Int64("key", 0)}, + } + client.Submit(event) + }() + payload, _, err := l.Next() + requireNoError(t, err) + packet, err := transport.ParsePacket(payload) + requireNoError(t, err) + + requireEqual(t, len(packet.Events), 1) + requireEqual(t, packet.Events[0].Name, "Name") + requireEqual(t, packet.Events[0].Scope, []string{"package/name"}) + requireEqual(t, len(packet.Events[0].Tags), 1) + requireEqual(t, packet.Events[0].Tags[0].Key, "key") + val, ok := packet.Events[0].Tags[0].Value.(*pb.Tag_Int64) + requireEqual(t, ok, true) + requireEqual(t, val.Int64, int64(0)) +} + +func requireNoError(t *testing.T, err error) { + if err != nil { + t.Fatalf("unexpected error: %+v", err) + } +} + +func requireEqual(t *testing.T, actual, expected any) { + if !reflect.DeepEqual(actual, expected) { + t.Fatalf("equality expected: %q vs %q", actual, expected) + } +} diff --git a/transport/udp.go b/transport/udp.go index 473ad15..bf514b7 100644 --- a/transport/udp.go +++ b/transport/udp.go @@ -49,6 +49,10 @@ func (u *UDPListener) Next() (payload []byte, source *net.UDPAddr, err error) { return buf[:n], source, err } +func (u *UDPListener) LocalAddr() net.Addr { + return u.conn.LocalAddr() +} + func (u *UDPListener) Close() error { return u.conn.Close() }