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

     1  package versionnegotiation
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/apernet/quic-go"
     8  	quicproxy "github.com/apernet/quic-go/integrationtests/tools/proxy"
     9  	"github.com/apernet/quic-go/internal/protocol"
    10  
    11  	. "github.com/onsi/ginkgo/v2"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Handshake RTT tests", func() {
    16  	const rtt = 400 * time.Millisecond
    17  
    18  	expectDurationInRTTs := func(startTime time.Time, num int) {
    19  		testDuration := time.Since(startTime)
    20  		rtts := float32(testDuration) / float32(rtt)
    21  		Expect(rtts).To(SatisfyAll(
    22  			BeNumerically(">=", num),
    23  			BeNumerically("<", num+1),
    24  		))
    25  	}
    26  
    27  	It("fails when there's no matching version, after 1 RTT", func() {
    28  		if len(protocol.SupportedVersions) == 1 {
    29  			Skip("Test requires at least 2 supported versions.")
    30  		}
    31  
    32  		serverConfig := &quic.Config{}
    33  		serverConfig.Versions = protocol.SupportedVersions[:1]
    34  		ln, err := quic.ListenAddr("localhost:0", getTLSConfig(), serverConfig)
    35  		Expect(err).ToNot(HaveOccurred())
    36  		defer ln.Close()
    37  
    38  		// start the proxy
    39  		proxy, err := quicproxy.NewQuicProxy("localhost:0", &quicproxy.Opts{
    40  			RemoteAddr:  ln.Addr().String(),
    41  			DelayPacket: func(_ quicproxy.Direction, _ []byte) time.Duration { return rtt / 2 },
    42  		})
    43  		Expect(err).ToNot(HaveOccurred())
    44  
    45  		startTime := time.Now()
    46  		_, err = quic.DialAddr(
    47  			context.Background(),
    48  			proxy.LocalAddr().String(),
    49  			getTLSClientConfig(),
    50  			maybeAddQLOGTracer(&quic.Config{Versions: protocol.SupportedVersions[1:2]}),
    51  		)
    52  		Expect(err).To(HaveOccurred())
    53  		expectDurationInRTTs(startTime, 1)
    54  	})
    55  })