public void NotCollectedIfSubscriberIsNotTheCallbackTarget() { WeakReference wr = null; new Action(() => { var subscriber = new TestSubcriber(); wr = new WeakReference(subscriber); // This creates a closure, so the callback target is not 'subscriber', but an instancce of a compiler generated class // So MC has to keep a strong reference to it, and 'subscriber' won't be collectable SafeMessagingCenter.Subscribe <TestPublisher>(subscriber, "test", p => subscriber.SetSuccess()); })(); GC.Collect(); GC.WaitForPendingFinalizers(); Assert.True(wr.IsAlive); // The closure in Subscribe should be keeping the subscriber alive Assert.NotNull(wr.Target as TestSubcriber); Assert.False(((TestSubcriber)wr.Target).Successful); var pub = new TestPublisher(); pub.Test(); Assert.True(((TestSubcriber)wr.Target).Successful); // Since it's still alive, the subscriber should still have received the message and updated the property }
public void Unsubscribe() { string sentMessage = null; SafeMessagingCenter.Subscribe <SafeMessagingCenterTests, string>(this, "SimpleTest", (sender, args) => sentMessage = args); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests, string>(this, "SimpleTest"); SafeMessagingCenter.Send(this, "SimpleTest", "My Message"); Assert.Null(sentMessage); }
public void NoArgUnsubscribe() { bool sentMessage = false; SafeMessagingCenter.Subscribe <SafeMessagingCenterTests>(this, "SimpleTest", (sender) => sentMessage = true); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests>(this, "SimpleTest"); SafeMessagingCenter.Send(this, "SimpleTest", "My Message"); Assert.False(sentMessage); }
public void NoArgSingleSubscriber() { bool sentMessage = false; SafeMessagingCenter.Subscribe <SafeMessagingCenterTests>(this, "SimpleTest", sender => sentMessage = true); SafeMessagingCenter.Send(this, "SimpleTest"); Assert.True(sentMessage); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests>(this, "SimpleTest"); }
public void SubscriberShouldBeCollected() { new Action(() => { var subscriber = new TestSubcriber(); SafeMessagingCenter.Subscribe <TestPublisher>(subscriber, "test", p => Assert.True(false)); //ie Assert.Fail })(); GC.Collect(); GC.WaitForPendingFinalizers(); var pub = new TestPublisher(); pub.Test(); // Assert.Fail() shouldn't be called, because the TestSubcriber object should have ben GCed }
public void Filter() { string sentMessage = null; SafeMessagingCenter.Subscribe <SafeMessagingCenterTests, string>(this, "SimpleTest", callback: (sender, args) => sentMessage = args, source: this); SafeMessagingCenter.Send(new SafeMessagingCenterTests(), "SimpleTest", "My Message"); Assert.Null(sentMessage); SafeMessagingCenter.Send(this, "SimpleTest", "My Message"); Assert.Equal("My Message", sentMessage); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests, string>(this, "SimpleTest"); }
public void MultipleSubscribersOfTheSameClass() { var sub1 = new object(); var sub2 = new object(); string args2 = null; const string message = "message"; SafeMessagingCenter.Subscribe <SafeMessagingCenterTests, string>(sub1, message, (sender, args) => { }); SafeMessagingCenter.Subscribe <SafeMessagingCenterTests, string>(sub2, message, (sender, args) => args2 = args); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests, string>(sub1, message); SafeMessagingCenter.Send(this, message, "Testing"); Assert.Equal("Testing", args2); //unsubscribing sub1 should not unsubscribe sub2 }
public void NoArgFilter() { bool sentMessage = false; SafeMessagingCenter.Subscribe(this, "SimpleTest", (sender) => sentMessage = true, source: this); SafeMessagingCenter.Send(new SafeMessagingCenterTests(), "SimpleTest"); Assert.False(sentMessage); SafeMessagingCenter.Send(this, "SimpleTest"); Assert.True(sentMessage); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests>(this, "SimpleTest"); }
public void StaticCallback() { int i = 4; _subscriber = new TestSubcriber(); // Using a class member so it doesn't get optimized away in Release build SafeMessagingCenter.Subscribe <TestPublisher>(_subscriber, "test", p => MessagingCenterTestsCallbackSource.Increment(ref i)); GC.Collect(); GC.WaitForPendingFinalizers(); var pub = new TestPublisher(); pub.Test(); Assert.True(i == 5, "The static method should have incremented 'i'"); }
public void MultiSubscriber() { var sub1 = new object(); var sub2 = new object(); string sentMessage1 = null; string sentMessage2 = null; SafeMessagingCenter.Subscribe <SafeMessagingCenterTests, string>(sub1, "SimpleTest", (sender, args) => sentMessage1 = args); SafeMessagingCenter.Subscribe <SafeMessagingCenterTests, string>(sub2, "SimpleTest", (sender, args) => sentMessage2 = args); SafeMessagingCenter.Send(this, "SimpleTest", "My Message"); Assert.Equal("My Message", sentMessage1); Assert.Equal("My Message", sentMessage2); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests, string>(sub1, "SimpleTest"); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests, string>(sub2, "SimpleTest"); }
public void NoArgMultiSubscriber() { var sub1 = new object(); var sub2 = new object(); bool sentMessage1 = false; bool sentMessage2 = false; SafeMessagingCenter.Subscribe <SafeMessagingCenterTests>(sub1, "SimpleTest", (sender) => sentMessage1 = true); SafeMessagingCenter.Subscribe <SafeMessagingCenterTests>(sub2, "SimpleTest", (sender) => sentMessage2 = true); SafeMessagingCenter.Send(this, "SimpleTest"); Assert.True(sentMessage1); Assert.True(sentMessage2); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests>(sub1, "SimpleTest"); SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests>(sub2, "SimpleTest"); }
public void NothingShouldBeCollected() { var success = false; _subscriber = new TestSubcriber(); // Using a class member so it doesn't get optimized away in Release build var source = new MessagingCenterTestsCallbackSource(); SafeMessagingCenter.Subscribe <TestPublisher>(_subscriber, "test", p => source.SuccessCallback(ref success)); GC.Collect(); GC.WaitForPendingFinalizers(); var pub = new TestPublisher(); pub.Test(); Assert.True(success); // TestCallbackSource.SuccessCallback() should be invoked to make success == true }
public void ThrowOnNullArgs() { Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Subscribe <SafeMessagingCenterTests, string>(null, "Foo", (sender, args) => { })); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Subscribe <SafeMessagingCenterTests, string>(this, null, (sender, args) => { })); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Subscribe <SafeMessagingCenterTests, string>(this, "Foo", null)); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Subscribe <SafeMessagingCenterTests>(null, "Foo", (sender) => { })); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Subscribe <SafeMessagingCenterTests>(this, null, (sender) => { })); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Subscribe(this, "Foo", (Action <SafeMessagingCenterTests>)null)); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Send <SafeMessagingCenterTests, string>(null, "Foo", "Bar")); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Send <SafeMessagingCenterTests, string>(this, null, "Bar")); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Send <SafeMessagingCenterTests>(null, "Foo")); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Send <SafeMessagingCenterTests>(this, null)); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests>(null, "Foo")); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests>(this, null)); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests, string>(null, "Foo")); Assert.Throws <ArgumentNullException>(() => SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests, string>(this, null)); }
public void UnsubscribeInCallback() { int messageCount = 0; var subscriber1 = new object(); var subscriber2 = new object(); SafeMessagingCenter.Subscribe <SafeMessagingCenterTests>(subscriber1, "SimpleTest", (sender) => { messageCount++; SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests>(subscriber2, "SimpleTest"); }); SafeMessagingCenter.Subscribe <SafeMessagingCenterTests>(subscriber2, "SimpleTest", (sender) => { messageCount++; SafeMessagingCenter.Unsubscribe <SafeMessagingCenterTests>(subscriber1, "SimpleTest"); }); SafeMessagingCenter.Send(this, "SimpleTest"); Assert.Equal(1, messageCount); }
public void SubscribeToTestMessages() { SafeMessagingCenter.Subscribe <TestPublisher>(this, "test", p => SetSuccess()); }