/// <summary> /// Subscribe to the given topic with a handler function. /// </summary> /// <param name="topic">pub-sub topic</param> public void Subscribe <T>(PubSubEventHandler <T> handler) where T : PubSubEvent { string topic = PubSubEvent.GetTopicForEventType(typeof(T)); MessageHandlers.Add(topic, new MessageHandler <T>(handler, this.Serializer)); string topicString = topic.ToString(); this.Socket.Subscribe(topicString); }
public void TestTopicUniqueness() { // Gather list of all PubSubEvent sub-classes. IEnumerable <Type> pubSubEventTypes = typeof(PubSubEvent).GetTypeInfo().Assembly.GetTypes() .Where(type => typeof(PubSubEvent).IsAssignableFrom(type) && type != typeof(PubSubEvent)); // Ensure all PubSubEvents have unique topics. HashSet <string> topics = new HashSet <string>(); foreach (Type eventType in pubSubEventTypes) { string topic = PubSubEvent.GetTopicForEventType(eventType); Assert.True(topics.Add(topic)); } }
public void TestTopicForNonSubclass() { // retrieving the topic for a class that doesn't inherit the pubsub event class should raise an error. Assert.Throws <ArgumentException>(() => PubSubEvent.GetTopicForEventType(typeof(TestEventNotSubclass))); }