github.com/gochain-io/gochain@v2.2.26+incompatible/les/execqueue_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 les
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  )
    23  
    24  func TestExecQueue(t *testing.T) {
    25  	var (
    26  		N        = 10000
    27  		q        = newExecQueue(N)
    28  		counter  int
    29  		execd    = make(chan int)
    30  		testexit = make(chan struct{})
    31  	)
    32  	defer q.quit()
    33  	defer close(testexit)
    34  
    35  	check := func(state string, wantOK bool) {
    36  		c := counter
    37  		counter++
    38  		qf := func(context.Context) {
    39  			select {
    40  			case execd <- c:
    41  			case <-testexit:
    42  			}
    43  		}
    44  		if q.canQueue() != wantOK {
    45  			t.Fatalf("canQueue() == %t for %s", !wantOK, state)
    46  		}
    47  		if q.queue(qf) != wantOK {
    48  			t.Fatalf("canQueue() == %t for %s", !wantOK, state)
    49  		}
    50  	}
    51  
    52  	for i := 0; i < N; i++ {
    53  		check("queue below cap", true)
    54  	}
    55  	check("full queue", false)
    56  	for i := 0; i < N; i++ {
    57  		if c := <-execd; c != i {
    58  			t.Fatal("execution out of order")
    59  		}
    60  	}
    61  	q.quit()
    62  	check("closed queue", false)
    63  }