void IncrementChangeCounter() { _changeCounter++; if (_changeCounter == CompactionFrequency) { _input.Send(new CompactionAction()); } }
public void Sending_a_message_to_an_nhibernate_backed_state_machine() { TraceLogger.Configure(LogLevel.Debug); _newValue = new Random().Next(1, 500000) / 100m; using (ISession session = SessionFactory.OpenSession()) using (ITransaction transaction = session.BeginTransaction()) { session.CreateQuery("Delete TestStateMachineInstance").ExecuteUpdate(); transaction.Commit(); } var input = new ChannelAdapter(); using (input.Connect(x => { x.AddConsumersFor <TestStateMachineInstance>() .BindUsing <TestStateMachineInstanceBinding, int>() .HandleOnCallingThread() .CreateNewInstanceBy(id => new TestStateMachineInstance(id)) .PersistUsingNHibernate() .UseSessionProvider(() => SessionFactory.OpenSession()); })) { _networkTypes = input.Flatten().Select(c => c.GetType()); var future = new Future <int>(); TestStateMachineInstance.CompletedLatch = new CountdownLatch(1, future.Complete); // input.Send(new UpdateOrder { Id = 47 }); input.Send(new CreateOrder { Id = 27 }); input.Send(new UpdateOrder { Id = 27, Value = _newValue, }); input.Send(new CompleteOrder { Id = 27, }); future.WaitUntilCompleted(5.Seconds()).ShouldBeTrue(); } }
public void Sending_a_message_to_an_nhibernate_backed_state_machine() { TraceLogger.Configure(LogLevel.Debug); _newValue = new Random().Next(1, 500000)/100m; using (ISession session = SessionFactory.OpenSession()) using (ITransaction transaction = session.BeginTransaction()) { session.CreateQuery("Delete TestStateMachineInstance").ExecuteUpdate(); transaction.Commit(); } var input = new ChannelAdapter(); using (input.Connect(x => { x.AddConsumersFor<TestStateMachineInstance>() .BindUsing<TestStateMachineInstanceBinding, int>() .HandleOnCallingThread() .CreateNewInstanceBy(id => new TestStateMachineInstance(id)) .PersistUsingNHibernate() .UseSessionProvider(() => SessionFactory.OpenSession()); })) { _networkTypes = input.Flatten().Select(c => c.GetType()); var future = new Future<int>(); TestStateMachineInstance.CompletedLatch = new CountdownLatch(1, future.Complete); // input.Send(new UpdateOrder { Id = 47 }); input.Send(new CreateOrder { Id = 27 }); input.Send(new UpdateOrder { Id = 27, Value = _newValue, }); input.Send(new CompleteOrder { Id = 27, }); future.WaitUntilCompleted(5.Seconds()).ShouldBeTrue(); } }
public void Sending_a_message_to_an_nhibernate_instance_channel() { _newValue = new Random().Next(1, 500000)/100m; using (ISession session = SessionFactory.OpenSession()) using (ITransaction transaction = session.BeginTransaction()) { session.CreateQuery("Delete TestInstance").ExecuteUpdate(); session.Save(new TestInstance(27)); transaction.Commit(); } var input = new ChannelAdapter(); using (input.Connect(x => { x.AddConsumerOf<UpdateValue>() .UsingInstance() .Of<TestInstance>() .HandleOnCallingThread() .DistributedBy(msg => msg.Id) .PersistUsingNHibernate() .UsingSessionProvider(m => SessionFactory.OpenSession()) .OnChannel(m => m.UpdateValueChannel) .CreateNewInstanceBy(m => new TestInstance(m.Id)); })) { // input.Send(new UpdateValue(27, _newValue)); } }
public void A_state_machine_is_connected_to_an_untyped_channel() { _instance = new SampleStateMachine(); _input = new ChannelAdapter(); _disconnect = _input.Connect(x => { // adds a single state machine instance to the channel via listeners x.AddConsumersFor <SampleStateMachine>() .UsingInstance(_instance) .HandleOnCallingThread(); }); _input.Send(new SampleStarted()); _input.Send(new SampleStopped()); }
public void Sending_a_message_to_an_nhibernate_instance_channel() { _newValue = new Random().Next(1, 500000) / 100m; using (ISession session = SessionFactory.OpenSession()) using (ITransaction transaction = session.BeginTransaction()) { session.CreateQuery("Delete TestInstance").ExecuteUpdate(); session.Save(new TestInstance(27)); transaction.Commit(); } var input = new ChannelAdapter(); using (input.Connect(x => { x.AddConsumerOf <UpdateValue>() .UsingInstance() .Of <TestInstance>() .HandleOnCallingThread() .DistributedBy(msg => msg.Id) .PersistUsingNHibernate() .UsingSessionProvider(m => SessionFactory.OpenSession()) .OnChannel(m => m.UpdateValueChannel) .CreateNewInstanceBy(m => new TestInstance(m.Id)); })) { // input.Send(new UpdateValue(27, _newValue)); } }
public void Sending_a_message_to_an_nhibernate_backed_state_machine() { TraceLogger.Configure(LogLevel.Debug); _newValue = new Random().Next(1, 500000) / 100m; var input = new ChannelAdapter(); using (input.Connect(x => { x.AddConsumersFor <TestStateMachineInstance>() .BindUsing <TestStateMachineInstanceBinding, int>() .HandleOnCallingThread() .CreateNewInstanceBy(id => new TestStateMachineInstance(id)) .PersistInMemoryUsing(_cache); })) { _networkTypes = input.Flatten().Select(c => c.GetType()); var future = new Future <int>(); TestStateMachineInstance.CompletedLatch = new CountdownLatch(1, future.Complete); // input.Send(new UpdateOrder { Id = 47, Value = _newValue, }); input.Send(new CreateOrder { Id = 27 }); input.Send(new UpdateOrder { Id = 27, Value = _newValue, }); input.Send(new CompleteOrder { Id = 27, }); future.WaitUntilCompleted(5.Seconds()).ShouldBeTrue(); } }
public void Should_properly_wrap_the_channel_as_synchronized() { Assert.IsNull(SynchronizationContext.Current); var fiber = new ThreadPoolFiber(); var input = new ChannelAdapter(); var context = new TestSynchronizationContext(); var future = new Future <TestMessage>(); SynchronizationContext.SetSynchronizationContext(context); Assert.IsNotNull(SynchronizationContext.Current); using (input.Connect(x => { x.AddConsumerOf <TestMessage>() .OnCurrentSynchronizationContext() .UsingConsumer(message => { Trace.WriteLine("Received on Thread: " + Thread.CurrentThread.ManagedThreadId); Assert.IsNotNull(SynchronizationContext.Current); Assert.AreEqual(context, SynchronizationContext.Current); future.Complete(message); }); })) { Trace.WriteLine("Subscribed on Thread: " + Thread.CurrentThread.ManagedThreadId); SynchronizationContext.SetSynchronizationContext(null); input.Flatten().Select(c => c.GetType()).ShouldEqual(new[] { typeof(ChannelAdapter), typeof(BroadcastChannel), typeof(TypedChannelAdapter <TestMessage>), typeof(SynchronizedChannel <TestMessage>), typeof(ConsumerChannel <TestMessage>), }); fiber.Add(() => { Trace.WriteLine("Thread: " + Thread.CurrentThread.ManagedThreadId); Assert.IsNull(SynchronizationContext.Current); input.Send(new TestMessage()); }); Assert.IsNull(SynchronizationContext.Current); future.WaitUntilCompleted(2.Seconds()).ShouldBeTrue(); } }
public void Should_properly_setup_the_message_body() { ChannelAdapter channel = new ChannelAdapter(); channel.Send <Message <MyBody> >(msg => { }); }
public void Should_properly_wrap_the_channel_as_synchronized() { Assert.IsNull(SynchronizationContext.Current); var fiber = new PoolFiber(); var input = new ChannelAdapter(); var context = new TestSynchronizationContext(); var future = new Future<TestMessage>(); SynchronizationContext.SetSynchronizationContext(context); Assert.IsNotNull(SynchronizationContext.Current); using (input.Connect(x => { x.AddConsumerOf<TestMessage>() .OnCurrentSynchronizationContext() .UsingConsumer(message => { Trace.WriteLine("Received on Thread: " + Thread.CurrentThread.ManagedThreadId); Assert.IsNotNull(SynchronizationContext.Current); Assert.AreEqual(context, SynchronizationContext.Current); future.Complete(message); }); })) { Trace.WriteLine("Subscribed on Thread: " + Thread.CurrentThread.ManagedThreadId); SynchronizationContext.SetSynchronizationContext(null); input.Flatten().Select(c => c.GetType()).ShouldEqual(new[] { typeof(ChannelAdapter), typeof(BroadcastChannel), typeof(TypedChannelAdapter<TestMessage>), typeof(SynchronizedChannel<TestMessage>), typeof(ConsumerChannel<TestMessage>), }); fiber.Add(() => { Trace.WriteLine("Thread: " + Thread.CurrentThread.ManagedThreadId); Assert.IsNull(SynchronizationContext.Current); input.Send(new TestMessage()); }); Assert.IsNull(SynchronizationContext.Current); future.WaitUntilCompleted(2.Seconds()).ShouldBeTrue(); } }
public void Sending_a_message_to_an_nhibernate_backed_state_machine() { _newValue = new Random().Next(1, 500000)/100m; var input = new ChannelAdapter(); using (input.Connect(x => { x.AddConsumersFor<TestStateMachineInstance>() .BindUsing<TestStateMachineInstanceBinding, int>() .HandleOnCallingThread() .CreateNewInstanceBy(id => new TestStateMachineInstance(id)) .PersistInMemoryUsing(_cache); })) { _networkTypes = input.Flatten().Select(c => c.GetType()); var future = new Future<int>(); TestStateMachineInstance.CompletedLatch = new CountdownLatch(1, future.Complete); // input.Send(new UpdateOrder { Id = 47, Value = _newValue, }); input.Send(new CreateOrder { Id = 27 }); input.Send(new UpdateOrder { Id = 27, Value = _newValue, }); input.Send(new CompleteOrder { Id = 27, }); future.WaitUntilCompleted(5.Seconds()).ShouldBeTrue(); } }
void HandleConnection(DateTime acceptedAt, HttpListenerContext httpContext) { try { _connectionChannel.Send(new HttpConnectionContext(this, httpContext, acceptedAt, ConnectionComplete)); } catch { } }
public void Should_send_to_a_adapter_consumer_chain() { Future <TestMessage> future = new Future <TestMessage>(); var consumer = new ConsumerChannel <TestMessage>(_fiber, future.Complete); var adapter = new ChannelAdapter <TestMessage>(consumer); adapter.Send(new TestMessage()); future.IsCompleted.ShouldBeTrue(); }
void HandleConnection(DateTime acceptedAt, HttpListenerContext httpContext) { try { _connectionChannel.Send(new HttpConnectionContext(this, httpContext, acceptedAt, ConnectionComplete)); } catch (Exception ex) { _log.Error(x => x.Write(ex, "FAILED: {0}", httpContext.Request.Url)); } }
public void Should_work_through_a_channel_network() { var received = new FutureChannel <Request <Simple> >(); UntypedChannel channel = new ChannelAdapter(); channel.Connect(x => x.AddChannel(received)); var simpleImpl = new SimpleImpl(); channel.Send(new RequestImpl <SimpleImpl>(null, simpleImpl)); received.IsCompleted.ShouldBeTrue(); received.Value.ShouldNotBeNull(); received.Value.Body.ShouldEqual(simpleImpl); }
public void Should_remove_a_consumer_from_an_adapter_chain() { var adapter = new ChannelAdapter <TestMessage>(new ShuntChannel <TestMessage>()); var future = new Future <TestMessage>(); var consumer = new ConsumerChannel <TestMessage>(_fiber, future.Complete); using (ChannelConnection scope = adapter.Connect(x => x.AddChannel(consumer))) {} new TraceChannelVisitor().Visit(adapter); adapter.Send(new TestMessage()); future.IsCompleted.ShouldBeFalse(); }
public void Should_remove_my_consumer() { var input = new ChannelAdapter(); var futureA = new Future <TestMessage>(); var consumerA = new ConsumerChannel <TestMessage>(new SynchronousFiber(), futureA.Complete); using (ChannelConnection connection = input.Connect(x => x.AddChannel(consumerA))) { } new TraceChannelVisitor().Visit(input); input.Send(new TestMessage()); futureA.IsCompleted.ShouldBeFalse(); }
public void Should_add_a_consumer_to_an_empty_adapter_chain() { var adapter = new ChannelAdapter <TestMessage>(new ShuntChannel <TestMessage>()); var future = new Future <TestMessage>(); var consumer = new ConsumerChannel <TestMessage>(_fiber, future.Complete); using (var scope = adapter.Connect(x => { x.AddChannel(consumer); })) { new TraceChannelVisitor().Visit(adapter); adapter.Send(new TestMessage()); } future.IsCompleted.ShouldBeTrue(); }
public void Should_add_a_consumer_to_an_empty_adapter_chain() { var adapter = new ChannelAdapter<TestMessage>(new ShuntChannel<TestMessage>()); var future = new Future<TestMessage>(); var consumer = new ConsumerChannel<TestMessage>(_fiber, future.Complete); using (ChannelConnection scope = adapter.Connect(x => { x.AddChannel(consumer); })) { new TraceChannelVisitor().Visit(adapter); adapter.Send(new TestMessage()); } future.IsCompleted.ShouldBeTrue(); }
public void Should_add_a_consumer_that_is_assignable_to_the_type() { var firstFuture = new Future<TestMessage>(); var secondFuture = new Future<ITestMessage>(); var first = new ConsumerChannel<TestMessage>(_fiber, firstFuture.Complete); var subs = new BroadcastChannel<TestMessage>(new[] {first}); var adapter = new ChannelAdapter<TestMessage>(subs); var second = new ConsumerChannel<ITestMessage>(_fiber, secondFuture.Complete); using (var scope = adapter.Connect(x => x.AddChannel(second))) { new TraceChannelVisitor().Visit(adapter); adapter.Send(new TestMessage()); } firstFuture.IsCompleted.ShouldBeTrue(); secondFuture.IsCompleted.ShouldBeTrue(); }
public void Should_add_a_consumer_to_an_existing_adapter_chain() { var firstFuture = new Future <TestMessage>(); var secondFuture = new Future <TestMessage>(); var first = new ConsumerChannel <TestMessage>(_fiber, firstFuture.Complete); var subs = new BroadcastChannel <TestMessage>(new[] { first }); var adapter = new ChannelAdapter <TestMessage>(subs); var second = new ConsumerChannel <TestMessage>(_fiber, secondFuture.Complete); using (var scope = adapter.Connect(x => x.AddChannel(second))) { new TraceChannelVisitor().Visit(adapter); adapter.Send(new TestMessage()); } firstFuture.IsCompleted.ShouldBeTrue(); secondFuture.IsCompleted.ShouldBeTrue(); }
public void Should_add_a_provider_based_consumer() { var input = new ChannelAdapter(); var futureA = new Future <TestMessage>(); var futureB = new Future <TestMessage>(); var futureC = new Future <TestMessage>(); TestConsumer.Future = futureA; using (input.Connect(x => { x.AddConsumerOf <TestMessage>() .UsingInstance().Of <TestConsumer>() .HandleOnCallingThread() .ObtainedBy(() => new TestConsumer()) .OnChannel(y => y.InputChannel); x.AddConsumerOf <TestMessage>() .UsingConsumer(futureB.Complete) .HandleOnCallingThread(); x.AddConsumerOf <TestMessage>() .UsingSelectiveConsumer(message => futureC.Complete) .HandleOnCallingThread(); })) { Trace.WriteLine("Complete network:"); new TraceChannelVisitor().Visit(input); input.Send(new TestMessage()); } Trace.WriteLine("Empty network:"); new TraceChannelVisitor().Visit(input); futureA.IsCompleted.ShouldBeTrue(); futureB.IsCompleted.ShouldBeTrue(); futureC.IsCompleted.ShouldBeTrue(); }
public void Should_add_a_provider_based_consumer() { var input = new ChannelAdapter(); var futureA = new Future<TestMessage>(); var futureB = new Future<TestMessage>(); var futureC = new Future<TestMessage>(); TestConsumer.Future = futureA; using (input.Connect(x => { x.AddConsumerOf<TestMessage>() .UsingInstance().Of<TestConsumer>() .HandleOnCallingThread() .ObtainedBy(() => new TestConsumer()) .OnChannel(y => y.InputChannel); x.AddConsumerOf<TestMessage>() .UsingConsumer(futureB.Complete) .HandleOnCallingThread(); x.AddConsumerOf<TestMessage>() .UsingSelectiveConsumer(message => futureC.Complete) .HandleOnCallingThread(); })) { Trace.WriteLine("Complete network:"); new TraceChannelVisitor().Visit(input); input.Send(new TestMessage()); } Trace.WriteLine("Empty network:"); new TraceChannelVisitor().Visit(input); futureA.IsCompleted.ShouldBeTrue(); futureB.IsCompleted.ShouldBeTrue(); futureC.IsCompleted.ShouldBeTrue(); }
public void Should_manage_interleaved_changes_the_the_chain() { var firstFuture = new Future <TestMessage>(); var secondFuture = new Future <TestMessage>(); var adapter = new ChannelAdapter <TestMessage>(new ShuntChannel <TestMessage>()); var first = new ConsumerChannel <TestMessage>(_fiber, firstFuture.Complete); var firstScope = adapter.Connect(x => x.AddChannel(first)); var second = new ConsumerChannel <TestMessage>(_fiber, secondFuture.Complete); var secondScope = adapter.Connect(x => x.AddChannel(second)); firstScope.Dispose(); new TraceChannelVisitor().Visit(adapter); adapter.Send(new TestMessage()); firstFuture.IsCompleted.ShouldBeFalse(); secondFuture.IsCompleted.ShouldBeTrue(); }
public void Should_be_an_distinct_interval_consumer_on_the_channel() { var future = new Future<int>(); var input = new ChannelAdapter(); int expected = 5; using (input.Connect(x => { x.AddConsumerOf<TestMessage>() .BufferFor(2.Seconds()) .Distinct(c => c.Value) .UsingConsumer(message => future.Complete(message.Count)) .HandleOnCallingThread(); })) { for (int i = 0; i < expected; i++) { input.Send(new TestMessage { Value = i }); } future.WaitUntilCompleted(6.Seconds()).ShouldBeTrue(); future.Value.ShouldEqual(expected); input.Flatten().Select(c => c.GetType()).ShouldEqual(new[] { typeof(ChannelAdapter), typeof(BroadcastChannel), typeof(TypedChannelAdapter<TestMessage>), typeof(IntervalChannel<TestMessage>), typeof(DistinctChannel<TestMessage, int>), typeof(ConsumerChannel<IDictionary<int, TestMessage>>), }); } }
public void Should_be_an_distinct_interval_consumer_on_the_channel() { var future = new Future <int>(); var input = new ChannelAdapter(); int expected = 5; using (input.Connect(x => { x.AddConsumerOf <TestMessage>() .BufferFor(2.Seconds()) .Distinct(c => c.Value) .UsingConsumer(message => future.Complete(message.Count)) .HandleOnCallingThread(); })) { for (int i = 0; i < expected; i++) { input.Send(new TestMessage { Value = i }); } future.WaitUntilCompleted(6.Seconds()).ShouldBeTrue(); future.Value.ShouldEqual(expected); input.Flatten().Select(c => c.GetType()).ShouldEqual(new[] { typeof(ChannelAdapter), typeof(BroadcastChannel), typeof(TypedChannelAdapter <TestMessage>), typeof(IntervalChannel <TestMessage>), typeof(DistinctChannel <TestMessage, int>), typeof(ConsumerChannel <IDictionary <int, TestMessage> >), }); } }
public void Should_register_my_consumer() { var input = new ChannelAdapter(); var futureA = new Future <TestMessage>(); var consumerA = new ConsumerChannel <TestMessage>(new SynchronousFiber(), futureA.Complete); var futureB = new Future <TestMessage>(); var consumerB = new ConsumerChannel <TestMessage>(new SynchronousFiber(), futureB.Complete); using (input.Connect(x => { x.AddChannel(consumerA); x.AddChannel(consumerB); })) { new TraceChannelVisitor().Visit(input); input.Send(new TestMessage()); } futureA.IsCompleted.ShouldBeTrue(); futureB.IsCompleted.ShouldBeTrue(); }
public void Should_manage_interleaved_changes_the_the_chain() { var firstFuture = new Future<TestMessage>(); var secondFuture = new Future<TestMessage>(); var adapter = new ChannelAdapter<TestMessage>(new ShuntChannel<TestMessage>()); var first = new ConsumerChannel<TestMessage>(_fiber, firstFuture.Complete); var firstScope = adapter.Connect(x => x.AddChannel(first)); var second = new ConsumerChannel<TestMessage>(_fiber, secondFuture.Complete); var secondScope = adapter.Connect(x => x.AddChannel(second)); firstScope.Dispose(); new TraceChannelVisitor().Visit(adapter); adapter.Send(new TestMessage()); firstFuture.IsCompleted.ShouldBeFalse(); secondFuture.IsCompleted.ShouldBeTrue(); }
public void Should_work_through_a_channel_network() { var received = new FutureChannel<Request<Simple>>(); UntypedChannel channel = new ChannelAdapter(); channel.Connect(x => x.AddChannel(received)); var simpleImpl = new SimpleImpl(); channel.Send(new RequestImpl<SimpleImpl>(null, simpleImpl)); received.IsCompleted.ShouldBeTrue(); received.Value.ShouldNotBeNull(); received.Value.Body.ShouldEqual(simpleImpl); }
public void Should_be_an_interval_consumer_on_the_channel() { var future = new Future<int>(); var input = new ChannelAdapter(); int expected = 5; using (input.Connect(x => { x.AddConsumerOf<TestMessage>() .BufferFor(2.Seconds()) .UsingConsumer(message => { future.Complete(message.Count); }) .ExecuteOnProducerThread(); })) { for (int i = 0; i < expected; i++) input.Send(new TestMessage()); future.WaitUntilCompleted(6.Seconds()).ShouldBeTrue(); future.Value.ShouldEqual(expected); input.Flatten().Select(c => c.GetType()).ShouldEqual(new[] { typeof(ChannelAdapter), typeof(BroadcastChannel), typeof(TypedChannelAdapter<TestMessage>), typeof(IntervalChannel<TestMessage>), typeof(ConsumerChannel<ICollection<TestMessage>>), }); } }
public void Should_remove_my_consumer() { var input = new ChannelAdapter(); var futureA = new Future<TestMessage>(); var consumerA = new ConsumerChannel<TestMessage>(new SynchronousFiber(), futureA.Complete); using (ChannelConnection connection = input.Connect(x => x.AddChannel(consumerA))) { } new TraceChannelVisitor().Visit(input); input.Send(new TestMessage()); futureA.IsCompleted.ShouldBeFalse(); }
public void Send <T>(T message) { _input.Send(message); }
public void Should_send_to_a_adapter_consumer_chain() { Future<TestMessage> future = new Future<TestMessage>(); var consumer = new ConsumerChannel<TestMessage>(_fiber, future.Complete); var adapter = new ChannelAdapter<TestMessage>(consumer); adapter.Send(new TestMessage()); future.IsCompleted.ShouldBeTrue(); }
public void Should_remove_a_consumer_from_an_adapter_chain() { var adapter = new ChannelAdapter<TestMessage>(new ShuntChannel<TestMessage>()); var future = new Future<TestMessage>(); var consumer = new ConsumerChannel<TestMessage>(_fiber, future.Complete); using (var scope = adapter.Connect(x => x.AddChannel(consumer))) { } new TraceChannelVisitor().Visit(adapter); adapter.Send(new TestMessage()); future.IsCompleted.ShouldBeFalse(); }
public void Should_register_my_consumer() { var input = new ChannelAdapter(); var futureA = new Future<TestMessage>(); var consumerA = new ConsumerChannel<TestMessage>(new SynchronousFiber(), futureA.Complete); var futureB = new Future<TestMessage>(); var consumerB = new ConsumerChannel<TestMessage>(new SynchronousFiber(), futureB.Complete); using (input.Connect(x => { x.AddChannel(consumerA); x.AddChannel(consumerB); })) { new TraceChannelVisitor().Visit(input); input.Send(new TestMessage()); } futureA.IsCompleted.ShouldBeTrue(); futureB.IsCompleted.ShouldBeTrue(); }