Official cwtch.im peer and server implementations. https://cwtch.im
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.

eventmanager_test.go 3.6KB

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