github.com/apernet/quic-go@v0.43.1-0.20240515053213-5e9e635fd9f0/sys_conn_test.go (about)

     1  package quic
     2  
     3  import (
     4  	"net"
     5  	"time"
     6  
     7  	"github.com/apernet/quic-go/internal/protocol"
     8  
     9  	. "github.com/onsi/ginkgo/v2"
    10  	. "github.com/onsi/gomega"
    11  	"go.uber.org/mock/gomock"
    12  )
    13  
    14  var _ = Describe("Basic Conn Test", func() {
    15  	It("reads a packet", func() {
    16  		c := NewMockPacketConn(mockCtrl)
    17  		addr := &net.UDPAddr{IP: net.IPv4(1, 2, 3, 4), Port: 1234}
    18  		c.EXPECT().ReadFrom(gomock.Any()).DoAndReturn(func(b []byte) (int, net.Addr, error) {
    19  			data := []byte("foobar")
    20  			Expect(b).To(HaveLen(protocol.MaxPacketBufferSize))
    21  			return copy(b, data), addr, nil
    22  		})
    23  
    24  		conn, err := wrapConn(c)
    25  		Expect(err).ToNot(HaveOccurred())
    26  		p, err := conn.ReadPacket()
    27  		Expect(err).ToNot(HaveOccurred())
    28  		Expect(p.data).To(Equal([]byte("foobar")))
    29  		Expect(p.rcvTime).To(BeTemporally("~", time.Now(), scaleDuration(100*time.Millisecond)))
    30  		Expect(p.remoteAddr).To(Equal(addr))
    31  	})
    32  })