fetcher_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "encoding/binary"
  20. "math/rand"
  21. "sync"
  22. "sync/atomic"
  23. "testing"
  24. "time"
  25. )
  26. const testFetcherReqCount = 5000
  27. func fetcherTestVector(b uint, s uint64) []byte {
  28. r := make([]byte, 10)
  29. binary.BigEndian.PutUint16(r[0:2], uint16(b))
  30. binary.BigEndian.PutUint64(r[2:10], s)
  31. return r
  32. }
  33. func TestFetcher(t *testing.T) {
  34. testFetcher(t, 1)
  35. }
  36. func TestFetcherMultipleReaders(t *testing.T) {
  37. testFetcher(t, 10)
  38. }
  39. func testFetcher(t *testing.T, cnt int) {
  40. f := &fetcher{
  41. requestMap: make(map[uint64]fetchRequest),
  42. }
  43. distCh := make(chan distRequest, channelCap)
  44. stop := make(chan struct{})
  45. var reqCount uint32
  46. for i := 0; i < 10; i++ {
  47. go func() {
  48. for {
  49. req, ok := <-distCh
  50. if !ok {
  51. return
  52. }
  53. time.Sleep(time.Duration(rand.Intn(100000)))
  54. atomic.AddUint32(&reqCount, 1)
  55. f.deliver([]uint64{req.sectionIndex}, [][]byte{fetcherTestVector(req.bloomIndex, req.sectionIndex)})
  56. }
  57. }()
  58. }
  59. var wg, wg2 sync.WaitGroup
  60. for cc := 0; cc < cnt; cc++ {
  61. wg.Add(1)
  62. in := make(chan uint64, channelCap)
  63. out := f.fetch(in, distCh, stop, &wg2)
  64. time.Sleep(time.Millisecond * 10 * time.Duration(cc))
  65. go func() {
  66. for i := uint64(0); i < testFetcherReqCount; i++ {
  67. in <- i
  68. }
  69. }()
  70. go func() {
  71. for i := uint64(0); i < testFetcherReqCount; i++ {
  72. bv := <-out
  73. if !bytes.Equal(bv, fetcherTestVector(0, i)) {
  74. if len(bv) != 10 {
  75. t.Errorf("Vector #%d length is %d, expected 10", i, len(bv))
  76. } else {
  77. j := binary.BigEndian.Uint64(bv[2:10])
  78. t.Errorf("Expected vector #%d, fetched #%d", i, j)
  79. }
  80. }
  81. }
  82. wg.Done()
  83. }()
  84. }
  85. wg.Wait()
  86. close(stop)
  87. if reqCount != testFetcherReqCount {
  88. t.Errorf("Request count mismatch: expected %v, got %v", testFetcherReqCount, reqCount)
  89. }
  90. }