Browse Source

tests for Network

pull/1943/head
Anton Kaliaev 8 years ago
parent
commit
1a65dbebb9
No known key found for this signature in database GPG Key ID: 7B6881D965918214
2 changed files with 104 additions and 5 deletions
  1. +9
    -5
      tm-monitor/network.go
  2. +95
    -0
      tm-monitor/network_test.go

+ 9
- 5
tm-monitor/network.go View File

@ -24,11 +24,11 @@ type Health int
const (
// FullHealth means all nodes online, synced, validators making blocks
FullHealth = iota
FullHealth = Health(0)
// ModerateHealth means we're making blocks
ModerateHealth
ModerateHealth = Health(1)
// Dead means we're not making blocks due to all validators freezing or crashing
Dead
Dead = Health(2)
)
// Common statistics for network of nodes
@ -82,7 +82,11 @@ func (n *Network) NewBlock(b tmtypes.Header) {
n.Height = uint64(b.Height)
n.blockTimeMeter.Mark(1)
n.AvgBlockTime = (1.0 / n.blockTimeMeter.Rate1()) * 1000 // 1/s to ms
if n.blockTimeMeter.Rate1() > 0.0 {
n.AvgBlockTime = (1.0 / n.blockTimeMeter.Rate1()) * 1000 // 1/s to ms
} else {
n.AvgBlockTime = 0.0
}
n.txThroughputMeter.Mark(int64(b.NumTxs))
n.AvgTxThroughput = n.txThroughputMeter.Rate1()
@ -127,7 +131,7 @@ func (n *Network) NodeIsDown(name string) {
n.mu.Lock()
defer n.mu.Unlock()
if online := n.nodeStatusMap[name]; online {
if online, ok := n.nodeStatusMap[name]; !ok || online {
n.nodeStatusMap[name] = false
n.NumNodesMonitoredOnline--
n.UptimeData.wentDown = time.Now()


+ 95
- 0
tm-monitor/network_test.go View File

@ -0,0 +1,95 @@
package main_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
monitor "github.com/tendermint/netmon/tm-monitor"
tmtypes "github.com/tendermint/tendermint/types"
)
func TestNetworkNewBlock(t *testing.T) {
assert := assert.New(t)
n := monitor.NewNetwork()
n.NewBlock(tmtypes.Header{Height: 5, NumTxs: 100})
assert.Equal(uint64(5), n.Height)
assert.Equal(0.0, n.AvgBlockTime)
assert.Equal(0.0, n.AvgTxThroughput)
}
func TestNetworkNewBlockLatency(t *testing.T) {
assert := assert.New(t)
n := monitor.NewNetwork()
n.NewBlockLatency(9000000.0) // nanoseconds
assert.Equal(0.0, n.AvgBlockLatency)
}
func TestNetworkNodeIsDownThenOnline(t *testing.T) {
assert := assert.New(t)
n := monitor.NewNetwork()
n.NewNode("test")
n.NodeIsDown("test")
assert.Equal(0, n.NumNodesMonitoredOnline)
assert.Equal(monitor.Dead, n.Health)
n.NodeIsDown("test")
assert.Equal(0, n.NumNodesMonitoredOnline)
n.NodeIsOnline("test")
assert.Equal(1, n.NumNodesMonitoredOnline)
// assert.Equal(monitor.FullHealth, n.Health)
// n.NodeIsOnline("test")
// assert.Equal(1, n.NumNodesMonitoredOnline)
}
func TestNetworkNewNode(t *testing.T) {
assert := assert.New(t)
n := monitor.NewNetwork()
assert.Equal(0, n.NumNodesMonitored)
assert.Equal(0, n.NumNodesMonitoredOnline)
n.NewNode("test")
assert.Equal(1, n.NumNodesMonitored)
assert.Equal(1, n.NumNodesMonitoredOnline)
}
func TestNetworkNodeDeleted(t *testing.T) {
assert := assert.New(t)
n := monitor.NewNetwork()
n.NewNode("test")
n.NodeDeleted("test")
assert.Equal(0, n.NumNodesMonitored)
assert.Equal(0, n.NumNodesMonitoredOnline)
}
func TestNetworkGetHealthString(t *testing.T) {
assert := assert.New(t)
n := monitor.NewNetwork()
assert.Equal("full", n.GetHealthString())
n.Health = monitor.ModerateHealth
assert.Equal("moderate", n.GetHealthString())
n.Health = monitor.Dead
assert.Equal("dead", n.GetHealthString())
}
func TestNetworkUptime(t *testing.T) {
assert := assert.New(t)
n := monitor.NewNetwork()
assert.Equal(100.0, n.Uptime())
}
func TestNetworkStartTime(t *testing.T) {
assert := assert.New(t)
n := monitor.NewNetwork()
assert.True(n.StartTime().Before(time.Now()))
}

Loading…
Cancel
Save