64 lines
1.8 KiB
Go
64 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A load generator must never report a fast failure page as good throughput.
|
|
func TestExchangeRejectsInvalidResponses(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name string
|
|
status int
|
|
body string
|
|
requestID string
|
|
truncated bool
|
|
wantOK bool
|
|
}{
|
|
{"valid", 200, expectedBody, "perfCheck", false, true},
|
|
{"overload", 503, "server busy", "perfCheck", false, false},
|
|
{"wrongPayload", 200, `{"data":"wrong"}`, "perfCheck", false, false},
|
|
{"missingRequestId", 200, expectedBody, "", false, false},
|
|
{"truncated", 200, expectedBody, "perfCheck", true, false},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Header.Get("X-Request-ID") != "perfCheck" {
|
|
t.Error("client did not send fixed requestId")
|
|
}
|
|
w.Header().Set("X-Request-ID", tt.requestID)
|
|
if tt.truncated {
|
|
w.Header().Set("Content-Length", "4096")
|
|
}
|
|
w.WriteHeader(tt.status)
|
|
_, _ = w.Write([]byte(tt.body))
|
|
}))
|
|
defer server.Close()
|
|
err := exchange(server.Client(), strings.TrimPrefix(server.URL, "http://"))
|
|
if (err == nil) != tt.wantOK {
|
|
t.Fatalf("valid=%v, error=%v", tt.wantOK, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPercentileBounds(t *testing.T) {
|
|
var h histogram
|
|
// Known ranks, including a tail much slower than the median.
|
|
for i := 1; i <= 100; i++ {
|
|
h.add(time.Duration(i) * time.Millisecond)
|
|
}
|
|
for _, p := range []uint64{50, 95, 99} {
|
|
value := h.percentile(100, p)
|
|
if value < float64(p) || value > float64(p)*1.0625 {
|
|
t.Fatalf("p%d=%gms outside advertised bucket rounding", p, value)
|
|
}
|
|
}
|
|
if value := h.percentile(0, 95); value != 0 {
|
|
t.Fatalf("empty histogram returned %g", value)
|
|
}
|
|
}
|