github.com/klaytn/klaytn@v1.12.1/datasync/downloader/peer_test.go (about)

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