github.com/decred/dcrlnd@v0.7.6/autopilot/betweenness_centrality_test.go (about) 1 package autopilot 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 ) 9 10 func TestBetweennessCentralityMetricConstruction(t *testing.T) { 11 failing := []int{-1, 0} 12 ok := []int{1, 10} 13 14 for _, workers := range failing { 15 m, err := NewBetweennessCentralityMetric(workers) 16 require.Error( 17 t, err, "construction must fail with <= 0 workers", 18 ) 19 require.Nil(t, m) 20 } 21 22 for _, workers := range ok { 23 m, err := NewBetweennessCentralityMetric(workers) 24 require.NoError( 25 t, err, "construction must succeed with >= 1 workers", 26 ) 27 require.NotNil(t, m) 28 } 29 } 30 31 // Tests that empty graph results in empty centrality result. 32 func TestBetweennessCentralityEmptyGraph(t *testing.T) { 33 centralityMetric, err := NewBetweennessCentralityMetric(1) 34 require.NoError( 35 t, err, 36 "construction must succeed with positive number of workers", 37 ) 38 39 for _, chanGraph := range chanGraphs { 40 graph, cleanup, err := chanGraph.genFunc() 41 success := t.Run(chanGraph.name, func(t1 *testing.T) { 42 require.NoError(t, err, "unable to create graph") 43 44 if cleanup != nil { 45 defer cleanup() 46 } 47 48 err := centralityMetric.Refresh(graph) 49 require.NoError(t, err) 50 51 centrality := centralityMetric.GetMetric(false) 52 require.Equal(t, 0, len(centrality)) 53 54 centrality = centralityMetric.GetMetric(true) 55 require.Equal(t, 0, len(centrality)) 56 }) 57 if !success { 58 break 59 } 60 } 61 } 62 63 // Test betweenness centrality calculating using an example graph. 64 func TestBetweennessCentralityWithNonEmptyGraph(t *testing.T) { 65 workers := []int{1, 3, 9, 100} 66 67 tests := []struct { 68 normalize bool 69 centrality []float64 70 }{ 71 { 72 normalize: true, 73 centrality: normalizedTestGraphCentrality, 74 }, 75 { 76 normalize: false, 77 centrality: testGraphCentrality, 78 }, 79 } 80 81 for _, numWorkers := range workers { 82 for _, chanGraph := range chanGraphs { 83 numWorkers := numWorkers 84 graph, cleanup, err := chanGraph.genFunc() 85 require.NoError(t, err, "unable to create graph") 86 87 if cleanup != nil { 88 defer cleanup() 89 } 90 91 testName := fmt.Sprintf( 92 "%v %d workers", chanGraph.name, numWorkers, 93 ) 94 95 success := t.Run(testName, func(t1 *testing.T) { 96 metric, err := NewBetweennessCentralityMetric( 97 numWorkers, 98 ) 99 require.NoError( 100 t, err, 101 "construction must succeed with "+ 102 "positive number of workers", 103 ) 104 105 graphNodes := buildTestGraph( 106 t1, graph, centralityTestGraph, 107 ) 108 109 err = metric.Refresh(graph) 110 require.NoError(t, err) 111 112 for _, expected := range tests { 113 expected := expected 114 centrality := metric.GetMetric( 115 expected.normalize, 116 ) 117 118 require.Equal(t, 119 centralityTestGraph.nodes, 120 len(centrality), 121 ) 122 123 for i, c := range expected.centrality { 124 nodeID := NewNodeID( 125 graphNodes[i], 126 ) 127 result, ok := centrality[nodeID] 128 require.True(t, ok) 129 require.Equal(t, c, result) 130 } 131 } 132 }) 133 if !success { 134 break 135 } 136 } 137 } 138 }