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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-04 15:21:24 +00:00
|
|
|
change, err := computeMetrics(n, p2p.PeerEventTypeAdd)
|
2018-05-22 10:26:03 +00:00
|
|
|
require.NoError(t, err)
|
2019-10-04 15:21:24 +00:00
|
|
|
require.Equal(t, int64(1), change.Counter)
|
|
|
|
require.Equal(t, int64(10), change.Max)
|
2018-05-22 10:26:03 +00:00
|
|
|
|
|
|
|
// skip other events
|
2019-10-04 15:21:24 +00:00
|
|
|
change, err = computeMetrics(n, p2p.PeerEventTypeMsgRecv)
|
2018-05-22 10:26:03 +00:00
|
|
|
require.NoError(t, err)
|
2019-10-04 15:21:24 +00:00
|
|
|
require.Equal(t, int64(0), change.Counter)
|
|
|
|
change, err = computeMetrics(n, p2p.PeerEventTypeMsgSend)
|
2018-05-22 10:26:03 +00:00
|
|
|
require.NoError(t, err)
|
2019-10-04 15:21:24 +00:00
|
|
|
require.Equal(t, int64(0), change.Counter)
|
2018-05-22 10:26:03 +00:00
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
change, err = computeMetrics(n, p2p.PeerEventTypeDrop)
|
2018-05-22 10:26:03 +00:00
|
|
|
require.NoError(t, err)
|
2019-10-04 15:21:24 +00:00
|
|
|
require.Equal(t, int64(-1), change.Counter)
|
2018-05-22 10:26:03 +00:00
|
|
|
|
|
|
|
n.Server().MaxPeers = 20
|
2019-10-04 15:21:24 +00:00
|
|
|
change, err = computeMetrics(n, p2p.PeerEventTypeDrop)
|
2018-05-22 10:26:03 +00:00
|
|
|
require.NoError(t, err)
|
2019-10-04 15:21:24 +00:00
|
|
|
require.Equal(t, int64(20), change.Max)
|
2018-05-22 10:26:03 +00:00
|
|
|
}
|