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
|
package qlog
import (
"bytes"
"errors"
"io"
"log"
"os"
"testing"
"github.com/quic-go/quic-go/internal/protocol"
"github.com/stretchr/testify/require"
)
type limitedWriter struct {
io.WriteCloser
N int
written int
}
func (w *limitedWriter) Write(p []byte) (int, error) {
if w.written+len(p) > w.N {
return 0, errors.New("writer full")
}
n, err := w.WriteCloser.Write(p)
w.written += n
return n, err
}
func TestWritingStopping(t *testing.T) {
buf := &bytes.Buffer{}
t.Run("stops writing when encountering an error", func(t *testing.T) {
tracer := NewConnectionTracer(
&limitedWriter{WriteCloser: nopWriteCloser(buf), N: 250},
protocol.PerspectiveServer,
protocol.ParseConnectionID([]byte{0xde, 0xad, 0xbe, 0xef}),
)
for i := uint32(0); i < 1000; i++ {
tracer.UpdatedPTOCount(i)
}
// Capture log output
var logBuf bytes.Buffer
log.SetOutput(&logBuf)
defer log.SetOutput(os.Stdout)
tracer.Close()
require.Contains(t, logBuf.String(), "writer full")
})
}
|