make the buffered qlog writer used in interop flush before closing

This commit is contained in:
Marten Seemann 2020-03-19 12:58:56 +07:00
parent 2f2583beb0
commit 3e8c3cafc1
5 changed files with 57 additions and 21 deletions

View file

@ -62,13 +62,7 @@ func main() {
log.Fatal(err)
}
log.Printf("Creating qlog file %s.\n", filename)
return struct {
io.Writer
io.Closer
}{
bufio.NewWriter(f),
f,
}
return utils.NewBufferedWriteCloser(bufio.NewWriter(f), f)
}
}
roundTripper := &http3.RoundTripper{

View file

@ -216,13 +216,7 @@ func main() {
log.Fatal(err)
}
log.Printf("Creating qlog file %s.\n", filename)
return struct {
io.Writer
io.Closer
}{
bufio.NewWriter(f),
f,
}
return utils.NewBufferedWriteCloser(bufio.NewWriter(f), f)
}
}

View file

@ -0,0 +1,26 @@
package utils
import (
"bufio"
"io"
)
type bufferedWriteCloser struct {
*bufio.Writer
io.Closer
}
// NewBufferedWriteCloser creates an io.WriteCloser from a bufio.Writer and an io.Closer
func NewBufferedWriteCloser(writer *bufio.Writer, closer io.Closer) io.WriteCloser {
return &bufferedWriteCloser{
Writer: writer,
Closer: closer,
}
}
func (h bufferedWriteCloser) Close() error {
if err := h.Writer.Flush(); err != nil {
return err
}
return h.Closer.Close()
}

View file

@ -0,0 +1,26 @@
package utils
import (
"bufio"
"bytes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type nopCloser struct{}
func (nopCloser) Close() error { return nil }
var _ = Describe("buffered io.WriteCloser", func() {
It("flushes before closing", func() {
buf := &bytes.Buffer{}
w := bufio.NewWriter(buf)
wc := NewBufferedWriteCloser(w, &nopCloser{})
wc.Write([]byte("foobar"))
Expect(buf.Len()).To(BeZero())
Expect(wc.Close()).To(Succeed())
Expect(buf.String()).To(Equal("foobar"))
})
})

View file

@ -7,6 +7,8 @@ import (
"log"
"os"
"strings"
"github.com/lucas-clemente/quic-go/internal/utils"
)
// GetSSLKeyLog creates a file for the TLS key log
@ -39,12 +41,6 @@ func GetQLOGWriter() (func(connID []byte) io.WriteCloser, error) {
if err != nil {
log.Fatalf("Failed to create qlog file %s: %s", path, err.Error())
}
return struct {
io.Writer
io.Closer
}{
bufio.NewWriter(f),
f,
}
return utils.NewBufferedWriteCloser(bufio.NewWriter(f), f)
}, nil
}