github.com/newrelic/go-agent@v3.26.0+incompatible/internal/queuing_test.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package internal
     5  
     6  import (
     7  	"net/http"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestParseQueueTime(t *testing.T) {
    13  	badInput := []string{
    14  		"",
    15  		"nope",
    16  		"t",
    17  		"0",
    18  		"0.0",
    19  		"9999999999999999999999999999999999999999999999999",
    20  		"-1368811467146000",
    21  		"3000000000",
    22  		"3000000000000",
    23  		"900000000",
    24  		"900000000000",
    25  	}
    26  	for _, s := range badInput {
    27  		if qt := parseQueueTime(s); !qt.IsZero() {
    28  			t.Error(s, qt)
    29  		}
    30  	}
    31  
    32  	testcases := []struct {
    33  		input  string
    34  		expect int64
    35  	}{
    36  		// Microseconds
    37  		{"1368811467146000", 1368811467},
    38  		// Milliseconds
    39  		{"1368811467146.000", 1368811467},
    40  		{"1368811467146", 1368811467},
    41  		// Seconds
    42  		{"1368811467.146000", 1368811467},
    43  		{"1368811467.146", 1368811467},
    44  		{"1368811467", 1368811467},
    45  	}
    46  	for _, tc := range testcases {
    47  		qt := parseQueueTime(tc.input)
    48  		if qt.Unix() != tc.expect {
    49  			t.Error(tc.input, tc.expect, qt, qt.UnixNano())
    50  		}
    51  	}
    52  }
    53  
    54  func TestQueueDuration(t *testing.T) {
    55  	hdr := make(http.Header)
    56  	hdr.Set("X-Queue-Start", "1465798814")
    57  	qd := QueueDuration(hdr, time.Unix(1465798816, 0))
    58  	if qd != 2*time.Second {
    59  		t.Error(qd)
    60  	}
    61  
    62  	hdr = make(http.Header)
    63  	hdr.Set("X-Request-Start", "1465798814")
    64  	qd = QueueDuration(hdr, time.Unix(1465798816, 0))
    65  	if qd != 2*time.Second {
    66  		t.Error(qd)
    67  	}
    68  
    69  	hdr = make(http.Header)
    70  	qd = QueueDuration(hdr, time.Unix(1465798816, 0))
    71  	if qd != 0 {
    72  		t.Error(qd)
    73  	}
    74  
    75  	hdr = make(http.Header)
    76  	hdr.Set("X-Request-Start", "invalid-time")
    77  	qd = QueueDuration(hdr, time.Unix(1465798816, 0))
    78  	if qd != 0 {
    79  		t.Error(qd)
    80  	}
    81  
    82  	hdr = make(http.Header)
    83  	hdr.Set("X-Queue-Start", "t=1465798814")
    84  	qd = QueueDuration(hdr, time.Unix(1465798816, 0))
    85  	if qd != 2*time.Second {
    86  		t.Error(qd)
    87  	}
    88  
    89  	// incorrect time order
    90  	hdr = make(http.Header)
    91  	hdr.Set("X-Queue-Start", "t=1465798816")
    92  	qd = QueueDuration(hdr, time.Unix(1465798814, 0))
    93  	if qd != 0 {
    94  		t.Error(qd)
    95  	}
    96  }