Skip to content

Commit

Permalink
quic/qlog: correctly write negative durations
Browse files Browse the repository at this point in the history
"-10.000001", not "10.-000001".

Change-Id: I84f6487bad15ab3a190e73e655236376b1781e85
Reviewed-on: https://go-review.googlesource.com/c/net/+/545576
Reviewed-by: Jonathan Amsterdam <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
neild committed Dec 18, 2023
1 parent b0eb4d6 commit 1e59a7e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 4 additions & 0 deletions internal/quic/qlog/json_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ func (w *jsonWriter) writeBoolField(name string, v bool) {

// writeDuration writes a duration as milliseconds.
func (w *jsonWriter) writeDuration(v time.Duration) {
if v < 0 {
w.buf.WriteByte('-')
v = -v
}
fmt.Fprintf(&w.buf, "%d.%06d", v.Milliseconds(), v%time.Millisecond)
}

Expand Down
5 changes: 3 additions & 2 deletions internal/quic/qlog/json_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ func TestJSONWriterBoolField(t *testing.T) {
func TestJSONWriterDurationField(t *testing.T) {
w := newTestJSONWriter()
w.writeRecordStart()
w.writeDurationField("field", (10*time.Millisecond)+(2*time.Nanosecond))
w.writeDurationField("field1", (10*time.Millisecond)+(2*time.Nanosecond))
w.writeDurationField("field2", -((10 * time.Millisecond) + (2 * time.Nanosecond)))
w.writeRecordEnd()
wantJSONRecord(t, w, `{"field":10.000002}`)
wantJSONRecord(t, w, `{"field1":10.000002,"field2":-10.000002}`)
}

func TestJSONWriterFloat64Field(t *testing.T) {
Expand Down

0 comments on commit 1e59a7e

Please sign in to comment.