github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/core/bloombits/scheduler_test.go (about)

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