scheduler_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 bloombits
  17. import (
  18. "bytes"
  19. "math/big"
  20. "math/rand"
  21. "sync"
  22. "sync/atomic"
  23. "testing"
  24. "time"
  25. )
  26. // Tests that the scheduler can deduplicate and forward retrieval requests to
  27. // underlying fetchers and serve responses back, irrelevant of the concurrency
  28. // of the requesting clients or serving data fetchers.
  29. func TestSchedulerSingleClientSingleFetcher(t *testing.T) { testScheduler(t, 1, 1, 5000) }
  30. func TestSchedulerSingleClientMultiFetcher(t *testing.T) { testScheduler(t, 1, 10, 5000) }
  31. func TestSchedulerMultiClientSingleFetcher(t *testing.T) { testScheduler(t, 10, 1, 5000) }
  32. func TestSchedulerMultiClientMultiFetcher(t *testing.T) { testScheduler(t, 10, 10, 5000) }
  33. func testScheduler(t *testing.T, clients int, fetchers int, requests int) {
  34. t.Parallel()
  35. f := newScheduler(0)
  36. // Create a batch of handler goroutines that respond to bloom bit requests and
  37. // deliver them to the scheduler.
  38. var fetchPend sync.WaitGroup
  39. fetchPend.Add(fetchers)
  40. defer fetchPend.Wait()
  41. fetch := make(chan *request, 16)
  42. defer close(fetch)
  43. var delivered uint32
  44. for i := 0; i < fetchers; i++ {
  45. go func() {
  46. defer fetchPend.Done()
  47. for req := range fetch {
  48. time.Sleep(time.Duration(rand.Intn(int(100 * time.Microsecond))))
  49. atomic.AddUint32(&delivered, 1)
  50. f.deliver([]uint64{
  51. req.section + uint64(requests), // Non-requested data (ensure it doesn't go out of bounds)
  52. req.section, // Requested data
  53. req.section, // Duplicated data (ensure it doesn't double close anything)
  54. }, [][]byte{
  55. {},
  56. new(big.Int).SetUint64(req.section).Bytes(),
  57. new(big.Int).SetUint64(req.section).Bytes(),
  58. })
  59. }
  60. }()
  61. }
  62. // Start a batch of goroutines to concurrently run scheduling tasks
  63. quit := make(chan struct{})
  64. var pend sync.WaitGroup
  65. pend.Add(clients)
  66. for i := 0; i < clients; i++ {
  67. go func() {
  68. defer pend.Done()
  69. in := make(chan uint64, 16)
  70. out := make(chan []byte, 16)
  71. f.run(in, fetch, out, quit, &pend)
  72. go func() {
  73. for j := 0; j < requests; j++ {
  74. in <- uint64(j)
  75. }
  76. close(in)
  77. }()
  78. b := new(big.Int)
  79. for j := 0; j < requests; j++ {
  80. bits := <-out
  81. if want := b.SetUint64(uint64(j)).Bytes(); !bytes.Equal(bits, want) {
  82. t.Errorf("vector %d: delivered content mismatch: have %x, want %x", j, bits, want)
  83. }
  84. }
  85. }()
  86. }
  87. pend.Wait()
  88. if have := atomic.LoadUint32(&delivered); int(have) != requests {
  89. t.Errorf("request count mismatch: have %v, want %v", have, requests)
  90. }
  91. }