-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics_test.go
More file actions
81 lines (69 loc) · 2.18 KB
/
Copy pathmetrics_test.go
File metadata and controls
81 lines (69 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2024-2026 Blink Labs Software
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"strings"
"testing"
"time"
"github.com/blinklabs-io/handshake-node/peer"
)
func TestValidateLocalListeners(t *testing.T) {
local := []string{"127.0.0.1:12039", "[::1]:12039", "localhost:12039"}
if err := validateLocalListeners(local, "metricslisten",
"metricsallowpublic"); err != nil {
t.Fatalf("validateLocalListeners local: %v", err)
}
public := []string{"0.0.0.0:12039"}
if err := validateLocalListeners(public, "metricslisten",
"metricsallowpublic"); err == nil {
t.Fatalf("validateLocalListeners accepted public bind")
}
}
func TestSyncProgress(t *testing.T) {
peers := []*peer.StatsSnap{
{LastBlock: 100},
{LastBlock: 80},
}
if got := syncProgress(25, peers, false); got != 0.25 {
t.Fatalf("syncProgress = %v, want 0.25", got)
}
if got := syncProgress(25, peers, true); got != 1 {
t.Fatalf("syncProgress current = %v, want 1", got)
}
if got := syncProgress(25, nil, false); got != 0 {
t.Fatalf("syncProgress no peers = %v, want 0", got)
}
}
func TestPrometheusLabelEscaping(t *testing.T) {
var b strings.Builder
writeSample(&b, "handshake_p2p_messages_total", map[string]string{
"type": "bad\"type\\x",
}, 1)
got := b.String()
want := `type="bad\"type\\x"`
if !strings.Contains(got, want) {
t.Fatalf("writeSample = %q, want substring %q", got, want)
}
}
func TestBlockValidationMetricsUseSummaryAndGauges(t *testing.T) {
metrics := newNodeMetrics()
metrics.observeBlockValidation(2 * time.Second)
metrics.observeBlockValidation(3 * time.Second)
var b strings.Builder
writeBlockValidationMetrics(&b, metrics)
got := b.String()
for _, want := range []string{
"handshake_block_validation_seconds_count 2",
"handshake_block_validation_seconds_sum 5",
"handshake_block_validation_last_seconds 3",
"handshake_block_validation_max_seconds 3",
} {
if !strings.Contains(got, want) {
t.Fatalf("metrics output missing %q:\n%s", want, got)
}
}
if strings.Contains(got, "handshake_block_validation_seconds{") {
t.Fatalf("summary metric includes non-quantile labels:\n%s", got)
}
}