distributor_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2017 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 les
  17. import (
  18. "math/rand"
  19. "sync"
  20. "testing"
  21. "time"
  22. "github.com/ethereum/go-ethereum/common/mclock"
  23. )
  24. type testDistReq struct {
  25. cost, procTime, order uint64
  26. canSendTo map[*testDistPeer]struct{}
  27. }
  28. func (r *testDistReq) getCost(dp distPeer) uint64 {
  29. return r.cost
  30. }
  31. func (r *testDistReq) canSend(dp distPeer) bool {
  32. _, ok := r.canSendTo[dp.(*testDistPeer)]
  33. return ok
  34. }
  35. func (r *testDistReq) request(dp distPeer) func() {
  36. return func() { dp.(*testDistPeer).send(r) }
  37. }
  38. type testDistPeer struct {
  39. sent []*testDistReq
  40. sumCost uint64
  41. lock sync.RWMutex
  42. }
  43. func (p *testDistPeer) send(r *testDistReq) {
  44. p.lock.Lock()
  45. defer p.lock.Unlock()
  46. p.sent = append(p.sent, r)
  47. p.sumCost += r.cost
  48. }
  49. func (p *testDistPeer) worker(t *testing.T, checkOrder bool, stop chan struct{}) {
  50. var last uint64
  51. for {
  52. wait := time.Millisecond
  53. p.lock.Lock()
  54. if len(p.sent) > 0 {
  55. rq := p.sent[0]
  56. wait = time.Duration(rq.procTime)
  57. p.sumCost -= rq.cost
  58. if checkOrder {
  59. if rq.order <= last {
  60. t.Errorf("Requests processed in wrong order")
  61. }
  62. last = rq.order
  63. }
  64. p.sent = p.sent[1:]
  65. }
  66. p.lock.Unlock()
  67. select {
  68. case <-stop:
  69. return
  70. case <-time.After(wait):
  71. }
  72. }
  73. }
  74. const (
  75. testDistBufLimit = 10000000
  76. testDistMaxCost = 1000000
  77. testDistPeerCount = 2
  78. testDistReqCount = 10
  79. testDistMaxResendCount = 3
  80. )
  81. func (p *testDistPeer) waitBefore(cost uint64) (time.Duration, float64) {
  82. p.lock.RLock()
  83. sumCost := p.sumCost + cost
  84. p.lock.RUnlock()
  85. if sumCost < testDistBufLimit {
  86. return 0, float64(testDistBufLimit-sumCost) / float64(testDistBufLimit)
  87. }
  88. return time.Duration(sumCost - testDistBufLimit), 0
  89. }
  90. func (p *testDistPeer) canQueue() bool {
  91. return true
  92. }
  93. func (p *testDistPeer) queueSend(f func()) bool {
  94. f()
  95. return true
  96. }
  97. func TestRequestDistributor(t *testing.T) {
  98. testRequestDistributor(t, false)
  99. }
  100. func TestRequestDistributorResend(t *testing.T) {
  101. testRequestDistributor(t, true)
  102. }
  103. func testRequestDistributor(t *testing.T, resend bool) {
  104. stop := make(chan struct{})
  105. defer close(stop)
  106. dist := newRequestDistributor(nil, &mclock.System{})
  107. var peers [testDistPeerCount]*testDistPeer
  108. for i := range peers {
  109. peers[i] = &testDistPeer{}
  110. go peers[i].worker(t, !resend, stop)
  111. dist.registerTestPeer(peers[i])
  112. }
  113. // Disable the mechanism that we will wait a few time for request
  114. // even there is no suitable peer to send right now.
  115. waitForPeers = 0
  116. var wg sync.WaitGroup
  117. for i := 1; i <= testDistReqCount; i++ {
  118. cost := uint64(rand.Int63n(testDistMaxCost))
  119. procTime := uint64(rand.Int63n(int64(cost + 1)))
  120. rq := &testDistReq{
  121. cost: cost,
  122. procTime: procTime,
  123. order: uint64(i),
  124. canSendTo: make(map[*testDistPeer]struct{}),
  125. }
  126. for _, peer := range peers {
  127. if rand.Intn(2) != 0 {
  128. rq.canSendTo[peer] = struct{}{}
  129. }
  130. }
  131. wg.Add(1)
  132. req := &distReq{
  133. getCost: rq.getCost,
  134. canSend: rq.canSend,
  135. request: rq.request,
  136. }
  137. chn := dist.queue(req)
  138. go func() {
  139. cnt := 1
  140. if resend && len(rq.canSendTo) != 0 {
  141. cnt = rand.Intn(testDistMaxResendCount) + 1
  142. }
  143. for i := 0; i < cnt; i++ {
  144. if i != 0 {
  145. chn = dist.queue(req)
  146. }
  147. p := <-chn
  148. if p == nil {
  149. if len(rq.canSendTo) != 0 {
  150. t.Errorf("Request that could have been sent was dropped")
  151. }
  152. } else {
  153. peer := p.(*testDistPeer)
  154. if _, ok := rq.canSendTo[peer]; !ok {
  155. t.Errorf("Request sent to wrong peer")
  156. }
  157. }
  158. }
  159. wg.Done()
  160. }()
  161. if rand.Intn(1000) == 0 {
  162. time.Sleep(time.Duration(rand.Intn(5000000)))
  163. }
  164. }
  165. wg.Wait()
  166. }