github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/queuing_test.go (about)

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