@marcel91200 / caddy Mirror · read-only
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 01:00 UTC

master
go 67 lines 3.09 KB
Raw
1 package reverseproxy
2
3 import (
4 "testing"
5 "time"
6
7 "github.com/prometheus/client_golang/prometheus"
8 "github.com/prometheus/client_golang/prometheus/testutil"
9 )
10
11 func TestTrackActiveStreamRecordsLifecycleAndBytes(t *testing.T) {
12 const upstream = "127.0.0.1:7443"
13
14 // Use fresh metric vectors for deterministic assertions in this unit test.
15 reverseProxyMetrics.streamsActive = prometheus.NewGaugeVec(prometheus.GaugeOpts{}, []string{"upstream"})
16 reverseProxyMetrics.streamsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{"upstream", "result"})
17 reverseProxyMetrics.streamDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{}, []string{"upstream", "result"})
18 reverseProxyMetrics.streamBytes = prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{"upstream", "direction"})
19
20 finish := trackActiveStream(upstream)
21
22 if got := testutil.ToFloat64(reverseProxyMetrics.streamsActive.WithLabelValues(upstream)); got != 1 {
23 t.Fatalf("active streams = %v, want 1", got)
24 }
25
26 finish("closed", 150*time.Millisecond, 1234, 4321)
27
28 if got := testutil.ToFloat64(reverseProxyMetrics.streamsActive.WithLabelValues(upstream)); got != 0 {
29 t.Fatalf("active streams = %v, want 0", got)
30 }
31 if got := testutil.ToFloat64(reverseProxyMetrics.streamsTotal.WithLabelValues(upstream, "closed")); got != 1 {
32 t.Fatalf("streams_total closed = %v, want 1", got)
33 }
34 if got := testutil.ToFloat64(reverseProxyMetrics.streamBytes.WithLabelValues(upstream, "to_upstream")); got != 1234 {
35 t.Fatalf("bytes to_upstream = %v, want 1234", got)
36 }
37 if got := testutil.ToFloat64(reverseProxyMetrics.streamBytes.WithLabelValues(upstream, "from_upstream")); got != 4321 {
38 t.Fatalf("bytes from_upstream = %v, want 4321", got)
39 }
40
41 // A second finish call should be ignored by the once guard.
42 finish("error", 1*time.Second, 111, 222)
43 if got := testutil.ToFloat64(reverseProxyMetrics.streamsTotal.WithLabelValues(upstream, "error")); got != 0 {
44 t.Fatalf("streams_total error = %v, want 0", got)
45 }
46 }
47
48 func TestTrackActiveStreamDoesNotCountZeroBytes(t *testing.T) {
49 const upstream = "127.0.0.1:9000"
50
51 reverseProxyMetrics.streamsActive = prometheus.NewGaugeVec(prometheus.GaugeOpts{}, []string{"upstream"})
52 reverseProxyMetrics.streamsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{"upstream", "result"})
53 reverseProxyMetrics.streamDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{}, []string{"upstream", "result"})
54 reverseProxyMetrics.streamBytes = prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{"upstream", "direction"})
55
56 trackActiveStream(upstream)("timeout", 250*time.Millisecond, 0, 0)
57
58 if got := testutil.ToFloat64(reverseProxyMetrics.streamBytes.WithLabelValues(upstream, "to_upstream")); got != 0 {
59 t.Fatalf("bytes to_upstream = %v, want 0", got)
60 }
61 if got := testutil.ToFloat64(reverseProxyMetrics.streamBytes.WithLabelValues(upstream, "from_upstream")); got != 0 {
62 t.Fatalf("bytes from_upstream = %v, want 0", got)
63 }
64 if got := testutil.ToFloat64(reverseProxyMetrics.streamsTotal.WithLabelValues(upstream, "timeout")); got != 1 {
65 t.Fatalf("streams_total timeout = %v, want 1", got)
66 }
67 }