80 lines
2.8 KiB
Go
80 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
orbit "git.yuntuo.hk/k2safe/orbit/sdk/go"
|
|
)
|
|
|
|
func TestPercentileMSUsesNearestRankAndEmptyIsZero(t *testing.T) {
|
|
values := []time.Duration{10 * time.Millisecond, 20 * time.Millisecond, 30 * time.Millisecond, 40 * time.Millisecond}
|
|
if got := percentileMS(values, 0.50); got != 20 {
|
|
t.Fatalf("p50=%v, want 20", got)
|
|
}
|
|
if got := percentileMS(values, 0.95); got != 40 {
|
|
t.Fatalf("p95=%v, want 40", got)
|
|
}
|
|
if got := percentileMS(nil, 0.99); got != 0 {
|
|
t.Fatalf("empty percentile=%v, want 0", got)
|
|
}
|
|
}
|
|
|
|
func TestParseConfigClampsWorkersAndRejectsInvalidQueue(t *testing.T) {
|
|
queue := "0123456789abcdef0123456789abcdef"
|
|
cfg, err := parseConfig([]string{"-url", "http://127.0.0.1:8080", "-token", "token", "-queue", queue, "-requests", "3", "-concurrency", "8"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.Concurrency != 3 {
|
|
t.Fatalf("concurrency=%d, want clamp to requests", cfg.Concurrency)
|
|
}
|
|
if _, err := parseConfig([]string{"-url", "http://127.0.0.1:8080", "-token", "token", "-queue", "bad"}); err == nil {
|
|
t.Fatal("invalid queue accepted")
|
|
}
|
|
}
|
|
|
|
func TestErrorCodeDoesNotExposeTransportText(t *testing.T) {
|
|
if got := errorCode(&orbit.APIError{StatusCode: 409, Code: "IDEMPOTENCY_CONFLICT"}); got != "IDEMPOTENCY_CONFLICT" {
|
|
t.Fatalf("api code=%q", got)
|
|
}
|
|
if got := errorCode(context.DeadlineExceeded); got != "TIMEOUT" {
|
|
t.Fatalf("deadline code=%q", got)
|
|
}
|
|
if got := errorCode(errors.New("secret transport detail")); got != "TRANSPORT" {
|
|
t.Fatalf("transport code=%q", got)
|
|
}
|
|
}
|
|
|
|
func TestRunSamplesUsesOneRequestPerSequenceWithoutRetry(t *testing.T) {
|
|
var requests atomic.Int32
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost || r.URL.Path != "/v1/tasks" {
|
|
t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path)
|
|
}
|
|
requests.Add(1)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"request_id":"0123456789abcdef0123456789abcdef","data":{"task":{},"deduplicated":false}}`))
|
|
}))
|
|
defer server.Close()
|
|
client, err := orbit.NewClient(server.URL, orbit.Options{Token: "test-token"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg := sampleConfig{QueueID: "0123456789abcdef0123456789abcdef", Requests: 7, Concurrency: 3, Timeout: time.Second, Handler: "load.noop", HandlerVersion: "v1", PayloadVersion: "v1"}
|
|
results := runSamples(client, cfg, "0123456789abcdef0123456789abcdef")
|
|
if len(results) != cfg.Requests || int(requests.Load()) != cfg.Requests {
|
|
t.Fatalf("results=%d requests=%d, want %d", len(results), requests.Load(), cfg.Requests)
|
|
}
|
|
for _, result := range results {
|
|
if result.code != "" {
|
|
t.Fatalf("unexpected request failure: %s", result.code)
|
|
}
|
|
}
|
|
}
|