2018-05-22 10:26:03 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-10-22 17:32:05 +00:00
|
|
|
|
|
|
|
prom "github.com/prometheus/client_golang/prometheus"
|
2018-05-22 10:26:03 +00:00
|
|
|
)
|
|
|
|
|
2019-10-22 17:32:05 +00:00
|
|
|
func TestParsingLabelsFromNodeName(t *testing.T) {
|
|
|
|
var labels prom.Labels
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// mobile name
|
|
|
|
labels, err = labelsFromNodeName("StatusIM/v0.30.1-beta.2/android-arm/go1.12")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, labels,
|
|
|
|
prom.Labels{
|
|
|
|
"platform": "android-arm",
|
|
|
|
"type": "StatusIM",
|
|
|
|
"version": "v0.30.1-beta.2",
|
|
|
|
})
|
|
|
|
// desktop name
|
|
|
|
labels, err = labelsFromNodeName("Statusd/v0.29.0-beta.2/linux-amd64/go1.11")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, labels,
|
|
|
|
prom.Labels{
|
|
|
|
"platform": "linux-amd64",
|
|
|
|
"type": "Statusd",
|
|
|
|
"version": "v0.29.0-beta.2",
|
|
|
|
})
|
|
|
|
// missing version
|
|
|
|
labels, err = labelsFromNodeName("StatusIM/android-arm64/go1.11")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, labels,
|
|
|
|
prom.Labels{
|
|
|
|
"platform": "android-arm64",
|
|
|
|
"type": "StatusIM",
|
|
|
|
"version": "unknown",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-05-22 10:26:03 +00:00
|
|
|
func TestUpdateNodeMetricsPeersCounter(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
n, err := node.New(&node.Config{
|
|
|
|
P2P: p2p.Config{
|
|
|
|
MaxPeers: 10,
|
|
|
|
},
|
2019-02-01 17:02:52 +00:00
|
|
|
NoUSB: true,
|
2018-05-22 10:26:03 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, n.Start())
|
|
|
|
defer func() { require.NoError(t, n.Stop()) }()
|
2019-10-22 17:32:05 +00:00
|
|
|
server := n.Server()
|
2018-05-22 10:26:03 +00:00
|
|
|
|
2019-10-22 17:32:05 +00:00
|
|
|
change, err := computeMetrics(server, p2p.PeerEventTypeAdd)
|
2018-05-22 10:26:03 +00:00
|
|
|
require.NoError(t, err)
|
2019-10-22 17:32:05 +00:00
|
|
|
require.Equal(t, float64(1), change.Counter)
|
|
|
|
require.Equal(t, float64(10), change.Max)
|
2018-05-22 10:26:03 +00:00
|
|
|
|
|
|
|
// skip other events
|
2019-10-22 17:32:05 +00:00
|
|
|
change, err = computeMetrics(server, p2p.PeerEventTypeMsgRecv)
|
2018-05-22 10:26:03 +00:00
|
|
|
require.NoError(t, err)
|
2019-10-22 17:32:05 +00:00
|
|
|
require.Equal(t, float64(0), change.Counter)
|
|
|
|
change, err = computeMetrics(server, p2p.PeerEventTypeMsgSend)
|
2018-05-22 10:26:03 +00:00
|
|
|
require.NoError(t, err)
|
2019-10-22 17:32:05 +00:00
|
|
|
require.Equal(t, float64(0), change.Counter)
|
2018-05-22 10:26:03 +00:00
|
|
|
|
2019-10-22 17:32:05 +00:00
|
|
|
change, err = computeMetrics(server, p2p.PeerEventTypeDrop)
|
2018-05-22 10:26:03 +00:00
|
|
|
require.NoError(t, err)
|
2019-10-22 17:32:05 +00:00
|
|
|
require.Equal(t, float64(-1), change.Counter)
|
2018-05-22 10:26:03 +00:00
|
|
|
|
2019-10-22 17:32:05 +00:00
|
|
|
server.MaxPeers = 20
|
|
|
|
change, err = computeMetrics(server, p2p.PeerEventTypeDrop)
|
2018-05-22 10:26:03 +00:00
|
|
|
require.NoError(t, err)
|
2019-10-22 17:32:05 +00:00
|
|
|
require.Equal(t, float64(20), change.Max)
|
2018-05-22 10:26:03 +00:00
|
|
|
}
|