github.com/phillinzzz/newBsc@v1.1.6/eth/downloader/peer_test.go (about)

     1  // Copyright 2020 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-ethereum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package downloader
    18  
    19  import (
    20  	"sort"
    21  	"testing"
    22  )
    23  
    24  func TestPeerThroughputSorting(t *testing.T) {
    25  	a := &peerConnection{
    26  		id:               "a",
    27  		headerThroughput: 1.25,
    28  	}
    29  	b := &peerConnection{
    30  		id:               "b",
    31  		headerThroughput: 1.21,
    32  	}
    33  	c := &peerConnection{
    34  		id:               "c",
    35  		headerThroughput: 1.23,
    36  	}
    37  
    38  	peers := []*peerConnection{a, b, c}
    39  	tps := []float64{a.headerThroughput,
    40  		b.headerThroughput, c.headerThroughput}
    41  	sortPeers := &peerThroughputSort{peers, tps}
    42  	sort.Sort(sortPeers)
    43  	if got, exp := sortPeers.p[0].id, "a"; got != exp {
    44  		t.Errorf("sort fail, got %v exp %v", got, exp)
    45  	}
    46  	if got, exp := sortPeers.p[1].id, "c"; got != exp {
    47  		t.Errorf("sort fail, got %v exp %v", got, exp)
    48  	}
    49  	if got, exp := sortPeers.p[2].id, "b"; got != exp {
    50  		t.Errorf("sort fail, got %v exp %v", got, exp)
    51  	}
    52  
    53  }