public static void MixedIndividualOrCompleteAdditionRemoval() { var subscriber = new TestEventHandler(); var collection = new EventSubscriberCollection(); // add individually Assert.True(collection.Add <NamedEvent>(subscriber)); Assert.True(collection.Add <TestEvent>(subscriber)); // remove all Assert.True(collection.RemoveAll(subscriber)); // check individually Assert.False(collection.Remove <NamedEvent>(subscriber)); Assert.False(collection.Remove <TestEvent>(subscriber)); // add all Assert.True(collection.AddAll(subscriber)); // remove individually Assert.True(collection.Remove <NamedEvent>(subscriber)); Assert.True(collection.Remove <TestEvent>(subscriber)); // check all Assert.False(collection.RemoveAll(subscriber)); // add all Assert.True(collection.AddAll(subscriber)); // remove some Assert.True(collection.Remove <NamedEvent>(subscriber)); // remove rest Assert.True(collection.RemoveAll(subscriber)); // check Assert.False(collection.RemoveAll(subscriber)); }
public static void EventHandlingSuspension() { var queue = new TaskEventQueue(); Assert.False(queue.EventHandling.IsSuspended); var subscriber = new TestEventHandler(); queue.Subscribers.AddAll(subscriber); var evnt = new TestEvent(); queue.EventHandling.Suspend(); queue.Enqueue(evnt); Thread.Sleep(SleepTime); Assert.Null(subscriber.LastEventHandled); queue.EventHandling.Resume(); Thread.Sleep(SleepTime); Assert.AreSame(evnt, subscriber.LastEventHandled); queue.BeginShutdown(); }
public static void AddRemoveAllEventHandlers() { var subscriber = new TestEventHandler(); var collection = new EventSubscriberCollection(); // invalid argument tests Assert.Throws <ArgumentNullException>(() => collection.AddAll(null, weakRef: true)); Assert.Throws <ArgumentNullException>(() => collection.AddAll(null, weakRef: false)); Assert.Throws <ArgumentNullException>(() => collection.RemoveAll(null)); // can not remove, what's not there Assert.False(collection.RemoveAll(subscriber)); // add Assert.True(collection.AddAll(subscriber, weakRef: false)); Assert.False(collection.AddAll(subscriber, weakRef: false)); // subscribers only added once Assert.False(collection.AddAll(subscriber, weakRef: true)); // the type of reference used does not change this // remove Assert.True(collection.RemoveAll(subscriber)); Assert.False(collection.RemoveAll(subscriber)); }
public static void StrongSubscriberReference() { var collection = new EventSubscriberCollection(); WeakReference <TestEventHandler> weakRef; // register subscriber using strong references // then release our strong reference { var subscriber = new TestEventHandler(); weakRef = new WeakReference <TestEventHandler>(subscriber); collection.AddAll(subscriber, weakRef: false); subscriber = null; } // garbage collection GC.Collect(); GC.WaitForPendingFinalizers(); // we have proof that the collection did use strong references Assert.True(weakRef.TryGetTarget(out _)); GC.KeepAlive(collection); }