gitee.com/liu-zhao234568/cntest@v1.0.0/core/bloombits/scheduler_test.go (about)

     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  
    17  package bloombits
    18  
    19  import (
    20  	"bytes"
    21  	"math/big"
    22  	"math/rand"
    23  	"sync"
    24  	"sync/atomic"
    25  	"testing"
    26  	"time"
    27  )
    28  
    29  // Tests that the scheduler can deduplicate and forward retrieval requests to
    30  // underlying fetchers and serve responses back, irrelevant of the concurrency
    31  // of the requesting clients or serving data fetchers.
    32  func TestSchedulerSingleClientSingleFetcher(t *testing.T) { testScheduler(t, 1, 1, 5000) }
    33  func TestSchedulerSingleClientMultiFetcher(t *testing.T)  { testScheduler(t, 1, 10, 5000) }
    34  func TestSchedulerMultiClientSingleFetcher(t *testing.T)  { testScheduler(t, 10, 1, 5000) }
    35  func TestSchedulerMultiClientMultiFetcher(t *testing.T)   { testScheduler(t, 10, 10, 5000) }
    36  
    37  func testScheduler(t *testing.T, clients int, fetchers int, requests int) {
    38  	t.Parallel()
    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  			b := new(big.Int)
    93  			for j := 0; j < requests; j++ {
    94  				bits := <-out
    95  				if want := b.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  }