Skip to content

Commit 1f0ee5e

Browse files
authored
chore: add tests on watcher (#5)
1 parent 31a1f35 commit 1f0ee5e

12 files changed

Lines changed: 546 additions & 50 deletions

File tree

go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/prometheus/client_golang v1.16.0
99
github.com/shopspring/decimal v1.3.1
1010
github.com/sirupsen/logrus v1.9.0
11+
github.com/stretchr/testify v1.8.4
1112
github.com/urfave/cli/v2 v2.25.7
1213
golang.org/x/sync v0.3.0
1314
)
@@ -16,16 +17,20 @@ require (
1617
github.com/beorn7/perks v1.0.1 // indirect
1718
github.com/cespare/xxhash/v2 v2.2.0 // indirect
1819
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
20+
github.com/davecgh/go-spew v1.1.1 // indirect
1921
github.com/golang/protobuf v1.5.3 // indirect
22+
github.com/kr/text v0.2.0 // indirect
2023
github.com/mattn/go-colorable v0.1.13 // indirect
2124
github.com/mattn/go-isatty v0.0.19 // indirect
2225
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
26+
github.com/pmezard/go-difflib v1.0.0 // indirect
2327
github.com/prometheus/client_model v0.3.0 // indirect
2428
github.com/prometheus/common v0.42.0 // indirect
2529
github.com/prometheus/procfs v0.10.1 // indirect
30+
github.com/rogpeppe/go-internal v1.11.0 // indirect
2631
github.com/russross/blackfriday/v2 v2.1.0 // indirect
27-
github.com/stretchr/testify v1.8.4 // indirect
2832
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
2933
golang.org/x/sys v0.12.0 // indirect
3034
google.golang.org/protobuf v1.31.0 // indirect
35+
gopkg.in/yaml.v3 v3.0.1 // indirect
3136
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj
66
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
77
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
88
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
9+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
910
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1011
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1112
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -18,6 +19,9 @@ github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg
1819
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
1920
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
2021
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
22+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
23+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
24+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2125
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
2226
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
2327
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
@@ -35,6 +39,8 @@ github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI
3539
github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc=
3640
github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+PymziUAg=
3741
github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM=
42+
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
43+
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
3844
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
3945
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
4046
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
@@ -63,5 +69,7 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
6369
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
6470
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
6571
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
72+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
6673
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6774
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
75+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/app/run.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package app
22

33
import (
44
"context"
5-
"net/http"
65
"os"
76
"os/signal"
87
"syscall"
@@ -56,10 +55,13 @@ func RunFunc(cCtx *cli.Context) error {
5655
// Validator Watcher
5756
//
5857
logrus.Infof("connecting to node %s", node)
58+
59+
client := near.NewClient(node)
60+
5961
registry := prometheus.NewRegistry()
6062
metrics := metrics.New(namespace)
6163
metrics.Register(registry)
62-
client := near.NewClient(node, &http.Client{})
64+
6365
watcher := watcher.New(client, metrics, &watcher.Config{
6466
Writer: os.Stdout,
6567
TrackedAccounts: validators,
@@ -73,9 +75,7 @@ func RunFunc(cCtx *cli.Context) error {
7375
// HTTP server
7476
//
7577
logrus.Infof("starting HTTP server on %s", httpAddr)
76-
readyProbe := func() bool {
77-
return !watcher.IsSyncing()
78-
}
78+
readyProbe := watcher.IsSynced
7979
httpServer := NewHTTPServer(
8080
httpAddr,
8181
WithReadyProbe(readyProbe),

pkg/metrics/metrics.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Metrics struct {
1414
NextValidatorStake *prometheus.GaugeVec
1515
PrevEpochKickout *prometheus.GaugeVec
1616
ProtocolVersion prometheus.Gauge
17-
SeatPriceDesc prometheus.Gauge
17+
SeatPrice prometheus.Gauge
1818
SyncingDesc prometheus.Gauge
1919
ValidatorExpectedBlocks *prometheus.GaugeVec
2020
ValidatorExpectedChunks *prometheus.GaugeVec
@@ -65,14 +65,14 @@ func New(namespace string) *Metrics {
6565
Namespace: namespace,
6666
Name: "prev_epoch_kickout",
6767
Help: "Near previous epoch kicked out validators"},
68-
[]string{"account_id", "public_key", "epoch_start_height", "tracked"},
68+
[]string{"account_id", "reason", "epoch_start_height", "tracked"},
6969
),
7070
ProtocolVersion: prometheus.NewGauge(prometheus.GaugeOpts{
7171
Namespace: namespace,
7272
Name: "protocol_version",
7373
Help: "Current protocol version deployed to the blockchain",
7474
}),
75-
SeatPriceDesc: prometheus.NewGauge(prometheus.GaugeOpts{
75+
SeatPrice: prometheus.NewGauge(prometheus.GaugeOpts{
7676
Namespace: namespace,
7777
Name: "seat_price",
7878
Help: "Validator seat price",
@@ -145,7 +145,7 @@ func (m *Metrics) Register(reg prometheus.Registerer) {
145145
reg.MustRegister(m.NextValidatorStake)
146146
reg.MustRegister(m.PrevEpochKickout)
147147
reg.MustRegister(m.ProtocolVersion)
148-
reg.MustRegister(m.SeatPriceDesc)
148+
reg.MustRegister(m.SeatPrice)
149149
reg.MustRegister(m.SyncingDesc)
150150
reg.MustRegister(m.ValidatorExpectedBlocks)
151151
reg.MustRegister(m.ValidatorExpectedChunks)

pkg/metrics/utils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
package metrics
22

3+
import "hash/fnv"
4+
35
func BoolToFloat64(b bool) float64 {
46
if b {
57
return 1
68
}
79
return 0
810
}
11+
12+
func StringToFloat64(s string) float64 {
13+
h := fnv.New32a()
14+
h.Write([]byte(s))
15+
return float64(h.Sum32())
16+
}

pkg/metrics/utils_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package metrics
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestBoolToFloat64(t *testing.T) {
10+
assert.Equal(t, float64(1), BoolToFloat64(true))
11+
assert.Equal(t, float64(0), BoolToFloat64(false))
12+
}
13+
14+
func TestStringToFloat64(t *testing.T) {
15+
assert.NotEqual(t, float64(0), StringToFloat64("foobar"))
16+
}

pkg/near/client.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,25 @@ type Response struct {
3838
} `json:"error"`
3939
}
4040

41-
func NewClient(endpoint string, httpClient *http.Client) *Client {
42-
return &Client{
41+
type Option func(*Client)
42+
43+
func WithHTTPClient(httpClient *http.Client) Option {
44+
return func(c *Client) {
45+
c.httpClient = httpClient
46+
}
47+
}
48+
49+
func NewClient(endpoint string, options ...Option) *Client {
50+
client := &Client{
4351
Endpoint: endpoint,
44-
httpClient: httpClient,
52+
httpClient: &http.Client{},
4553
}
54+
55+
for _, option := range options {
56+
option(client)
57+
}
58+
59+
return client
4660
}
4761

4862
func (c *Client) Request(ctx context.Context, method string, params interface{}) (*Response, error) {

pkg/near/testutils/server.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package testutils
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
)
7+
8+
type ExpectedResponse struct {
9+
StatusCode int
10+
Body []byte
11+
}
12+
13+
func (r *ExpectedResponse) ExpectResponse(statusCode int, body string) {
14+
r.StatusCode = statusCode
15+
r.Body = []byte(body)
16+
}
17+
18+
func NewServer(resp *ExpectedResponse) *httptest.Server {
19+
return httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
20+
res.WriteHeader(resp.StatusCode)
21+
res.Write(resp.Body)
22+
}))
23+
}

pkg/watcher/utils.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,9 @@ package watcher
22

33
import (
44
"fmt"
5-
"hash/fnv"
6-
"strconv"
75
"strings"
86
)
97

10-
func GetStakeFromString(s string) float64 {
11-
if len(s) == 1 {
12-
return 0
13-
}
14-
l := len(s) - 19 - 5
15-
v, err := strconv.ParseFloat(s[0:l], 64)
16-
if err != nil {
17-
fmt.Println(err)
18-
}
19-
return float64(v)
20-
}
21-
22-
func GetFloatFromString(s string) float64 {
23-
v, err := strconv.ParseFloat(s, 64)
24-
if err != nil {
25-
fmt.Println(err)
26-
return 0
27-
}
28-
return v
29-
}
30-
31-
func HashString(s string) uint32 {
32-
h := fnv.New32a()
33-
h.Write([]byte(s))
34-
return h.Sum32()
35-
}
36-
378
func prettyPrintFloat(f float64) string {
389
if f == float64(int(f)) {
3910
return fmt.Sprintf("%.0f", f)

pkg/watcher/utils_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package watcher
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestPrettyPrintFloat(t *testing.T) {
10+
assert.Equal(t, "100", prettyPrintFloat(100))
11+
assert.Equal(t, "99.42", prettyPrintFloat(99.42098176))
12+
}
13+
14+
func TestPrettyPrintAccountID(t *testing.T) {
15+
assert.Equal(t, "foo", prettyPrintAccountID("foo.poolv1.near"))
16+
assert.Equal(t, "foo", prettyPrintAccountID("foo.pool.near"))
17+
assert.Equal(t, "foo", prettyPrintAccountID("foo.pool.f863973.m0"))
18+
}

0 commit comments

Comments
 (0)