2019-12-20 14:54:59 +03:00
|
|
|
package autopilot
|
|
|
|
|
|
|
|
import (
|
2020-03-24 19:28:55 +03:00
|
|
|
"fmt"
|
2019-12-20 14:54:59 +03:00
|
|
|
"testing"
|
2020-06-17 20:23:42 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-12-20 14:54:59 +03:00
|
|
|
)
|
|
|
|
|
2020-03-24 19:28:55 +03:00
|
|
|
func TestBetweennessCentralityMetricConstruction(t *testing.T) {
|
|
|
|
failing := []int{-1, 0}
|
|
|
|
ok := []int{1, 10}
|
|
|
|
|
|
|
|
for _, workers := range failing {
|
|
|
|
m, err := NewBetweennessCentralityMetric(workers)
|
2020-06-17 20:23:42 +03:00
|
|
|
require.Error(
|
|
|
|
t, err, "construction must fail with <= 0 workers",
|
|
|
|
)
|
|
|
|
require.Nil(t, m)
|
2020-03-24 19:28:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, workers := range ok {
|
|
|
|
m, err := NewBetweennessCentralityMetric(workers)
|
2020-06-17 20:23:42 +03:00
|
|
|
require.NoError(
|
|
|
|
t, err, "construction must succeed with >= 1 workers",
|
|
|
|
)
|
|
|
|
require.NotNil(t, m)
|
2020-03-24 19:28:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 14:54:59 +03:00
|
|
|
// Tests that empty graph results in empty centrality result.
|
|
|
|
func TestBetweennessCentralityEmptyGraph(t *testing.T) {
|
2020-03-24 19:28:55 +03:00
|
|
|
centralityMetric, err := NewBetweennessCentralityMetric(1)
|
2020-06-17 20:23:42 +03:00
|
|
|
require.NoError(
|
|
|
|
t, err,
|
|
|
|
"construction must succeed with positive number of workers",
|
|
|
|
)
|
2019-12-20 14:54:59 +03:00
|
|
|
|
|
|
|
for _, chanGraph := range chanGraphs {
|
|
|
|
graph, cleanup, err := chanGraph.genFunc()
|
|
|
|
success := t.Run(chanGraph.name, func(t1 *testing.T) {
|
2020-06-17 20:23:42 +03:00
|
|
|
require.NoError(t, err, "unable to create graph")
|
|
|
|
|
2019-12-20 14:54:59 +03:00
|
|
|
if cleanup != nil {
|
|
|
|
defer cleanup()
|
|
|
|
}
|
|
|
|
|
2020-06-17 20:23:42 +03:00
|
|
|
err := centralityMetric.Refresh(graph)
|
|
|
|
require.NoError(t, err)
|
2019-12-20 14:54:59 +03:00
|
|
|
|
|
|
|
centrality := centralityMetric.GetMetric(false)
|
2020-06-17 20:23:42 +03:00
|
|
|
require.Equal(t, 0, len(centrality))
|
2019-12-20 14:54:59 +03:00
|
|
|
|
|
|
|
centrality = centralityMetric.GetMetric(true)
|
2020-06-17 20:23:42 +03:00
|
|
|
require.Equal(t, 0, len(centrality))
|
2019-12-20 14:54:59 +03:00
|
|
|
})
|
|
|
|
if !success {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test betweenness centrality calculating using an example graph.
|
|
|
|
func TestBetweennessCentralityWithNonEmptyGraph(t *testing.T) {
|
2020-03-24 19:28:55 +03:00
|
|
|
workers := []int{1, 3, 9, 100}
|
|
|
|
|
2020-06-17 20:23:42 +03:00
|
|
|
tests := []struct {
|
2019-12-20 14:54:59 +03:00
|
|
|
normalize bool
|
|
|
|
centrality []float64
|
|
|
|
}{
|
|
|
|
{
|
2020-06-17 19:03:31 +03:00
|
|
|
normalize: true,
|
|
|
|
centrality: normalizedTestGraphCentrality,
|
2019-12-20 14:54:59 +03:00
|
|
|
},
|
|
|
|
{
|
2020-06-17 19:03:31 +03:00
|
|
|
normalize: false,
|
|
|
|
centrality: testGraphCentrality,
|
2019-12-20 14:54:59 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-03-24 19:28:55 +03:00
|
|
|
for _, numWorkers := range workers {
|
|
|
|
for _, chanGraph := range chanGraphs {
|
|
|
|
numWorkers := numWorkers
|
|
|
|
graph, cleanup, err := chanGraph.genFunc()
|
2020-06-17 20:23:42 +03:00
|
|
|
require.NoError(t, err, "unable to create graph")
|
|
|
|
|
2020-03-24 19:28:55 +03:00
|
|
|
if cleanup != nil {
|
|
|
|
defer cleanup()
|
2019-12-20 14:54:59 +03:00
|
|
|
}
|
|
|
|
|
2020-06-17 20:23:42 +03:00
|
|
|
testName := fmt.Sprintf(
|
|
|
|
"%v %d workers", chanGraph.name, numWorkers,
|
|
|
|
)
|
|
|
|
|
2020-03-24 19:28:55 +03:00
|
|
|
success := t.Run(testName, func(t1 *testing.T) {
|
2020-06-17 20:23:42 +03:00
|
|
|
metric, err := NewBetweennessCentralityMetric(
|
2020-03-28 18:38:46 +03:00
|
|
|
numWorkers,
|
|
|
|
)
|
2020-06-17 20:23:42 +03:00
|
|
|
require.NoError(
|
|
|
|
t, err,
|
|
|
|
"construction must succeed with "+
|
|
|
|
"positive number of workers",
|
|
|
|
)
|
2020-03-24 19:28:55 +03:00
|
|
|
|
2020-06-17 20:23:42 +03:00
|
|
|
graphNodes := buildTestGraph(
|
|
|
|
t1, graph, centralityTestGraph,
|
|
|
|
)
|
2019-12-20 14:54:59 +03:00
|
|
|
|
2020-06-17 20:23:42 +03:00
|
|
|
err = metric.Refresh(graph)
|
|
|
|
require.NoError(t, err)
|
2019-12-20 14:54:59 +03:00
|
|
|
|
2020-06-17 20:23:42 +03:00
|
|
|
for _, expected := range tests {
|
|
|
|
expected := expected
|
|
|
|
centrality := metric.GetMetric(
|
|
|
|
expected.normalize,
|
|
|
|
)
|
|
|
|
|
|
|
|
require.Equal(t,
|
|
|
|
centralityTestGraph.nodes,
|
|
|
|
len(centrality),
|
|
|
|
)
|
|
|
|
|
|
|
|
for i, c := range expected.centrality {
|
|
|
|
nodeID := NewNodeID(
|
|
|
|
graphNodes[i],
|
|
|
|
)
|
|
|
|
result, ok := centrality[nodeID]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, c, result)
|
2019-12-20 14:54:59 +03:00
|
|
|
}
|
|
|
|
}
|
2020-03-24 19:28:55 +03:00
|
|
|
})
|
|
|
|
if !success {
|
|
|
|
break
|
2019-12-20 14:54:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|