public void BasicˑTest() { var provider = new MyEventProvider(); var listener = new MyEventListener(); listener.RegisterˑMyEvent(provider); provider.RaiseˑMyEvent(); Assert.That(listener.EventCount, Is.EqualTo(1)); listener.ReleaseˑMyEvent(); provider.RaiseˑMyEvent(); Assert.That(listener.EventCount, Is.EqualTo(1)); }
public void ListenerGarbageCollectionˑTest() { var provider = new MyEventProvider(); var listener = new MyEventListener(); listener.RegisterˑMyEvent(provider); provider.RaiseˑMyEvent(); Assert.That(listener.EventCount, Is.EqualTo(1)); var c = EventHandle.StatisticsːInstancesˑCurrent; listener = null; ForceGarbageCollection(); provider.RaiseˑMyEvent(); Assert.That(((IEventSourceInternal)provider.MyEvent).GetContainers().Count, Is.EqualTo(0)); }
public void MassiveListenerGCTest() { var provider = new MyEventProvider(); var eventContainers = ((EventSource)provider.MyEvent).GetContainers(); var r = new System.Random(); var stopwatch = new Stopwatch(); stopwatch.Start(); const int max = 5000; var c = eventContainers.Count; for (int i = 1; i <= max; i++) { var listener = new MyEventListener(); listener.RegisterˑMyEvent(provider); provider.RaiseˑMyEvent(); Thread.Sleep(r.Next(8, 12 + 1)); if (eventContainers.Count + c < i) { //OK any listener has been collected Trace.WriteLine("GC collected at iteration " + i + " " + stopwatch.Elapsed + " " + eventContainers.Count + " alive"); c = i - eventContainers.Count; } listener.Dispose(); listener = null; } Trace.WriteLine(eventContainers.Count * 100 / max + "% alive after " + stopwatch.Elapsed); var next = stopwatch.Elapsed.Add(TimeSpan.FromSeconds(1)); while (eventContainers.Count > 0) { if (stopwatch.Elapsed < next) { Thread.Sleep(100); //ForceGarbageCollection(); provider.RaiseˑMyEvent(); continue; } Trace.WriteLine(eventContainers.Count * 100 / max + "% alive after " + stopwatch.Elapsed); next = stopwatch.Elapsed.Add(TimeSpan.FromSeconds(1)); } //Assert.Fail("Nothing collected!"); }
public void ProviderGarbageCollectionˑTest() { var provider = new MyEventProvider(); var listener = new MyEventListener(); listener.RegisterˑMyEvent(provider); provider.RaiseˑMyEvent(); Assert.That(listener.EventCount, Is.EqualTo(1)); var c = EventHandle.StatisticsːInstancesˑCurrent; provider = null; ForceGarbageCollection(); Assert.That(((EventHandle)listener._MyEventHandle).IsAlive, Is.EqualTo(false)); }