eventpump_test.go 4.4 KB

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