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.
 
 
 
 

104 lines
3.5 KiB

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