You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
3.2 KiB

lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
5 years ago
5 years ago
  1. package v2
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. type eventA struct {
  9. priorityNormal
  10. }
  11. var errDone = fmt.Errorf("done")
  12. func simpleHandler(event Event) (Event, error) {
  13. if _, ok := event.(eventA); ok {
  14. return noOp, errDone
  15. }
  16. return noOp, nil
  17. }
  18. func TestRoutineFinal(t *testing.T) {
  19. var (
  20. bufferSize = 10
  21. routine = newRoutine("simpleRoutine", simpleHandler, bufferSize)
  22. )
  23. assert.False(t, routine.isRunning(),
  24. "expected an initialized routine to not be running")
  25. go routine.start()
  26. <-routine.ready()
  27. assert.True(t, routine.isRunning(),
  28. "expected an started routine")
  29. assert.True(t, routine.send(eventA{}),
  30. "expected sending to a ready routine to succeed")
  31. assert.Equal(t, errDone, <-routine.final(),
  32. "expected the final event to be done")
  33. assert.False(t, routine.isRunning(),
  34. "expected an completed routine to no longer be running")
  35. }
  36. func TestRoutineStop(t *testing.T) {
  37. var (
  38. bufferSize = 10
  39. routine = newRoutine("simpleRoutine", simpleHandler, bufferSize)
  40. )
  41. assert.False(t, routine.send(eventA{}),
  42. "expected sending to an unstarted routine to fail")
  43. go routine.start()
  44. <-routine.ready()
  45. assert.True(t, routine.send(eventA{}),
  46. "expected sending to a running routine to succeed")
  47. routine.stop()
  48. assert.False(t, routine.send(eventA{}),
  49. "expected sending to a stopped routine to fail")
  50. }
  51. type finalCount struct {
  52. count int
  53. }
  54. func (f finalCount) Error() string {
  55. return "end"
  56. }
  57. func genStatefulHandler(maxCount int) handleFunc {
  58. counter := 0
  59. return func(event Event) (Event, error) {
  60. if _, ok := event.(eventA); ok {
  61. counter++
  62. if counter >= maxCount {
  63. return noOp, finalCount{counter}
  64. }
  65. return eventA{}, nil
  66. }
  67. return noOp, nil
  68. }
  69. }
  70. func feedback(r *Routine) {
  71. for event := range r.next() {
  72. r.send(event)
  73. }
  74. }
  75. func TestStatefulRoutine(t *testing.T) {
  76. var (
  77. count = 10
  78. handler = genStatefulHandler(count)
  79. bufferSize = 20
  80. routine = newRoutine("statefulRoutine", handler, bufferSize)
  81. )
  82. go routine.start()
  83. go feedback(routine)
  84. <-routine.ready()
  85. assert.True(t, routine.send(eventA{}),
  86. "expected sending to a started routine to succeed")
  87. final := <-routine.final()
  88. if fnl, ok := final.(finalCount); ok {
  89. assert.Equal(t, count, fnl.count,
  90. "expected the routine to count to 10")
  91. } else {
  92. t.Fail()
  93. }
  94. }
  95. type lowPriorityEvent struct {
  96. priorityLow
  97. }
  98. type highPriorityEvent struct {
  99. priorityHigh
  100. }
  101. func handleWithPriority(event Event) (Event, error) {
  102. switch event.(type) {
  103. case lowPriorityEvent:
  104. return noOp, nil
  105. case highPriorityEvent:
  106. return noOp, errDone
  107. }
  108. return noOp, nil
  109. }
  110. func TestPriority(t *testing.T) {
  111. var (
  112. bufferSize = 20
  113. routine = newRoutine("priorityRoutine", handleWithPriority, bufferSize)
  114. )
  115. go routine.start()
  116. <-routine.ready()
  117. go func() {
  118. for {
  119. routine.send(lowPriorityEvent{})
  120. time.Sleep(1 * time.Millisecond)
  121. }
  122. }()
  123. time.Sleep(10 * time.Millisecond)
  124. assert.True(t, routine.isRunning(),
  125. "expected an started routine")
  126. assert.True(t, routine.send(highPriorityEvent{}),
  127. "expected send to succeed even when saturated")
  128. assert.Equal(t, errDone, <-routine.final())
  129. assert.False(t, routine.isRunning(),
  130. "expected an started routine")
  131. }