public void messageBroker_POCO_broadcast_using_more_then_one_subscriber_should_call_on_different_thread() { TestRunner.Execute(ApartmentState.MTA, () => { var h1 = new ManualResetEvent(false); var h2 = new ManualResetEvent(false); var currentThreadId = Thread.CurrentThread.ManagedThreadId; var s1ThreadId = Thread.CurrentThread.ManagedThreadId; var s2ThreadId = Thread.CurrentThread.ManagedThreadId; var dispatcher = new NullDispatcher(); var broker = new MessageBroker(dispatcher); broker.Subscribe <PocoTestMessage>(this, (s, msg) => { s1ThreadId = Thread.CurrentThread.ManagedThreadId; h1.Set(); }); broker.Subscribe <PocoTestMessage>(this, (s, msg) => { s2ThreadId = Thread.CurrentThread.ManagedThreadId; h2.Set(); }); broker.Broadcast(this, new PocoTestMessage()); ManualResetEvent.WaitAll(new[] { h1, h2 }); currentThreadId.Should().Not.Be.EqualTo(s1ThreadId); currentThreadId.Should().Not.Be.EqualTo(s2ThreadId); }); }
public void SentMessage_CanBeReceived() { broker.Broadcast(new TestMessage { Text = "Hello World" }); // simulate message going over http broker.Receive(channel.LastMessageJson); receiver.LastMessage.Text.ShouldBe("Hello World"); }
public void MessageBroker_broadcast_from_multiple_thread_should_not_fail() { Exception failure = null; var wh = new ManualResetEvent( false ); var run = true; var dispatcher = new NullDispatcher(); var broker = new MessageBroker( dispatcher ); // do some metrics to find a good number of subscription to let the pub do a long running const int metricsCount = 100; for( int i = 0; i < metricsCount; i++ ) { broker.Subscribe<PocoTestMessage>( this, ( sender, msg ) => { } ); } var sw = Stopwatch.StartNew(); broker.Broadcast(this, new PocoTestMessage()); sw.Stop(); const int longRunningTiming = 100; var elapsedMilliseconds = sw.ElapsedMilliseconds == 0 ? longRunningTiming : sw.ElapsedMilliseconds; var subCountForLongRunning = (longRunningTiming / elapsedMilliseconds) * metricsCount; Trace.WriteLine(string.Format("Need {0} subscriptions to run for at least {1} ms. {2} took {3} ms.", subCountForLongRunning, longRunningTiming, metricsCount, sw.ElapsedMilliseconds)); for (int i = 0; i < subCountForLongRunning - metricsCount; i++) { broker.Subscribe<PocoTestMessage>(this, (sender, msg) => { }); } // this will take approximately 100 ms to do 1 broadcast var broadcastThread1 = new Thread( payload => { while( run ) { try { broker.Broadcast( this, new PocoTestMessage() ); } catch( Exception e ) { lock( this ) { failure = e; wh.Set(); } break; } } } ); broadcastThread1.IsBackground = true; broadcastThread1.Start(); // this should istantly throw an error because the broadcasting is enumerating the subscriptions and should take 100 ms to enumerate them all var subscriberThread1 = new Thread(payload => { while (run) { try { broker.Subscribe<PocoTestMessage>(this, (sender, msg) => { Thread.Sleep(10); }); } catch (Exception e) { lock (this) { failure = e; wh.Set(); break; } } } }); subscriberThread1.IsBackground = true; subscriberThread1.Start(); var timeout = 1; var signaled = wh.WaitOne( TimeSpan.FromSeconds( timeout ) ); if( !signaled ) { Trace.WriteLine( String.Format( "Run without any issue for {0} seconds.", timeout ) ); } run = false; subscriberThread1.Join(); Assert.IsNull( failure, failure != null ? failure.ToString() : "--" ); }
public void messageBroker_POCO_should_broadcast_non_POCO_message_with_correct_Sender() { Object expected = this; Object actual = null; var h = new ManualResetEvent( false ); var dispatcher = new NullDispatcher(); var broker = new MessageBroker( dispatcher ); broker.Subscribe<LegacyTestMessage>( this, msg => { actual = msg.Sender; h.Set(); } ); broker.Broadcast( this, new LegacyTestMessage() ); h.WaitOne(); Assert.AreEqual( expected, actual ); }
public void messageBroker_POCO_should_broadcast_non_POCO_message_and_subscribe_as_POCO() { var received = false; var h = new ManualResetEvent( false ); var dispatcher = new NullDispatcher(); var broker = new MessageBroker( dispatcher ); broker.Subscribe<LegacyTestMessage>( this, ( s, msg ) => { received = true; h.Set(); } ); broker.Broadcast( this, new LegacyTestMessage() ); h.WaitOne(); Assert.IsTrue( received ); }
public void messageBroker_broadcast_should_report_expected_exception() { ManualResetEvent h = new ManualResetEvent( false ); var dispatcher = new NullDispatcher(); var broker = new MessageBroker( dispatcher ); broker.Subscribe<LegacyTestMessage>( this, msg => { throw new ArgumentException(); } ); try { broker.Broadcast( new LegacyTestMessage( this ) ); h.WaitOne(); } catch { h.Set(); throw; } }
public void messageBroker_POCO_broadcast_using_more_then_one_subscriber_should_call_on_different_thread() { TestRunner.Execute( ApartmentState.MTA, () => { var h1 = new ManualResetEvent( false ); var h2 = new ManualResetEvent( false ); var currentThreadId = Thread.CurrentThread.ManagedThreadId; var s1ThreadId = Thread.CurrentThread.ManagedThreadId; var s2ThreadId = Thread.CurrentThread.ManagedThreadId; var dispatcher = new NullDispatcher(); var broker = new MessageBroker( dispatcher ); broker.Subscribe<PocoTestMessage>( this, ( s, msg ) => { s1ThreadId = Thread.CurrentThread.ManagedThreadId; h1.Set(); } ); broker.Subscribe<PocoTestMessage>( this, ( s, msg ) => { s2ThreadId = Thread.CurrentThread.ManagedThreadId; h2.Set(); } ); broker.Broadcast( this, new PocoTestMessage() ); ManualResetEvent.WaitAll( new[] { h1, h2 } ); currentThreadId.Should().Not.Be.EqualTo( s1ThreadId ); currentThreadId.Should().Not.Be.EqualTo( s2ThreadId ); } ); }
public void MessageBroker_broadcast_from_multiple_thread_should_not_fail() { Exception failure = null; var wh = new ManualResetEvent(false); var run = true; var dispatcher = new NullDispatcher(); var broker = new MessageBroker(dispatcher); // do some metrics to find a good number of subscription to let the pub do a long running const int metricsCount = 100; for (int i = 0; i < metricsCount; i++) { broker.Subscribe <PocoTestMessage>(this, (sender, msg) => { }); } var sw = Stopwatch.StartNew(); broker.Broadcast(this, new PocoTestMessage()); sw.Stop(); const int longRunningTiming = 100; var elapsedMilliseconds = sw.ElapsedMilliseconds == 0 ? longRunningTiming : sw.ElapsedMilliseconds; var subCountForLongRunning = (longRunningTiming / elapsedMilliseconds) * metricsCount; Trace.WriteLine(string.Format("Need {0} subscriptions to run for at least {1} ms. {2} took {3} ms.", subCountForLongRunning, longRunningTiming, metricsCount, sw.ElapsedMilliseconds)); for (int i = 0; i < subCountForLongRunning - metricsCount; i++) { broker.Subscribe <PocoTestMessage>(this, (sender, msg) => { }); } // this will take approximately 100 ms to do 1 broadcast var broadcastThread1 = new Thread(payload => { while (run) { try { broker.Broadcast(this, new PocoTestMessage()); } catch (Exception e) { lock (this) { failure = e; wh.Set(); } break; } } }); broadcastThread1.IsBackground = true; broadcastThread1.Start(); // this should istantly throw an error because the broadcasting is enumerating the subscriptions and should take 100 ms to enumerate them all var subscriberThread1 = new Thread(payload => { while (run) { try { broker.Subscribe <PocoTestMessage>(this, (sender, msg) => { Thread.Sleep(10); }); } catch (Exception e) { lock (this) { failure = e; wh.Set(); break; } } } }); subscriberThread1.IsBackground = true; subscriberThread1.Start(); var timeout = 1; var signaled = wh.WaitOne(TimeSpan.FromSeconds(timeout)); if (!signaled) { Trace.WriteLine(string.Format("Run without any issue for {0} seconds.", timeout)); } run = false; subscriberThread1.Join(); Assert.IsNull(failure, failure != null ? failure.ToString() : "--"); }