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