eventpump_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package flowutil
  10. import (
  11. "bytes"
  12. "errors"
  13. "fmt"
  14. "sort"
  15. "testing"
  16. )
  17. func TestEventPump(t *testing.T) {
  18. var res []string
  19. source1 := &bytes.Buffer{}
  20. source2 := errors.New("TEST")
  21. ep := NewEventPump()
  22. // Add observer 1
  23. ep.AddObserver("event1", source1, func(event string, eventSource interface{}) {
  24. if eventSource != source1 {
  25. t.Error("Unexpected event source:", eventSource)
  26. return
  27. }
  28. res = append(res, "1")
  29. sort.Strings(res)
  30. })
  31. // Add observer 2
  32. ep.AddObserver("event2", source2, func(event string, eventSource interface{}) {
  33. if eventSource != source2 {
  34. t.Error("Unexpected event source:", eventSource)
  35. return
  36. }
  37. res = append(res, "2")
  38. sort.Strings(res)
  39. })
  40. // Add observer 3
  41. ep.AddObserver("event2", source2, func(event string, eventSource interface{}) {
  42. if eventSource != source2 {
  43. t.Error("Unexpected event source:", eventSource)
  44. return
  45. }
  46. res = append(res, "3")
  47. sort.Strings(res)
  48. })
  49. // Run the tests
  50. // Test 1 straight forward case
  51. ep.PostEvent("event1", source1)
  52. if fmt.Sprint(res) != "[1]" {
  53. t.Error("Unexpected result:", res)
  54. return
  55. }
  56. res = make([]string, 0) // Reset res
  57. ep.PostEvent("event2", source2)
  58. if fmt.Sprint(res) != "[2 3]" {
  59. t.Error("Unexpected result:", res)
  60. return
  61. }
  62. res = make([]string, 0) // Reset res
  63. ep.PostEvent("event1", source2)
  64. if fmt.Sprint(res) != "[]" {
  65. t.Error("Unexpected result:", res)
  66. return
  67. }
  68. // Add observer 4
  69. ep.AddObserver("", source1, func(event string, eventSource interface{}) {
  70. if eventSource != source1 {
  71. t.Error("Unexpected event source:", eventSource)
  72. return
  73. }
  74. res = append(res, "4")
  75. sort.Strings(res)
  76. })
  77. // Add observer 5
  78. ep.AddObserver("", nil, func(event string, eventSource interface{}) {
  79. res = append(res, "5")
  80. sort.Strings(res)
  81. })
  82. // Add observer 6
  83. ep.AddObserver("", source2, func(event string, eventSource interface{}) {
  84. if eventSource != source2 {
  85. t.Error("Unexpected event source:", eventSource)
  86. return
  87. }
  88. res = append(res, "6")
  89. sort.Strings(res)
  90. })
  91. res = make([]string, 0) // Reset res
  92. ep.PostEvent("event1", source2)
  93. if fmt.Sprint(res) != "[5 6]" {
  94. t.Error("Unexpected result:", res)
  95. return
  96. }
  97. res = make([]string, 0) // Reset res
  98. ep.PostEvent("event3", source2)
  99. if fmt.Sprint(res) != "[5 6]" {
  100. t.Error("Unexpected result:", res)
  101. return
  102. }
  103. res = make([]string, 0) // Reset res
  104. ep.PostEvent("event3", source1)
  105. if fmt.Sprint(res) != "[4 5]" {
  106. t.Error("Unexpected result:", res)
  107. return
  108. }
  109. res = make([]string, 0) // Reset res
  110. ep.PostEvent("event3", errors.New("test"))
  111. if fmt.Sprint(res) != "[5]" {
  112. t.Error("Unexpected result:", res)
  113. return
  114. }
  115. // Remove observers
  116. res = make([]string, 0) // Reset res
  117. ep.PostEvent("event2", source2)
  118. if fmt.Sprint(res) != "[2 3 5 6]" {
  119. t.Error("Unexpected result:", res)
  120. return
  121. }
  122. ep.RemoveObservers("event2", source2)
  123. res = make([]string, 0) // Reset res
  124. ep.PostEvent("event2", source2)
  125. if fmt.Sprint(res) != "[5 6]" {
  126. t.Error("Unexpected result:", res)
  127. return
  128. }
  129. ep.RemoveObservers("", source2) // Remove all handlers specific to source 2
  130. res = make([]string, 0) // Reset res
  131. ep.PostEvent("event2", source2)
  132. if fmt.Sprint(res) != "[5]" {
  133. t.Error("Unexpected result:", res)
  134. return
  135. }
  136. ep.PostEvent("event1", source1)
  137. if fmt.Sprint(res) != "[1 4 5 5]" {
  138. t.Error("Unexpected result:", res)
  139. return
  140. }
  141. ep.RemoveObservers("event1", nil) // Remove all handlers specific to source 2
  142. res = make([]string, 0) // Reset res
  143. ep.PostEvent("event2", source2)
  144. if fmt.Sprint(res) != "[5]" {
  145. t.Error("Unexpected result:", res)
  146. return
  147. }
  148. ep.RemoveObservers("", nil) // Remove all handlers
  149. res = make([]string, 0) // Reset res
  150. ep.PostEvent("event2", source2)
  151. if fmt.Sprint(res) != "[]" {
  152. t.Error("Unexpected result:", res)
  153. return
  154. }
  155. // This call should be ignored
  156. ep.AddObserver("event1", source1, nil)
  157. if fmt.Sprint(ep.eventsObservers) != "map[]" {
  158. t.Error("Event map should be empty at this point:", ep.eventsObservers)
  159. return
  160. }
  161. }
  162. func TestWrongPostEvent(t *testing.T) {
  163. defer func() {
  164. if r := recover(); r == nil {
  165. t.Error("Posting events with empty values shouldn't work.")
  166. }
  167. }()
  168. ep := NewEventPump()
  169. ep.PostEvent("", nil)
  170. }