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. f := newScheduler(0)
  35. // Create a batch of handler goroutines that respond to bloom bit requests and
  36. // deliver them to the scheduler.
  37. var fetchPend sync.WaitGroup
  38. fetchPend.Add(fetchers)
  39. defer fetchPend.Wait()
  40. fetch := make(chan *request, 16)
  41. defer close(fetch)
  42. var delivered uint32
  43. for i := 0; i < fetchers; i++ {
  44. go func() {
  45. defer fetchPend.Done()
  46. for req := range fetch {
  47. time.Sleep(time.Duration(rand.Intn(int(100 * time.Microsecond))))
  48. atomic.AddUint32(&delivered, 1)
  49. f.deliver([]uint64{
  50. req.section + uint64(requests), // Non-requested data (ensure it doesn't go out of bounds)
  51. req.section, // Requested data
  52. req.section, // Duplicated data (ensure it doesn't double close anything)
  53. }, [][]byte{
  54. {},
  55. new(big.Int).SetUint64(req.section).Bytes(),
  56. new(big.Int).SetUint64(req.section).Bytes(),
  57. })
  58. }
  59. }()
  60. }
  61. // Start a batch of goroutines to concurrently run scheduling tasks
  62. quit := make(chan struct{})
  63. var pend sync.WaitGroup
  64. pend.Add(clients)
  65. for i := 0; i < clients; i++ {
  66. go func() {
  67. defer pend.Done()
  68. in := make(chan uint64, 16)
  69. out := make(chan []byte, 16)
  70. f.run(in, fetch, out, quit, &pend)
  71. go func() {
  72. for j := 0; j < requests; j++ {
  73. in <- uint64(j)
  74. }
  75. close(in)
  76. }()
  77. for j := 0; j < requests; j++ {
  78. bits := <-out
  79. if want := new(big.Int).SetUint64(uint64(j)).Bytes(); !bytes.Equal(bits, want) {
  80. t.Errorf("vector %d: delivered content mismatch: have %x, want %x", j, bits, want)
  81. }
  82. }
  83. }()
  84. }
  85. pend.Wait()
  86. if have := atomic.LoadUint32(&delivered); int(have) != requests {
  87. t.Errorf("request count mismatch: have %v, want %v", have, requests)
  88. }
  89. }