public void Only_The_Last_Matching_Handler_Is_Unsubscribed() { // Subscribe the same handlers multiple times var pub = new Publisher(); var calledSubscribers = new List <int>(); var sub1 = new InstanceSubscriber(1, calledSubscribers.Add); sub1.Subscribe(pub); sub1.Subscribe(pub); StaticSubscriber.CallCount = 0; StaticSubscriber.Subscribe(pub); StaticSubscriber.Subscribe(pub); // Make sure they really were subscribed pub.Raise(); calledSubscribers.Should().Equal(1, 1); StaticSubscriber.CallCount.Should().Be(2); // Unsubscribe one instance handler calledSubscribers.Clear(); sub1.Unsubscribe(pub); pub.Raise(); //calledSubscribers.Should().Equal(1); // Unsubscribe one static handler StaticSubscriber.CallCount = 0; StaticSubscriber.Unsubscribe(pub); pub.Raise(); StaticSubscriber.CallCount.Should().Be(1); // Make sure subscribers are not collected before the end of the test GC.KeepAlive(sub1); }
public void Handlers_Can_Be_Unsubscribed() { var pub = new Publisher(); var calledSubscribers = new List <int>(); var sub1 = new InstanceSubscriber(1, pub, calledSubscribers.Add); var sub2 = new InstanceSubscriber(2, pub, calledSubscribers.Add); StaticSubscriber.FooWasRaised = false; StaticSubscriber.Subscribe(pub); // Make sure they really were subscribed pub.Raise(); calledSubscribers.Should().Equal(1, 2); StaticSubscriber.FooWasRaised.Should().BeTrue(); calledSubscribers.Clear(); sub1.Unsubscribe(pub); pub.Raise(); calledSubscribers.Should().Equal(2); StaticSubscriber.FooWasRaised = false; StaticSubscriber.Unsubscribe(pub); pub.Raise(); StaticSubscriber.FooWasRaised.Should().BeFalse(); calledSubscribers.Clear(); sub2.Unsubscribe(pub); pub.Raise(); calledSubscribers.Should().BeEmpty(); // Make sure subscribers are not collected before the end of the test GC.KeepAlive(sub1); GC.KeepAlive(sub2); }
public void Static_Handlers_Are_Called() { var pub = new Publisher(); StaticSubscriber.FooWasRaised = false; StaticSubscriber.Subscribe(pub); pub.Raise(); StaticSubscriber.FooWasRaised.Should().BeTrue(); }
public async Task Static_Handlers_Are_Called() { var pub = new Publisher(); StaticSubscriber.CallCount = 0; StaticSubscriber.Subscribe(pub); await pub.Raise(); StaticSubscriber.CallCount.Should().Be(1); }
public async Task Handlers_Can_Be_Unsubscribed() { var pub = new Publisher(); var calledSubscribers = new List <int>(); var sub1 = new InstanceSubscriber(1, i => AddAsync(calledSubscribers, i)); sub1.Subscribe(pub); var sub2 = new InstanceSubscriber(2, i => AddAsync(calledSubscribers, i)); sub2.Subscribe(pub); StaticSubscriber.CallCount = 0; StaticSubscriber.Subscribe(pub); // Make sure they really were subscribed await pub.Raise(); calledSubscribers.Should().Equal(1, 2); StaticSubscriber.CallCount.Should().Be(1); calledSubscribers.Clear(); sub1.Unsubscribe(pub); await pub.Raise(); calledSubscribers.Should().Equal(2); StaticSubscriber.CallCount = 0; StaticSubscriber.Unsubscribe(pub); await pub.Raise(); StaticSubscriber.CallCount.Should().Be(0); calledSubscribers.Clear(); sub2.Unsubscribe(pub); await pub.Raise(); calledSubscribers.Should().BeEmpty(); // Make sure subscribers are not collected before the end of the test GC.KeepAlive(sub1); GC.KeepAlive(sub2); }
public void CannotRegisterStaticSubscriber() { StaticSubscriber subscriber = new StaticSubscriber(); topic.AddSubscription(subscriber, "StaticEventHandler", workItem, ThreadOption.Publisher); }