github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/core/bloombits/scheduler_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package bloombits 19 20 import ( 21 "bytes" 22 "math/big" 23 "math/rand" 24 "sync" 25 "sync/atomic" 26 "testing" 27 "time" 28 ) 29 30 // Tests that the scheduler can deduplicate and forward retrieval requests to 31 // underlying fetchers and serve responses back, irrelevant of the concurrency 32 // of the requesting clients or serving data fetchers. 33 func TestSchedulerSingleClientSingleFetcher(t *testing.T) { testScheduler(t, 1, 1, 5000) } 34 func TestSchedulerSingleClientMultiFetcher(t *testing.T) { testScheduler(t, 1, 10, 5000) } 35 func TestSchedulerMultiClientSingleFetcher(t *testing.T) { testScheduler(t, 10, 1, 5000) } 36 func TestSchedulerMultiClientMultiFetcher(t *testing.T) { testScheduler(t, 10, 10, 5000) } 37 38 func testScheduler(t *testing.T, clients int, fetchers int, requests int) { 39 f := newScheduler(0) 40 41 // Create a batch of handler goroutines that respond to bloom bit requests and 42 // deliver them to the scheduler. 43 var fetchPend sync.WaitGroup 44 fetchPend.Add(fetchers) 45 defer fetchPend.Wait() 46 47 fetch := make(chan *request, 16) 48 defer close(fetch) 49 50 var delivered uint32 51 for i := 0; i < fetchers; i++ { 52 go func() { 53 defer fetchPend.Done() 54 55 for req := range fetch { 56 time.Sleep(time.Duration(rand.Intn(int(100 * time.Microsecond)))) 57 atomic.AddUint32(&delivered, 1) 58 59 f.deliver([]uint64{ 60 req.section + uint64(requests), // Non-requested data (ensure it doesn't go out of bounds) 61 req.section, // Requested data 62 req.section, // Duplicated data (ensure it doesn't double close anything) 63 }, [][]byte{ 64 {}, 65 new(big.Int).SetUint64(req.section).Bytes(), 66 new(big.Int).SetUint64(req.section).Bytes(), 67 }) 68 } 69 }() 70 } 71 // Start a batch of goroutines to concurrently run scheduling tasks 72 quit := make(chan struct{}) 73 74 var pend sync.WaitGroup 75 pend.Add(clients) 76 77 for i := 0; i < clients; i++ { 78 go func() { 79 defer pend.Done() 80 81 in := make(chan uint64, 16) 82 out := make(chan []byte, 16) 83 84 f.run(in, fetch, out, quit, &pend) 85 86 go func() { 87 for j := 0; j < requests; j++ { 88 in <- uint64(j) 89 } 90 close(in) 91 }() 92 93 for j := 0; j < requests; j++ { 94 bits := <-out 95 if want := new(big.Int).SetUint64(uint64(j)).Bytes(); !bytes.Equal(bits, want) { 96 t.Errorf("vector %d: delivered content mismatch: have %x, want %x", j, bits, want) 97 } 98 } 99 }() 100 } 101 pend.Wait() 102 103 if have := atomic.LoadUint32(&delivered); int(have) != requests { 104 t.Errorf("request count mismatch: have %v, want %v", have, requests) 105 } 106 }