peer_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. package downloader
  17. import (
  18. "sort"
  19. "testing"
  20. )
  21. func TestPeerThroughputSorting(t *testing.T) {
  22. a := &peerConnection{
  23. id: "a",
  24. headerThroughput: 1.25,
  25. }
  26. b := &peerConnection{
  27. id: "b",
  28. headerThroughput: 1.21,
  29. }
  30. c := &peerConnection{
  31. id: "c",
  32. headerThroughput: 1.23,
  33. }
  34. peers := []*peerConnection{a, b, c}
  35. tps := []float64{a.headerThroughput,
  36. b.headerThroughput, c.headerThroughput}
  37. sortPeers := &peerThroughputSort{peers, tps}
  38. sort.Sort(sortPeers)
  39. if got, exp := sortPeers.p[0].id, "a"; got != exp {
  40. t.Errorf("sort fail, got %v exp %v", got, exp)
  41. }
  42. if got, exp := sortPeers.p[1].id, "c"; got != exp {
  43. t.Errorf("sort fail, got %v exp %v", got, exp)
  44. }
  45. if got, exp := sortPeers.p[2].id, "b"; got != exp {
  46. t.Errorf("sort fail, got %v exp %v", got, exp)
  47. }
  48. }