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.

105 lines
3.6KB

  1. package event
  2. import (
  3. "git.openprivacy.ca/openprivacy/libricochet-go/log"
  4. "sync"
  5. "testing"
  6. "time"
  7. )
  8. // Most basic Manager Test, Initialize, Subscribe, Publish, Receive
  9. func TestEventManager(t *testing.T) {
  10. eventManager := NewEventManager()
  11. // We need to make this buffer at least 1, otherwise we will log an error!
  12. testChan := make(chan Event, 1)
  13. simpleQueue := &simpleQueue{testChan, sync.Mutex{}, false}
  14. eventManager.Subscribe("TEST", simpleQueue)
  15. eventManager.Publish(Event{EventType: "TEST", Data: map[Field]string{"Value": "Hello World"}})
  16. event := <-testChan
  17. if event.EventType == "TEST" && event.Data["Value"] == "Hello World" {
  18. } else {
  19. t.Errorf("Received Invalid Event")
  20. }
  21. eventManager.Shutdown()
  22. }
  23. // Most basic Manager Test, Initialize, Subscribe, Publish, Receive
  24. func TestEventManagerOverflow(t *testing.T) {
  25. eventManager := NewEventManager()
  26. // Explicitly setting this to 0 log an error!
  27. testChan := make(chan Event)
  28. simpleQueue := &simpleQueue{testChan, sync.Mutex{}, false}
  29. eventManager.Subscribe("TEST", simpleQueue)
  30. eventManager.Publish(Event{EventType: "TEST"})
  31. }
  32. func TestEventManagerMultiple(t *testing.T) {
  33. log.SetLevel(log.LevelDebug)
  34. eventManager := NewEventManager()
  35. groupEventQueue := NewQueue()
  36. peerEventQueue := NewQueue()
  37. allEventQueue := NewQueue()
  38. eventManager.Subscribe("PeerEvent", peerEventQueue)
  39. eventManager.Subscribe("GroupEvent", groupEventQueue)
  40. eventManager.Subscribe("PeerEvent", allEventQueue)
  41. eventManager.Subscribe("GroupEvent", allEventQueue)
  42. eventManager.Subscribe("ErrorEvent", allEventQueue)
  43. eventManager.Publish(Event{EventType: "PeerEvent", Data: map[Field]string{"Value": "Hello World Peer"}})
  44. eventManager.Publish(Event{EventType: "GroupEvent", Data: map[Field]string{"Value": "Hello World Group"}})
  45. eventManager.Publish(Event{EventType: "PeerEvent", Data: map[Field]string{"Value": "Hello World Peer"}})
  46. eventManager.Publish(Event{EventType: "ErrorEvent", Data: map[Field]string{"Value": "Hello World Error"}})
  47. eventManager.Publish(Event{EventType: "NobodyIsSubscribedToThisEvent", Data: map[Field]string{"Value": "Noone should see this!"}})
  48. assertLength := func(len int, expected int, label string) {
  49. if len != expected {
  50. t.Errorf("Expected %s to be %v was %v", label, expected, len)
  51. }
  52. }
  53. time.Sleep(time.Second)
  54. assertLength(groupEventQueue.Len(), 1, "Group Event Queue Length")
  55. assertLength(peerEventQueue.Len(), 2, "Peer Event Queue Length")
  56. assertLength(allEventQueue.Len(), 4, "All Event Queue Length")
  57. checkEvent := func(eventType Type, expected Type, label string) {
  58. if eventType != expected {
  59. t.Errorf("Expected %s to be %v was %v", label, expected, eventType)
  60. }
  61. }
  62. event := groupEventQueue.Next()
  63. checkEvent(event.EventType, "GroupEvent", "First Group Event")
  64. event = peerEventQueue.Next()
  65. checkEvent(event.EventType, "PeerEvent", "First Peer Event")
  66. event = peerEventQueue.Next()
  67. checkEvent(event.EventType, "PeerEvent", "Second Peer Event")
  68. event = allEventQueue.Next()
  69. checkEvent(event.EventType, "PeerEvent", "ALL: First Peer Event")
  70. event = allEventQueue.Next()
  71. checkEvent(event.EventType, "GroupEvent", "ALL: First Group Event")
  72. event = allEventQueue.Next()
  73. checkEvent(event.EventType, "PeerEvent", "ALL: Second Peer Event")
  74. event = allEventQueue.Next()
  75. checkEvent(event.EventType, "ErrorEvent", "ALL: First Error Event")
  76. eventManager.Shutdown()
  77. groupEventQueue.Shutdown()
  78. peerEventQueue.Shutdown()
  79. allEventQueue.Shutdown()
  80. // Reading from a closed queue should result in an instant return and an empty event
  81. event = groupEventQueue.Next()
  82. checkEvent(event.EventType, "", "Test Next() on Empty Queue")
  83. }