forked from quic-go/quic-go
qlog: add a default tracer that writes to QLOGDIR (#4233)
* add qlog default tracer which writes to QLOGDIR * gofumpt * add qlog default tracer which writes to QLOGDIR * fix flaky tests * Update README.md Co-authored-by: Marten Seemann <martenseemann@gmail.com> * Update README.md Co-authored-by: Marten Seemann <martenseemann@gmail.com> * Update README.md Co-authored-by: Marten Seemann <martenseemann@gmail.com> * Update README.md Co-authored-by: Marten Seemann <martenseemann@gmail.com> --------- Co-authored-by: Marten Seemann <martenseemann@gmail.com>
This commit is contained in:
90
integrationtests/self/qlog_dir_test.go
Normal file
90
integrationtests/self/qlog_dir_test.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package self_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/quic-go/quic-go"
|
||||
"github.com/quic-go/quic-go/qlog"
|
||||
)
|
||||
|
||||
var _ = Describe("qlog dir tests", Serial, func() {
|
||||
var originalQlogDirValue string
|
||||
var tempTestDirPath string
|
||||
|
||||
BeforeEach(func() {
|
||||
originalQlogDirValue = os.Getenv("QLOGDIR")
|
||||
var err error
|
||||
tempTestDirPath, err = os.MkdirTemp("", "temp_test_dir")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
err := os.Setenv("QLOGDIR", originalQlogDirValue)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
err = os.RemoveAll(tempTestDirPath)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
handshake := func() {
|
||||
serverStopped := make(chan struct{})
|
||||
server, err := quic.ListenAddr(
|
||||
"localhost:0",
|
||||
getTLSConfig(),
|
||||
&quic.Config{
|
||||
Tracer: qlog.DefaultTracer,
|
||||
},
|
||||
)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
go func() {
|
||||
defer GinkgoRecover()
|
||||
defer close(serverStopped)
|
||||
for {
|
||||
if _, err := server.Accept(context.Background()); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
conn, err := quic.DialAddr(
|
||||
context.Background(),
|
||||
server.Addr().String(),
|
||||
getTLSClientConfig(),
|
||||
&quic.Config{
|
||||
Tracer: qlog.DefaultTracer,
|
||||
},
|
||||
)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
conn.CloseWithError(0, "")
|
||||
server.Close()
|
||||
<-serverStopped
|
||||
}
|
||||
|
||||
It("environment variable is set", func() {
|
||||
qlogDir := path.Join(tempTestDirPath, "qlogs")
|
||||
err := os.Setenv("QLOGDIR", qlogDir)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
handshake()
|
||||
_, err = os.Stat(tempTestDirPath)
|
||||
qlogDirCreated := !os.IsNotExist(err)
|
||||
Expect(qlogDirCreated).To(BeTrue())
|
||||
childs, err := os.ReadDir(qlogDir)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(len(childs)).To(Equal(2))
|
||||
odcids := make([]string, 0)
|
||||
vantagePoints := make([]string, 0)
|
||||
qlogFileNameRegexp := regexp.MustCompile(`^([0-f]+)_(client|server).qlog$`)
|
||||
for _, child := range childs {
|
||||
matches := qlogFileNameRegexp.FindStringSubmatch(child.Name())
|
||||
odcids = append(odcids, matches[1])
|
||||
vantagePoints = append(vantagePoints, matches[2])
|
||||
}
|
||||
Expect(odcids[0]).To(Equal(odcids[1]))
|
||||
Expect(vantagePoints).To(ContainElements("client", "server"))
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user