forked from quic-go/quic-go
fix crash when the qlog callbacks returns a nil io.WriteCloser
This commit is contained in:
12
qlog/qlog.go
12
qlog/qlog.go
@@ -31,11 +31,17 @@ func NewTracer(getLogWriter func(connectionID []byte) io.WriteCloser) logging.Tr
|
||||
}
|
||||
|
||||
func (t *tracer) TracerForServer(odcid protocol.ConnectionID) logging.ConnectionTracer {
|
||||
return newTracer(t.getLogWriter(odcid.Bytes()), protocol.PerspectiveServer, odcid)
|
||||
if w := t.getLogWriter(odcid.Bytes()); w != nil {
|
||||
return newConnectionTracer(w, protocol.PerspectiveServer, odcid)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *tracer) TracerForClient(odcid protocol.ConnectionID) logging.ConnectionTracer {
|
||||
return newTracer(t.getLogWriter(odcid.Bytes()), protocol.PerspectiveClient, odcid)
|
||||
if w := t.getLogWriter(odcid.Bytes()); w != nil {
|
||||
return newConnectionTracer(w, protocol.PerspectiveClient, odcid)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type connectionTracer struct {
|
||||
@@ -57,7 +63,7 @@ type connectionTracer struct {
|
||||
var _ logging.ConnectionTracer = &connectionTracer{}
|
||||
|
||||
// newTracer creates a new connectionTracer to record a qlog.
|
||||
func newTracer(w io.WriteCloser, p protocol.Perspective, odcid protocol.ConnectionID) logging.ConnectionTracer {
|
||||
func newConnectionTracer(w io.WriteCloser, p protocol.Perspective, odcid protocol.ConnectionID) logging.ConnectionTracer {
|
||||
t := &connectionTracer{
|
||||
w: w,
|
||||
perspective: p,
|
||||
|
||||
@@ -48,7 +48,16 @@ type entry struct {
|
||||
Event map[string]interface{}
|
||||
}
|
||||
|
||||
var _ = Describe("Tracer", func() {
|
||||
var _ = Describe("Tracing", func() {
|
||||
Context("tracer", func() {
|
||||
It("returns nil when there's no io.WriteCloser", func() {
|
||||
t := NewTracer(func([]byte) io.WriteCloser { return nil })
|
||||
Expect(t.TracerForClient(logging.ConnectionID{1, 2, 3, 4})).To(BeNil())
|
||||
Expect(t.TracerForServer(logging.ConnectionID{1, 2, 3, 4})).To(BeNil())
|
||||
})
|
||||
})
|
||||
|
||||
Context("connection tracer", func() {
|
||||
var (
|
||||
tracer logging.ConnectionTracer
|
||||
buf *bytes.Buffer
|
||||
@@ -56,11 +65,8 @@ var _ = Describe("Tracer", func() {
|
||||
|
||||
BeforeEach(func() {
|
||||
buf = &bytes.Buffer{}
|
||||
tracer = newTracer(
|
||||
nopWriteCloser(buf),
|
||||
protocol.PerspectiveServer,
|
||||
protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef},
|
||||
)
|
||||
t := NewTracer(func([]byte) io.WriteCloser { return nopWriteCloser(buf) })
|
||||
tracer = t.TracerForServer(logging.ConnectionID{0xde, 0xad, 0xbe, 0xef})
|
||||
})
|
||||
|
||||
It("exports a trace that has the right metadata", func() {
|
||||
@@ -91,7 +97,7 @@ var _ = Describe("Tracer", func() {
|
||||
})
|
||||
|
||||
It("stops writing when encountering an error", func() {
|
||||
tracer = newTracer(
|
||||
tracer = newConnectionTracer(
|
||||
&limitedWriter{WriteCloser: nopWriteCloser(buf), N: 250},
|
||||
protocol.PerspectiveServer,
|
||||
protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef},
|
||||
@@ -623,3 +629,4 @@ var _ = Describe("Tracer", func() {
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user