exec_queue_test.go 1.6 KB

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