manager_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser 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. // The go-ethereum library 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 Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package flowcontrol
  17. import (
  18. "math/rand"
  19. "testing"
  20. "time"
  21. "github.com/ethereum/go-ethereum/common/mclock"
  22. )
  23. type testNode struct {
  24. node *ClientNode
  25. bufLimit, capacity uint64
  26. waitUntil mclock.AbsTime
  27. index, totalCost uint64
  28. }
  29. const (
  30. testMaxCost = 1000000
  31. testLength = 100000
  32. )
  33. // testConstantTotalCapacity simulates multiple request sender nodes and verifies
  34. // whether the total amount of served requests matches the expected value based on
  35. // the total capacity and the duration of the test.
  36. // Some nodes are sending requests occasionally so that their buffer should regularly
  37. // reach the maximum while other nodes (the "max capacity nodes") are sending at the
  38. // maximum permitted rate. The max capacity nodes are changed multiple times during
  39. // a single test.
  40. func TestConstantTotalCapacity(t *testing.T) {
  41. testConstantTotalCapacity(t, 10, 1, 0)
  42. testConstantTotalCapacity(t, 10, 1, 1)
  43. testConstantTotalCapacity(t, 30, 1, 0)
  44. testConstantTotalCapacity(t, 30, 2, 3)
  45. testConstantTotalCapacity(t, 100, 1, 0)
  46. testConstantTotalCapacity(t, 100, 3, 5)
  47. testConstantTotalCapacity(t, 100, 5, 10)
  48. }
  49. func testConstantTotalCapacity(t *testing.T, nodeCount, maxCapacityNodes, randomSend int) {
  50. clock := &mclock.Simulated{}
  51. nodes := make([]*testNode, nodeCount)
  52. var totalCapacity uint64
  53. for i := range nodes {
  54. nodes[i] = &testNode{capacity: uint64(50000 + rand.Intn(100000))}
  55. totalCapacity += nodes[i].capacity
  56. }
  57. m := NewClientManager(PieceWiseLinear{{0, totalCapacity}}, clock)
  58. for _, n := range nodes {
  59. n.bufLimit = n.capacity * 6000
  60. n.node = NewClientNode(m, ServerParams{BufLimit: n.bufLimit, MinRecharge: n.capacity})
  61. }
  62. maxNodes := make([]int, maxCapacityNodes)
  63. for i := range maxNodes {
  64. // we don't care if some indexes are selected multiple times
  65. // in that case we have fewer max nodes
  66. maxNodes[i] = rand.Intn(nodeCount)
  67. }
  68. var sendCount int
  69. for i := 0; i < testLength; i++ {
  70. now := clock.Now()
  71. for _, idx := range maxNodes {
  72. for nodes[idx].send(t, now) {
  73. }
  74. }
  75. if rand.Intn(testLength) < maxCapacityNodes*3 {
  76. maxNodes[rand.Intn(maxCapacityNodes)] = rand.Intn(nodeCount)
  77. }
  78. sendCount += randomSend
  79. failCount := randomSend * 10
  80. for sendCount > 0 && failCount > 0 {
  81. if nodes[rand.Intn(nodeCount)].send(t, now) {
  82. sendCount--
  83. } else {
  84. failCount--
  85. }
  86. }
  87. clock.Run(time.Millisecond)
  88. }
  89. var totalCost uint64
  90. for _, n := range nodes {
  91. totalCost += n.totalCost
  92. }
  93. ratio := float64(totalCost) / float64(totalCapacity) / testLength
  94. if ratio < 0.98 || ratio > 1.02 {
  95. t.Errorf("totalCost/totalCapacity/testLength ratio incorrect (expected: 1, got: %f)", ratio)
  96. }
  97. }
  98. func (n *testNode) send(t *testing.T, now mclock.AbsTime) bool {
  99. if now < n.waitUntil {
  100. return false
  101. }
  102. n.index++
  103. if ok, _, _ := n.node.AcceptRequest(0, n.index, testMaxCost); !ok {
  104. t.Fatalf("Rejected request after expected waiting time has passed")
  105. }
  106. rcost := uint64(rand.Int63n(testMaxCost))
  107. bv := n.node.RequestProcessed(0, n.index, testMaxCost, rcost)
  108. if bv < testMaxCost {
  109. n.waitUntil = now + mclock.AbsTime((testMaxCost-bv)*1001000/n.capacity)
  110. }
  111. n.totalCost += rcost
  112. return true
  113. }