github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/les/execqueue_test.go (about)

     1  // Copyright 2017 The Spectrum Authors
     2  // This file is part of the Spectrum library.
     3  //
     4  // The Spectrum 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 Spectrum 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 Spectrum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package les
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestExecQueue(t *testing.T) {
    24  	var (
    25  		N        = 10000
    26  		q        = newExecQueue(N)
    27  		counter  int
    28  		execd    = make(chan int)
    29  		testexit = make(chan struct{})
    30  	)
    31  	defer q.quit()
    32  	defer close(testexit)
    33  
    34  	check := func(state string, wantOK bool) {
    35  		c := counter
    36  		counter++
    37  		qf := func() {
    38  			select {
    39  			case execd <- c:
    40  			case <-testexit:
    41  			}
    42  		}
    43  		if q.canQueue() != wantOK {
    44  			t.Fatalf("canQueue() == %t for %s", !wantOK, state)
    45  		}
    46  		if q.queue(qf) != wantOK {
    47  			t.Fatalf("canQueue() == %t for %s", !wantOK, state)
    48  		}
    49  	}
    50  
    51  	for i := 0; i < N; i++ {
    52  		check("queue below cap", true)
    53  	}
    54  	check("full queue", false)
    55  	for i := 0; i < N; i++ {
    56  		if c := <-execd; c != i {
    57  			t.Fatal("execution out of order")
    58  		}
    59  	}
    60  	q.quit()
    61  	check("closed queue", false)
    62  }