public void MultipleInitializationsRaceConditionTest() { var transportResolver = new TransportResolver(new Dictionary <string, TransportInfo> { { "tr1", new TransportInfo("host", "guest", "guest", null, "InMemory") }, { "tr2", new TransportInfo("host", "guest", "guest", null, "InMemory") } }); var messagingEngine = new MessagingEngine(transportResolver, new ITransportFactory[] { new InMemoryTransportFactory() }); messagingEngine.SerializationManager.RegisterSerializer("txt", typeof(string), new FakeStringSerializer()); var fp1 = new FeedProvider1(messagingEngine); var consoleLogger = new ConsoleLogger(); fp1.Logger = consoleLogger; var fp2 = new FeedProvider2(messagingEngine); fp2.Logger = consoleLogger; var databus = new DataBus(); databus.RegisterFeedProvider("FP1", fp1); databus.RegisterFeedProvider("FP2", fp2); var disposable1 = databus.Channel <string>("FP1").Feed("context1").Subscribe(Console.WriteLine); var disposable2 = databus.Channel <string>("FP2").Feed("context2").Subscribe(Console.WriteLine); Thread.Sleep(10000); disposable1.Dispose(); disposable2.Dispose(); }
public void StatusFlowRepeatsCurrentStatusOnSubscribeTest() { var feedProvider = MockRepository.GenerateMock <IFeedProvider <long, string> >(); feedProvider.Expect(provider => provider.CanProvideFor("context")).IgnoreArguments().Return(true); var feedSource = new Subject <long>(); feedProvider.Expect(provider => provider.CreateFeed("context")).IgnoreArguments() .Return(Observable.Defer(() => feedSource)); var db = new DataBus(200); db.RegisterFeedProvider("feed", feedProvider); var feed = db.Channel <long>("feed").Feed("context"); FeedStatus? latestStatusFlowValue = null; ManualResetEvent statusHandlerCalled = new ManualResetEvent(false); using (feed.Subscribe()) { feedSource.OnNext(1); using (feed.StatusFlow.Subscribe(status => { latestStatusFlowValue = status; statusHandlerCalled.Set(); Console.WriteLine(status); })) { Assert.IsTrue(statusHandlerCalled.WaitOne(200), "Value was not pushed on subscrbe to StatusFlow"); Assert.AreEqual(FeedStatus.Available, latestStatusFlowValue, "Wrong value was pushed on subscrbe to StatusFlow"); } } }
public void ExceptionHandlingTest() { DataBus db = new DataBus(); db.RegisterFeedProvider <string, string>("channel", context => Observable.Interval(TimeSpan.FromMilliseconds(50)).Select(i => context + i)); ManualResetEvent ev = new ManualResetEvent(false); DataBusUnhandledExceptionEventArgs eventArgs = null; db.UnhandledException += (sender, args) => { eventArgs = args; ev.Set(); }; var ex = new Exception("message"); using (db.Channel <string>("channel").Feed("test").Subscribe(s => { throw ex; })) { Assert.IsTrue(ev.WaitOne(500), "Exception was not handled"); Assert.IsNotNull(eventArgs); Assert.AreEqual("channel", eventArgs.ChannelName, "Channel name was not captured."); Assert.AreEqual(ex, eventArgs.Exception, "Exception was not captured."); } }
public void FeedProvidersRegisteredTwiceFailureTest() { DataBus db = new DataBus(); MyFeedProvider feedProvider1 = new MyFeedProvider(); db.RegisterFeedProvider("MyChannel", feedProvider1); db.RegisterFeedProvider("MyChannel", feedProvider1); db.Channel <string>("MyChannel").Feed(1); }
public void NeverRequestFeedForTheSameContextTwiceTest() { DataBus db = new DataBus(); MyFeedProvider feedProvider = new MyFeedProvider(); db.RegisterFeedProvider("MyChannel", feedProvider); AutoResetEvent ev = new AutoResetEvent(false); AutoResetEvent ev1 = new AutoResetEvent(false); int[] c = { 0, 0 }; var subscribtion1 = db.Channel <string>("MyChannel").Feed(1).Subscribe(s => { Console.WriteLine("subscribtion1\t" + s); if (++c[0] == 5) { ev.Set(); } }); var subscribtion2 = db.Channel <string>("MyChannel").Feed(1).Subscribe(s => { Console.WriteLine("subscribtion2\t" + s); if (++c[1] == 5) { ev1.Set(); } }); using (subscribtion1) { using (subscribtion2) { Assert.IsTrue(ev.WaitOne(1000), "Subscribtion handler was not called"); Assert.IsTrue(ev1.WaitOne(1000), "Subscribtion handler was not called"); } } Assert.AreNotEqual(0, feedProvider.SubscribeCounters[1], "Subscriber was not subscribed to the feed "); Assert.AreNotEqual(0, feedProvider.UnsubscribeCounters[1], "Subscriber was not unsubscribed from the feed "); Assert.AreEqual(1, feedProvider.SubscribeCounters[1], "Subscriber was subscribed more then 1 time to the same feed "); Assert.AreEqual(1, feedProvider.UnsubscribeCounters[1], "Subscriber was unsubscribed more then 1 time from the same feed "); }
public void TwoFeedProvidersForSameContextFailureTest() { DataBus db = new DataBus(); MyFeedProvider feedProvider1 = new MyFeedProvider(); MyFeedProvider feedProvider2 = new MyFeedProvider(); db.RegisterFeedProvider("MyChannel", feedProvider1); db.RegisterFeedProvider("MyChannel", feedProvider2); db.Channel <string>("MyChannel").Feed(1); }
public void DeferredSubscriptionStatusTest() { Subject <long> feedSource = new Subject <long>(); Action notefySubscribed = () => { }; var feedProvider = MockRepository.GenerateMock <IFeedProvider <long, string> >(); feedProvider.Expect(provider => provider.CanProvideFor("context")).IgnoreArguments().Return(true); feedProvider.Expect(provider => provider.OnFeedLost("context")).IgnoreArguments().Return(new long[0]); feedProvider.Expect(provider => provider.CreateFeed("context")).IgnoreArguments() .Return( DeferredObservable.CreateWithDisposable <long>((observer, action) => { notefySubscribed = action; return(feedSource.Subscribe(observer)); }) ); using (var db = new DataBus(200)) { db.RegisterFeedProvider("feed", feedProvider); var feed = db.Channel <long>("feed").Feed("context"); var retrievedStatuses = new List <FeedStatus>(); using (feed.StatusFlow.Subscribe(retrievedStatuses.Add)) { using (feed.Subscribe()) { Assert.AreEqual(FeedStatus.Subscribing, feed.Status, "Status is not Subscribing while subscriptions is started and not yet reported as finished"); feedSource.OnNext(1); Assert.AreEqual(FeedStatus.Subscribing, feed.Status, "Status is not Subscribing while subscriptions is started and not yet reported as finished"); notefySubscribed(); Assert.AreEqual(FeedStatus.Available, feed.Status, "Status was not set to Available after subscription is reported as finished"); var f = feedSource; feedSource = new Subject <long>(); f.OnError(new Exception("test")); Assert.AreEqual(FeedStatus.NotAvailable, feed.Status, "Status was not set to NotAvailable after feed source produced an error"); Thread.Sleep(500); } } Assert.That(retrievedStatuses.ToArray(), Is.EqualTo(new[] { FeedStatus.NotSubscribed, FeedStatus.Subscribing, FeedStatus.Available, FeedStatus.NotAvailable, FeedStatus.Subscribing, FeedStatus.NotSubscribed }), "Statuses was not reported correctly via StatusFlow"); } }
public void CanSubscribeReturnsFalseFailureTest() { DataBus db = new DataBus(); MyFeedProvider feedProvider = new MyFeedProvider(); db.RegisterFeedProvider("MyChannel", feedProvider); try { db.Channel <string>("MyChannel").Feed(2); } finally { Assert.IsTrue(feedProvider.CanProvideForWasCalled, "IFeedProvider.CanProvideFor was not called"); } }
public void StatusTest() { Subject <long> feedSource = null; var feedProvider = MockRepository.GenerateMock <IFeedProvider <long, string> >(); feedProvider.Expect(provider => provider.CanProvideFor("context")).IgnoreArguments().Return(true); feedProvider.Expect(provider => provider.OnFeedLost("context")).IgnoreArguments().Return(new long[0]); feedProvider.Expect(provider => provider.CreateFeed("context")).IgnoreArguments() .Return( Observable.Defer(() => { feedSource = new Subject <long>(); return(feedSource); } ) ); using (var db = new DataBus(200)) { db.RegisterFeedProvider("feed", feedProvider); var feed = db.Channel <long>("feed").Feed("context"); var retrievedStatuses = new List <FeedStatus>(); using (feed.StatusFlow.Subscribe(retrievedStatuses.Add)) { using (feed.Subscribe()) { feedSource.OnError(new Exception()); Assert.AreEqual(FeedStatus.NotAvailable, feed.Status, "Status was not set to NotAvailable after feed source produced an error"); Thread.Sleep(300); } } // Assert.AreEqual(retrievedStatuses.Count, "Statuses was not reported correctly via StatusFlow"); Assert.That(retrievedStatuses, Is.EqualTo(new[] { FeedStatus.NotSubscribed, FeedStatus.Subscribing, FeedStatus.Available, FeedStatus.NotAvailable, FeedStatus.Subscribing, FeedStatus.Available, FeedStatus.NotSubscribed }), "Statuses was not reported correctly via StatusFlow"); /* * Assert.AreEqual(FeedStatus.NotSubscribed, retrievedStatuses[0], "Statuses was not reported correctly via StatusFlow"); * Assert.AreEqual(FeedStatus.Subscribing, retrievedStatuses[1], "Statuses was not reported correctly via StatusFlow"); * Assert.AreEqual(FeedStatus.Available, retrievedStatuses[2], "Statuses was not reported correctly via StatusFlow"); * Assert.AreEqual(FeedStatus.NotAvailable, retrievedStatuses[3], "Statuses was not reported correctly via StatusFlow"); * Assert.AreEqual(FeedStatus.Subscribing, retrievedStatuses[4], "Statuses was not reported correctly via StatusFlow"); * Assert.AreEqual(FeedStatus.Available, retrievedStatuses[5], "Statuses was not reported correctly via StatusFlow"); * Assert.AreEqual(FeedStatus.NotSubscribed, retrievedStatuses[6], "Statuses was not reported correctly via StatusFlow"); */ } }
public void SubscribtionTest() { DataBus db = new DataBus(); db.RegisterFeedProvider <string, string>("strings", context => Observable.Interval(TimeSpan.FromMilliseconds(50)).Take(5).Select(i => context + i)); ManualResetEvent ev = new ManualResetEvent(false); Stopwatch sw = Stopwatch.StartNew(); using (db.Channel <string>("strings").Feed("test").Subscribe(s => { Console.WriteLine(sw.Elapsed.Milliseconds + " " + s); ev.Set(); } )) { Assert.IsTrue(ev.WaitOne(400), "Handler was not called"); } }
public void Subscribtion_LatestValueTest() { Subject <string> subject = new Subject <string>(); using (DataBus db = new DataBus()) { db.RegisterFeedProvider <string, string>("channel", context => subject); var feed = db.Channel <string>("channel").Feed("aaa"); using (feed.Subscribe(Console.WriteLine)) { Assert.IsFalse(feed.HasLatestValue, "Feed has last value before first data recieve from feedprovider generated observable"); subject.OnNext("First value"); Assert.IsTrue(feed.HasLatestValue, "Feed DOES NOT have last value before first data recieve from feedprovider generated observable"); Assert.AreEqual("First value", feed.LatestValue, "Last value does not match most recent value produced by feedprovider generated observable"); subject.OnNext("Second value"); Assert.AreEqual("Second value", feed.LatestValue, "Last value does not match most recent value produced by feedprovider generated observable"); } } }
public void RetrySubscribeTest() { var feedProvider = MockRepository.GenerateMock <IFeedProvider <long, string> >(); feedProvider.Expect(provider => provider.CanProvideFor("context")).IgnoreArguments().Return(true); feedProvider.Expect(provider => provider.OnFeedLost("context")).IgnoreArguments().Return(new long[0]); int[] createFeedCallsCount = new[] { 0 }; int[] handlerCallsCount = new[] { 0 }; Subject <long> feed = null; Func <string, IObservable <long> > createFeed = c => { createFeedCallsCount[0]++; feed = new Subject <long>(); return(feed); }; feedProvider.Expect(provider => provider.CreateFeed("context")).Do(createFeed); var db = new DataBus(300); db.RegisterFeedProvider("feed", feedProvider); using (db.Channel <long>("feed").Feed("context").Subscribe(l => handlerCallsCount[0]++)) { feed.OnNext(1); feed.OnError(new Exception()); Thread.Sleep(500); feed.OnNext(2); Thread.Sleep(100); } Assert.Greater(createFeedCallsCount[0], 0, "Feed was not created"); Assert.AreEqual(2, createFeedCallsCount[0], "Feed was not recreated after error"); Assert.AreEqual(2, handlerCallsCount[0], "Handler was not called for values produced after recreation of feed"); }
public void DisposeTest() { bool handlerWasCalled = false; var handler = new Action <long>(l => { handlerWasCalled = true; }); var feedProvider = MockRepository.GenerateMock <IFeedProvider <long, string> >(); feedProvider.Expect(provider => provider.CanProvideFor("context")).IgnoreArguments().Return(true); var db = new DataBus(); var subject = new Subject <long>(); feedProvider.Expect(provider => provider.CreateFeed("context")).IgnoreArguments().Return(subject); db.RegisterFeedProvider("channel1", feedProvider); db.Channel <long>("channel1").Feed("dummy").Subscribe(handler); db.Dispose(); subject.OnNext(1); Assert.That(handlerWasCalled, Is.False, "Handler was called after databus was disposed"); }