Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions cmd/sam-bench/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
package main

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
Comment on lines +18 to 25

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

3. Test coverage must follow the pyramid

Import time to allow setting a timeout on the context used in the integration test, adhering to Rule 3.5 of the Repository Style Guide.

Suggested change
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
References
  1. Rule 3.5: Integration tests must be time bounded. Look for unbounded waits, time.Sleep longer than a few hundred milliseconds, or polling without a deadline. Prefer context.WithTimeout and t.Deadline(). (link)


Expand Down Expand Up @@ -104,6 +110,38 @@ func TestGaugeIgnoresSourcesThatNeverReportedIt(t *testing.T) {
}
}

func TestWriteObservationWithNonFiniteMetrics(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("unavailable NaN\npositive +Inf\nnegative -Inf\nfinite 3\n"))
}))
defer server.Close()

metrics, err := scrapeAll(context.Background(), []string{server.URL})
if err != nil {
t.Fatalf("scrapeAll: %v", err)
}
Comment on lines +119 to +122

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

3. Test coverage must follow the pyramid

According to Rule 3.5 of the Repository Style Guide, integration tests must be time-bounded to avoid unbounded waits. Since scrapeAll performs HTTP requests, use a context with a timeout instead of context.Background().

Suggested change
metrics, err := scrapeAll(context.Background(), []string{server.URL})
if err != nil {
t.Fatalf("scrapeAll: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
metrics, err := scrapeAll(ctx, []string{server.URL})
if err != nil {
t.Fatalf("scrapeAll: %v", err)
}
References
  1. Rule 3.5: Integration tests must be time bounded. Look for unbounded waits, time.Sleep longer than a few hundred milliseconds, or polling without a deadline. Prefer context.WithTimeout and t.Deadline(). (link)

path := filepath.Join(t.TempDir(), "observation.json")
wantReport := &bench.Report{Requests: 1, Succeeded: 1}
if err := write(path, observation{Report: wantReport, After: metrics}); err != nil {
t.Fatalf("write: %v", err)
}

data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("ReadFile: %v", err)
}
var got observation
if err := json.Unmarshal(data, &got); err != nil {
t.Fatalf("json.Unmarshal: %v", err)
}
if got.Report == nil || got.Report.Requests != 1 || got.Report.Succeeded != 1 {
t.Errorf("report = %+v, want %+v", got.Report, wantReport)
}
if values := got.After[server.URL]; len(values) != 1 || values["finite"] != 3 {
t.Errorf("metrics = %v, want only finite=3", values)
}
}

func TestRenderPutsEveryObservationInTheTable(t *testing.T) {
observations := []observation{
{
Expand Down
5 changes: 3 additions & 2 deletions internal/bench/scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bufio"
"context"
"fmt"
"math"
"net/http"
"sort"
"strconv"
Expand Down Expand Up @@ -170,8 +171,8 @@ func parseExposition(r interface{ Read([]byte) (int, error) }) ([]Series, error)
continue
}
value, err := strconv.ParseFloat(fields[0], 64)
if err != nil {
// NaN and +Inf are legal values that carry no information here.
if err != nil || math.IsNaN(value) || math.IsInf(value, 0) {
// NaN and infinity are legal values that carry no information here.
continue
}

Expand Down
22 changes: 22 additions & 0 deletions internal/bench/scrape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package bench

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -85,6 +86,27 @@ func TestParseExpositionKeepsCommasInsideLabelValues(t *testing.T) {
}
}

func TestParseExpositionNonFinite(t *testing.T) {
series, err := parseExposition(strings.NewReader("unavailable NaN\npositive +Inf\nnegative -Inf\nfinite 3\nzero 0\nfinite_negative -2.5\n"))
if err != nil {
t.Fatalf("parseExposition: %v", err)
}

metrics := (&Snapshot{Series: series}).Flatten()
want := map[string]float64{"finite": 3, "zero": 0, "finite_negative": -2.5}
if len(metrics) != len(want) {
t.Fatalf("metrics = %v, want %v", metrics, want)
}
for name, value := range want {
if got, ok := metrics[name]; !ok || got != value {
t.Errorf("metric %q = %v (found %v), want %v", name, got, ok, value)
}
}
if _, err := json.Marshal(metrics); err != nil {
t.Fatalf("json.Marshal: %v", err)
}
}

func TestScrapeReadsALiveEndpoint(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("sam_node_requests_in_flight 3\n"))
Expand Down
Loading