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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package internal
import (
"net/http"
"testing"
"time"
)
func TestParseQueueTime(t *testing.T) {
badInput := []string{
"",
"nope",
"t",
"0",
"0.0",
"9999999999999999999999999999999999999999999999999",
"-1368811467146000",
"3000000000",
"3000000000000",
"900000000",
"900000000000",
}
for _, s := range badInput {
if qt := parseQueueTime(s); !qt.IsZero() {
t.Error(s, qt)
}
}
testcases := []struct {
input string
expect int64
}{
// Microseconds
{"1368811467146000", 1368811467},
// Milliseconds
{"1368811467146.000", 1368811467},
{"1368811467146", 1368811467},
// Seconds
{"1368811467.146000", 1368811467},
{"1368811467.146", 1368811467},
{"1368811467", 1368811467},
}
for _, tc := range testcases {
qt := parseQueueTime(tc.input)
if qt.Unix() != tc.expect {
t.Error(tc.input, tc.expect, qt, qt.UnixNano())
}
}
}
func TestQueueDuration(t *testing.T) {
hdr := make(http.Header)
hdr.Set("X-Queue-Start", "1465798814")
qd := QueueDuration(hdr, time.Unix(1465798816, 0))
if qd != 2*time.Second {
t.Error(qd)
}
hdr = make(http.Header)
hdr.Set("X-Request-Start", "1465798814")
qd = QueueDuration(hdr, time.Unix(1465798816, 0))
if qd != 2*time.Second {
t.Error(qd)
}
hdr = make(http.Header)
qd = QueueDuration(hdr, time.Unix(1465798816, 0))
if qd != 0 {
t.Error(qd)
}
hdr = make(http.Header)
hdr.Set("X-Request-Start", "invalid-time")
qd = QueueDuration(hdr, time.Unix(1465798816, 0))
if qd != 0 {
t.Error(qd)
}
hdr = make(http.Header)
hdr.Set("X-Queue-Start", "t=1465798814")
qd = QueueDuration(hdr, time.Unix(1465798816, 0))
if qd != 2*time.Second {
t.Error(qd)
}
// incorrect time order
hdr = make(http.Header)
hdr.Set("X-Queue-Start", "t=1465798816")
qd = QueueDuration(hdr, time.Unix(1465798814, 0))
if qd != 0 {
t.Error(qd)
}
}
|