eventpump_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package pubsub
  11. import (
  12. "bytes"
  13. "errors"
  14. "fmt"
  15. "sort"
  16. "testing"
  17. )
  18. var res []string
  19. var source1 = &bytes.Buffer{}
  20. var errSource2 error = fmt.Errorf("TEST")
  21. var ep = NewEventPump()
  22. func addObservers2(t *testing.T) {
  23. // Add observer 4
  24. ep.AddObserver("", source1, func(event string, eventSource interface{}) {
  25. if eventSource != source1 {
  26. t.Error("Unexpected event source:", eventSource)
  27. return
  28. }
  29. res = append(res, "4")
  30. sort.Strings(res)
  31. })
  32. // Add observer 5
  33. ep.AddObserver("", nil, func(event string, eventSource interface{}) {
  34. res = append(res, "5")
  35. sort.Strings(res)
  36. })
  37. // Add observer 6
  38. ep.AddObserver("", errSource2, func(event string, eventSource interface{}) {
  39. if eventSource != errSource2 {
  40. t.Error("Unexpected event source:", eventSource)
  41. return
  42. }
  43. res = append(res, "6")
  44. sort.Strings(res)
  45. })
  46. }
  47. func TestEventPump(t *testing.T) {
  48. addObservers1(t)
  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", errSource2)
  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", errSource2)
  64. if fmt.Sprint(res) != "[]" {
  65. t.Error("Unexpected result:", res)
  66. return
  67. }
  68. addObservers2(t)
  69. res = make([]string, 0) // Reset res
  70. ep.PostEvent("event1", errSource2)
  71. if fmt.Sprint(res) != "[5 6]" {
  72. t.Error("Unexpected result:", res)
  73. return
  74. }
  75. res = make([]string, 0) // Reset res
  76. ep.PostEvent("event3", errSource2)
  77. if fmt.Sprint(res) != "[5 6]" {
  78. t.Error("Unexpected result:", res)
  79. return
  80. }
  81. res = make([]string, 0) // Reset res
  82. ep.PostEvent("event3", source1)
  83. if fmt.Sprint(res) != "[4 5]" {
  84. t.Error("Unexpected result:", res)
  85. return
  86. }
  87. res = make([]string, 0) // Reset res
  88. ep.PostEvent("event3", errors.New("test"))
  89. if fmt.Sprint(res) != "[5]" {
  90. t.Error("Unexpected result:", res)
  91. return
  92. }
  93. // Remove observers
  94. res = make([]string, 0) // Reset res
  95. ep.PostEvent("event2", errSource2)
  96. if fmt.Sprint(res) != "[2 3 5 6]" {
  97. t.Error("Unexpected result:", res)
  98. return
  99. }
  100. ep.RemoveObservers("event2", errSource2)
  101. res = make([]string, 0) // Reset res
  102. ep.PostEvent("event2", errSource2)
  103. if fmt.Sprint(res) != "[5 6]" {
  104. t.Error("Unexpected result:", res)
  105. return
  106. }
  107. ep.RemoveObservers("", errSource2) // Remove all handlers specific to source 2
  108. res = make([]string, 0) // Reset res
  109. ep.PostEvent("event2", errSource2)
  110. if fmt.Sprint(res) != "[5]" {
  111. t.Error("Unexpected result:", res)
  112. return
  113. }
  114. ep.PostEvent("event1", source1)
  115. if fmt.Sprint(res) != "[1 4 5 5]" {
  116. t.Error("Unexpected result:", res)
  117. return
  118. }
  119. ep.RemoveObservers("event1", nil) // Remove all handlers specific to source 2
  120. res = make([]string, 0) // Reset res
  121. ep.PostEvent("event2", errSource2)
  122. if fmt.Sprint(res) != "[5]" {
  123. t.Error("Unexpected result:", res)
  124. return
  125. }
  126. ep.RemoveObservers("", nil) // Remove all handlers
  127. res = make([]string, 0) // Reset res
  128. ep.PostEvent("event2", errSource2)
  129. if fmt.Sprint(res) != "[]" {
  130. t.Error("Unexpected result:", res)
  131. return
  132. }
  133. // This call should be ignored
  134. ep.AddObserver("event1", source1, nil)
  135. if fmt.Sprint(ep.eventsObservers) != "map[]" {
  136. t.Error("Event map should be empty at this point:", ep.eventsObservers)
  137. return
  138. }
  139. }
  140. func addObservers1(t *testing.T) {
  141. // Add observer 1
  142. ep.AddObserver("event1", source1, func(event string, eventSource interface{}) {
  143. if eventSource != source1 {
  144. t.Error("Unexpected event source:", eventSource)
  145. return
  146. }
  147. res = append(res, "1")
  148. sort.Strings(res)
  149. })
  150. // Add observer 2
  151. ep.AddObserver("event2", errSource2, func(event string, eventSource interface{}) {
  152. if eventSource != errSource2 {
  153. t.Error("Unexpected event source:", eventSource)
  154. return
  155. }
  156. res = append(res, "2")
  157. sort.Strings(res)
  158. })
  159. // Add observer 3
  160. ep.AddObserver("event2", errSource2, func(event string, eventSource interface{}) {
  161. if eventSource != errSource2 {
  162. t.Error("Unexpected event source:", eventSource)
  163. return
  164. }
  165. res = append(res, "3")
  166. sort.Strings(res)
  167. })
  168. }
  169. func TestWrongPostEvent(t *testing.T) {
  170. defer func() {
  171. if r := recover(); r == nil {
  172. t.Error("Posting events with empty values shouldn't work.")
  173. }
  174. }()
  175. ep := NewEventPump()
  176. ep.PostEvent("", nil)
  177. }