File: qlog_dir_test.go

package info (click to toggle)
golang-github-lucas-clemente-quic-go 0.54.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,312 kB
  • sloc: sh: 54; makefile: 7
file content (79 lines) | stat: -rw-r--r-- 1,713 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package self_test

import (
	"context"
	"os"
	"path"
	"regexp"
	"testing"
	"time"

	"github.com/quic-go/quic-go"
	"github.com/quic-go/quic-go/qlog"

	"github.com/stretchr/testify/require"
)

func TestQlogDirEnvironmentVariable(t *testing.T) {
	tempDir := t.TempDir()
	qlogDir := path.Join(tempDir, "qlogs")
	t.Setenv("QLOGDIR", qlogDir)

	serverStopped := make(chan struct{})
	server, err := quic.Listen(
		newUDPConnLocalhost(t),
		getTLSConfig(),
		&quic.Config{
			Tracer: qlog.DefaultConnectionTracer,
		},
	)
	require.NoError(t, err)

	go func() {
		defer close(serverStopped)
		for {
			if _, err := server.Accept(context.Background()); err != nil {
				return
			}
		}
	}()

	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	conn, err := quic.Dial(
		ctx,
		newUDPConnLocalhost(t),
		server.Addr(),
		getTLSClientConfig(),
		&quic.Config{
			Tracer: qlog.DefaultConnectionTracer,
		},
	)
	require.NoError(t, err)
	conn.CloseWithError(0, "")
	server.Close()
	<-serverStopped

	_, err = os.Stat(qlogDir)
	qlogDirCreated := !os.IsNotExist(err)
	require.True(t, qlogDirCreated)

	childs, err := os.ReadDir(qlogDir)
	require.NoError(t, err)
	require.Len(t, childs, 2)

	odcids := make([]string, 0, 2)
	vantagePoints := make([]string, 0, 2)
	qlogFileNameRegexp := regexp.MustCompile(`^([0-f]+)_(client|server).sqlog$`)

	for _, child := range childs {
		matches := qlogFileNameRegexp.FindStringSubmatch(child.Name())
		require.Len(t, matches, 3)
		odcids = append(odcids, matches[1])
		vantagePoints = append(vantagePoints, matches[2])
	}

	require.Equal(t, odcids[0], odcids[1])
	require.Contains(t, vantagePoints, "client")
	require.Contains(t, vantagePoints, "server")
}