execqueue_test.go 1.6 KB

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