github.com/MetalBlockchain/subnet-evm@v0.4.9/core/bloombits/scheduler_test.go (about)

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