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

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 += 1
  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. }