github.com/danielpfeifer02/quic-go-prio-packs@v0.41.0-28/integrationtests/self/early_data_test.go (about)

     1  package self_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"net"
     8  	"time"
     9  
    10  	"github.com/danielpfeifer02/quic-go-prio-packs"
    11  	quicproxy "github.com/danielpfeifer02/quic-go-prio-packs/integrationtests/tools/proxy"
    12  
    13  	. "github.com/onsi/ginkgo/v2"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("early data", func() {
    18  	const rtt = 80 * time.Millisecond
    19  
    20  	It("sends 0.5-RTT data", func() {
    21  		ln, err := quic.ListenAddrEarly(
    22  			"localhost:0",
    23  			getTLSConfig(),
    24  			getQuicConfig(nil),
    25  		)
    26  		Expect(err).ToNot(HaveOccurred())
    27  		defer ln.Close()
    28  		done := make(chan struct{})
    29  		go func() {
    30  			defer GinkgoRecover()
    31  			defer close(done)
    32  			conn, err := ln.Accept(context.Background())
    33  			Expect(err).ToNot(HaveOccurred())
    34  			str, err := conn.OpenUniStream()
    35  			Expect(err).ToNot(HaveOccurred())
    36  			_, err = str.Write([]byte("early data"))
    37  			Expect(err).ToNot(HaveOccurred())
    38  			Expect(str.Close()).To(Succeed())
    39  			// make sure the Write finished before the handshake completed
    40  			Expect(conn.HandshakeComplete()).ToNot(BeClosed())
    41  			Eventually(conn.Context().Done()).Should(BeClosed())
    42  		}()
    43  		serverPort := ln.Addr().(*net.UDPAddr).Port
    44  		proxy, err := quicproxy.NewQuicProxy("localhost:0", &quicproxy.Opts{
    45  			RemoteAddr: fmt.Sprintf("localhost:%d", serverPort),
    46  			DelayPacket: func(quicproxy.Direction, []byte) time.Duration {
    47  				return rtt / 2
    48  			},
    49  		})
    50  		Expect(err).ToNot(HaveOccurred())
    51  		defer proxy.Close()
    52  
    53  		conn, err := quic.DialAddr(
    54  			context.Background(),
    55  			fmt.Sprintf("localhost:%d", proxy.LocalPort()),
    56  			getTLSClientConfig(),
    57  			getQuicConfig(nil),
    58  		)
    59  		Expect(err).ToNot(HaveOccurred())
    60  		str, err := conn.AcceptUniStream(context.Background())
    61  		Expect(err).ToNot(HaveOccurred())
    62  		data, err := io.ReadAll(str)
    63  		Expect(err).ToNot(HaveOccurred())
    64  		Expect(data).To(Equal([]byte("early data")))
    65  		conn.CloseWithError(0, "")
    66  		Eventually(done).Should(BeClosed())
    67  	})
    68  })